实现效果展示 :
wxml:
<map id="map" markers="{{markers}}" bindmarkertap="markertap" show-location longitude='{{lng}}' latitude='{{lat}}' class='map' show-location="{{true}} " catchtap="bindtap_Z">
https://developers.weixin.qq.com/miniprogram/dev/component/map.html
具体参数请点击上方链接
js文件
data: {
lat:'',
lng:'',
Locationlist: [],
onshow:false,
markers: [],
markertapid:0,
images: [],//图片
canUploadImages:0, //是否能上传图片 1.是 0.否
include_points:[], // 所以点位
mapCtx :''
},
onReady() {
this.$popup = this.selectComponent('.popup-component')
this.$photoPopup = this.selectComponent('.popup-photo')
this.setData({ // 获取map组件实例
mapCtx: wx.createMapContext('map')
})
this.getPositioning() // 获取定位
},
async getPositioning() {
const that = this
let lat = ''
let lng = ''
await app.getSelfLocation().then(res => { // 这里可以用wx提供的api获取定位
if (res) {
that.setData({
lng : res.curLng,
lat : res.curLat
})
} else {
this.$toast('获取定位失败')
}
})
const params = {
// 接口所需参数
}
request({
url: '',
data: params,
}).then( res => {
let markers = [];
this.setData({
Locationlist:res.data.data
})
this.data.Locationlist.forEach((item,i)=> { // 提取返回数据,首页自动展示所有定位
this.data.include_points.push({
longitude:Number(item.community.lng),
latitude:Number(item.community.lat)
})
})
this.data.mapCtx.includePoints({ //
padding: [ 80,80,80,80], // 修改页面缩放比例
points:this.data.include_points
})
for (let i = 0; i < res.data.data.length; i++) {
let marker = that.createMarker(res.data.data[i].community,res.data.data[i].isFinish);
marker.id = i
markers.push(marker)
}
that.setData({
markers: markers,
})
}).catch(err => {
console.log(err);
})
},
createMarker(point,i) { // 对数据进行处理 map标点
let marker = {
iconPath: i == '1' ? '../../static/image/okweizhi.png' : '../../static/image/noweizhi.png', // 这是气泡的样式 可以从阿里icon下载自己需要的图标,我这需要显示到达和未到达的两种样式,在这加了个判断
pid:point.id,
title: point.name || '',
latitude:Number(point.lat),
longitude: Number(point.lng),
state:i,
width: 30,
height: 30,
callout:{
content:point.name,
anchorY: 0,
borderRadius:10,
padding:10,
anchorX: 0,
display: 'BYCLICK', // 点击时显示气泡 这里再调试器上显示不出来,手机上功能正常
textAlign:'center',
}
};
return marker;
},