小程序地图上marker点击后改变尺寸并且将上个点击过的marker恢复原来大小,并将地图中心位置以该MARKER为中心
时间: 2024-06-11 13:09:36 浏览: 15
实现这个功能需要使用小程序提供的地图组件和marker组件,以及相关的API。
首先,在地图组件的属性中设置中心点center和缩放级别scale,以便在marker点击后改变地图中心位置和缩放级别。
在页面的data中定义一个变量lastMarkerId,用于记录上一次点击的marker的id。
然后,在marker组件的属性中设置id和点击事件bindtap,当marker被点击时,触发事件中的回调函数。
在回调函数中,先判断lastMarkerId是否为空,如果不为空,则将上一个点击过的marker恢复原来大小,然后将lastMarkerId设置为当前marker的id。
然后,使用setMarkerScale API将当前marker的大小改变为指定尺寸。
接下来,使用getCenterLocation API获取当前地图中心位置,然后使用setCenterOffset API将地图中心位置以当前marker为中心点。
最后,在页面的onLoad生命周期函数中初始化地图,并将map实例保存在页面的data中,以便后续使用。
示例代码如下:
```
<view class="map-container">
<map id="myMap"
bindregionchange="onRegionChange"
longitude="{{longitude}}"
latitude="{{latitude}}"
scale="{{scale}}"
markers="{{markers}}"
show-location
style="width: 100%; height: 100%;"
></map>
</view>
Page({
data: {
longitude: 116.404,
latitude: 39.915,
scale: 14,
markers: [
{
id: 1,
latitude: 39.915,
longitude: 116.404,
width: 30,
height: 30,
iconPath: "/images/marker.png"
},
{
id: 2,
latitude: 39.918,
longitude: 116.397,
width: 30,
height: 30,
iconPath: "/images/marker.png"
}
],
lastMarkerId: null,
map: null
},
onLoad: function () {
this.mapCtx = wx.createMapContext('myMap');
this.setData({
map: this.mapCtx
});
},
onRegionChange: function () {
// do something
},
onMarkerTap: function (event) {
var markerId = event.markerId;
var lastMarkerId = this.data.lastMarkerId;
if (lastMarkerId !== null) {
this.setMarkerScale(lastMarkerId, 30, 30);
}
this.setMarkerScale(markerId, 50, 50);
this.setData({
lastMarkerId: markerId
});
this.setCenterOffset(markerId);
},
setMarkerScale: function (markerId, width, height) {
this.data.map.includePoints({
padding: [50, 50, 50, 50],
points: [{
latitude: this.data.markers[markerId - 1].latitude,
longitude: this.data.markers[markerId - 1].longitude
}]
});
this.data.map.getScale({
success: function(res) {
var scale = res.scale;
var newWidth = width / scale;
var newHeight = height / scale;
var markers = this.data.markers;
markers[markerId - 1].width = newWidth;
markers[markerId - 1].height = newHeight;
this.setData({
markers: markers
});
}.bind(this)
});
},
setCenterOffset: function (markerId) {
this.data.map.getCenterLocation({
success: function (res) {
var latitude = res.latitude;
var longitude = res.longitude;
var markerLatitude = this.data.markers[markerId - 1].latitude;
var markerLongitude = this.data.markers[markerId - 1].longitude;
var offsetLatitude = markerLatitude - latitude;
var offsetLongitude = markerLongitude - longitude;
this.data.map.moveToLocation({
latitude: latitude + offsetLatitude,
longitude: longitude + offsetLongitude
});
}.bind(this)
});
}
});
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![whl](https://img-home.csdnimg.cn/images/20210720083646.png)
![whl](https://img-home.csdnimg.cn/images/20210720083646.png)
![whl](https://img-home.csdnimg.cn/images/20210720083646.png)
![whl](https://img-home.csdnimg.cn/images/20210720083646.png)