微信小程序map组件 markers 展示当前位置&&修改标记点图标

2022-06-16 10:11:31 浏览数 (1)

当前位置展示:

代码语言:javascript复制
<view>
	<map id="myMap" show-location class="container"  style="width: 100%; height: 800rpx;" />
</view>
代码语言:javascript复制
onShow: function() {
	this.mapCtx = wx.createMapContext('myMap')
	this.mapCtx.moveToLocation()
}

改个标记点的默认样式:

方法一

代码语言:javascript复制
<view>
	<map id="myMap"  scale="16"  markers="{{markers}}" bindmarkertap="markertap" bindregionchange="regionchange" show-location style="width: 100%; height: 400px;"></map>
</view>
代码语言:javascript复制
Page({
  data: {
	markers: [{
			iconPath: "../../static/images/home/icon.png",
			longitude:121.45088,
			latitude:31.25145,
			id: 0,
			width: 34,
			height: 49
		}]
  },
	regionchange(e) {
		console.log(e)
	},
	markertap(e) {
		console.log(e)
	},
  onShow: function () {
	  this.mapCtx = wx.createMapContext('myMap')
	  this.mapCtx.moveToLocation()
	  
	  const lat= "markers[0].latitude";
	  const log= "markers[0].longitude";
	  var that = this;
	  wx.getLocation({
	    type: "wgs84",
	    success: function(res){
	      that.setData({
	     [lat]:res.latitude,
	  	 [log]:res.longitude
	      })
	    }
	  })
  }
})

方法二

代码语言:javascript复制
<view>
	<map id="map" longitude="{{longitude}}" latitude="{{latitude}}" scale="16"  markers="{{markers}}" bindmarkertap="markertap" bindregionchange="regionchange" show-location style="width: 100%; height: 400px;"></map>
</view>
代码语言:javascript复制
Page({
  data: {
			longitude:121.45088,
			latitude:31.25145,
	markers: [{
			iconPath: "../../static/images/home/icon.png",
			longitude:121.45088,
			latitude:31.25145,
			id: 0,
			width: 34,
			height: 49
		}]
  },
	regionchange(e) {
		console.log(e)
	},
	markertap(e) {
		console.log(e)
	},
  onShow: function () {
	  const lat= "markers[0].latitude";
	  const log= "markers[0].longitude";
	  var that = this;
	  wx.getLocation({
	    type: "wgs84",
	    success: function(res){
	      that.setData({
	       latitude: res.latitude,
	       longitude: res.longitude,
	       [lat]:res.latitude,
	  	   [log]:res.longitude
	      })
	    }
	  })
  }
})

0 人点赞