小程序map组件的使用
wxml代码
<!-- map地图 -->
<!--
longitude 经度
latitude 纬度
scale 一开始的缩放层级 最小3,最大20
markers 地图标记
bindmarkertap 点击markers时的事件
polyline="{{polyline}}" 绘制多边形
style
-->
<view class="map_container">
<map class="map" id="map" longitude="{{longitude}}" latitude="{{latitude}}" scale="16" markers="{{markers}}" wx:if="{{showMap}}" bindmarkertap="markertap"></map>
</view>
wxss代码
.map_container{
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
width:750rpx;
height: 100vh;
}
map{
width:100%;
height: 100%;
}
js代码
Page({
data: {
markers: [],
polyline:[],
latitude: '',
longitude: '',
showMap:false,
},
markertap(e) {
wx.openLocation({
latitude: this.data.latitude,
longitude: this.data.longitude,
});
console.log(this.data.latitude+' '+this.data.longitude)
},
onLoad: function() {
var that = this;
wx.getLocation({
type: 'wgs84',
success: (res) => {
console.log(res);
that.setData({
latitude: res.latitude,
longitude: res.longitude
})
var marker = [{
id: 1,
latitude: res.latitude,
longitude: res.longitude,
iconPath: "/images/location3.png",
width: 50,
height: 50,
callout: {
content: "距离你最近的茶室34.7Km",
color: "#333333",
fontSize: 13,
borderRadius: 20,
bgColor: "#ffffff",
textAlign: "center" ,
padding: 7,
display: 'ALWAYS'
}
}]
var polyline= [{
points: [
{
longitude:res.longitude,
latitude: res.latitude,
}, {
longitude: res.longitude,
latitude: res.latitude,
}],
color: "#FF0000DD",
width: 2,
dottedLine: true
}]
that.setData({
markers: marker,
polyline:polyline,
});
that.setData({
showMap: true
});
},
});
},
onReady: function () {
},
onShow: function () {
},
onHide: function () {
},
onUnload: function () {
},
onPullDownRefresh: function () {
},
onReachBottom: function () {
},
onShareAppMessage: function () {
}
})