前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >微信小程序map组件 markers 展示当前位置&&修改标记点图标

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

作者头像
江咏之
发布2022-06-16 10:11:31
4.4K0
发布2022-06-16 10:11:31
举报
文章被收录于专栏:技术社区

当前位置展示:

在这里插入图片描述
在这里插入图片描述
代码语言: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
	      })
	    }
	  })
  }
})
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021-07-22,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档