<map id="myMap" show-location />
| 属性 | 说明 |
|---|---|
| longitude | 经度;必填 |
| latitude | 纬度;必填 |
| show-location | 显示带有方向的当前定位点 |
| show-scale | 显示缩放标尺 |
| show-compass | 显示罗盘 |
| markers |
在地图上显示标记的位置;结合地图服务API - 地点搜索实现
latitude:标记经度; 必填
longitude:标记维度; 必填
iconPath:标记图片路径; 必填
id:标记id;number;注意是数字类型;每个标记的id应该不同; 必填
width:标记宽度;实际开发 必填
height:标记高度;实际开发 必填
label:标签;在标记下方显示信息;对象类型
callout:单击标记,在上方显示content信息,单击地图其它地方,信息消失;对象类型
|
| bindtap | 单击地图,获取单击点经纬度信息 |
| bindmarkertap | 单击地图标记,获取标记点id |
| bindcallouttap | 单击标记的气泡触发 |
| bindlabeltap | 单击标记的标签触发 |
| 属性 | callout | label |
|---|---|---|
| display | √:BYCLICK、ALWAYS、NEVER | × |
| content | √ | √ |
| color | √ | √ |
| fontSize | √ | √ |
| anchorX | √:marker 对应的经纬度 | √ |
| anchorY | √:marker 对应的经纬度 | √ |
| borderWidth | √ | √ |
| borderColor | √ | √ |
| borderRadius | √ | √ |
| bgColor | √ | √ |
| padding | √ | √ |
| textAlign | √:left, right, center | √ |
Page({
onReady: function (e) {
// 使用 wx.createMapContext 获取 map 上下文
this.mapCtx = wx.createMapContext('myMap')
}
})
| item | desc |
|---|---|
| getCenterLocation(Object object) | 获取当前地图中心的经纬度;返回 gcj02 坐标系,可以用于 wx.openLocation() |
| moveToLocation(Object object) | 将地图中心移置当前定位点;地图组件 需 设置 show-location 为 true;经纬度非必须 |
| addMarkers(Object object) | 在地图上添加标记 marker |
| removeMarkers(Object object) | 移除地图上标记 marker |
"requiredPrivateInfos": [
"getLocation"
]
"getLocation:fail the api need to be declared in the requiredPrivateInfos field in app.json/ext.json"
wx.getLocation({
type: 'gcj02',
success: res => console.log('getLocation ok', res),
fail: err => console.log('getLocation fail', err)
})
"permission": {
"scope.userLocation": {
"desc": "你的位置信息将用于小程序位置接口的效果展示"
}
}
wx.openLocation({
latitude: 25.26,
longitude: 110.32,
name: '桂林市体育中心',
address: '七星区穿山东路12号'
})
chooseLoc() {
wx.chooseLocation({
success: res => console.log('chooseLocation ok', res),
fail: err => console.log('chooseLocation fail', err)
})
}
choosePoi() {
wx.choosePoi({
success: res => console.log('choose poi ok', res),
fail: err => console.log('choose poi fail', err)
})
}
chooseAddr() {
wx.chooseAddress({
success: res => console.log('chooseAddress ok', res),
fail: err => console.log('chooseAddress fail', err)
})
}
"requiredPrivateInfos":[
"getLocation",
"chooseLocation",
"choosePoi",
"chooseAddress"
]
"permission": {
"scope.userLocation": {
"desc": "你的位置信息将用于小程序位置接口的效果展示"
}
}
<map
class="map"
id="map"
longitude="{{longitude}}"
latitude="{{latitude}}"
markers="{{markers}}
show-location
show-scale
show-compass
bindtap='getMapInfo'
bindmarkertap="getMarker"
bindlabeltap="getLabel"
bindcallouttap="getCallout"
bindregionchange="changeRegion"/>
<image
class="img"
src="../../utils/center.png"
mode="aspectFill"
bind:tap="moveToLocation" />
map{
width: 100vw;
height: 100vh;
}
.img {
position: fixed;
bottom: 20px;
left: 20px;
width: 30px;
height: 30px;
}
let mapCtx;
Page({
data: {
longitude: "",
latitude: ""
markers: []
},
onLoad(options) {
mapCtx = wx.createMapContext('myMap')
wx.getLocation({
type: 'gcj02',
success: res => {
this.setData({
longitude: res.longitude,
latitude: res.latitude
})
},
fail: err => console.log('getLocation fail', err)
})
}
})
addCenterMarkers(lati, long) {
let markers = this.data.markers
let ind = markers.length
let marker = {
id: ind,
latitude: lati,
longitude: long,
iconPath: '../../utils/marker.png',
width: 32,
height: 32,
callout: {
content: 'top',
color: '#ff4400',
textAlign: 'center',
display: 'BYCLICK'
},
label: {
content: 'center',
color: '#ff4400',
textAlign: 'cneter'
}
}
markers.push(marker)
this.setData({
markers
})
}
moveToLocation() {
mapCtx.moveToLocation({
longitude: this.data.longitude,
latitude: this.data.latitude
})
}
changeRegion(e) {
console.log('moving', e);
}
getMapInfo(e) {
console.log(e);
wx.showToast({
title: 'map tapped',
})
// 每次单击地图时,增加一个 markers
this.addCenterMarkers(e.detail.latitude, e.detail.longitude)
},
getMarker(e) {
console.log('marker tap', e);
wx.showToast({
title: 'marker tapped',
})
},
getLabel(e) {
console.log('label tap', e);
wx.showToast({
title: 'label tapped',
})
},
getCallout(e) {
console.log('callout tap', e);
wx.showToast({
title: 'callout tapped',
})
}