您的位置:首页 > 移动开发 > 微信开发

微信小程序<map>改变地图缩放级别

2018-03-05 16:09 597 查看

微信小程序<map>改变地图缩放级别



右上角,有两个按钮,分别为级别加减号,用来控制地图scale的数值变化,动态缩放地图

1.map.wxml

<map id="map" longitude="{{longitude}}" latitude="{{latitude}}" scale="{{scale}}" controls="{{controls}}" bindcontroltap="controltap" markers="{{markers}}" circles="{{circles}}" bindmarkertap="markertap" polyline="{{polyline}}" bindregionchange="regionchange" show-location style="width:100%; height:{{view.Height}}px;"
></map>

2.map.js

Page({
data: {
Height: 0,
scale: 13,
latitude: "",
longitude: "",
markers: [],
controls: [{
id: 1,
iconPath: '../images/jia.png',
position: {
left: 250,
top: 100,
width: 60,
height: 60
},
clickable: true
},{
id: 2,
iconPath: '../images/jian.png',
position: {
left: 250,
top: 160,
width: 60,
height: 60
},
clickable: true
}],
circles: []
},

onLoad: function () {
var _this = this;
wx.getSystemInfo({
success: function (res) {
//设置map高度,根据当前设备宽高满屏显示
_this.setData({
view: {
Height: res.windowHeight
}
})
}
})

wx.getLocation({
type: 'wgs84', // 默认为 wgs84 返回 gps 坐标,gcj02 返回可用于 wx.openLocation 的坐标
success: function (res) {
_this.setData({
latitude: res.latitude,
longitude: res.longitude,
markers: [{
id: "1",
latitude: res.latitude,
longitude: res.longitude,
width: 50,
height: 50,
iconPath: "../images/my.png",
title: "我的当前位置"
}],
circles: [{
latitude: res.latitude,
longitude: res.longitude,
color: '#FF0000DD',
fillColor: '#7cb5ec88',
radius: 3000,
strokeWidth: 1
}]
})
}
})
},

regionchange(e) {
console.log("regionchange===" + e.type)
},

//点击merkers
markertap(e) {
console.log(e.markerId)
wx.showActionSheet({
itemList: ["A"],
success: function (res) {
console.log(res.tapIndex)
},
fail: function (res) {
console.log(res.errMsg)
}
})
},

//点击缩放按钮动态请求数据
controltap(e) {
var that = this;
console.log("scale===" + this.data.scale)
if (e.controlId === 1) {
that.setData({
scale: --this.data.scale
})
} else {
that.setData({
scale: ++this.data.scale
})
}
},
})
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: