DDR爱好者之家 Design By 杰米
微信小程序中使用地图(map)组件,通过点击(tap)获取经纬度,按照官方的回应,暂时是没法做到的,从地图组件API多有残缺判断,怀疑是个实习生干的...
做个变通,适用性有限,请大家参考。基本思路就是在地图上铺满一层marker,从而通过点击marker获得经纬度。
复制代码 代码如下:<map id="map" longitude="102.324520" latitude="40.099994" scale="4" bindcontroltap="controltap" polygons="{{polygons}}" bindregionchange="regionchange" markers="{{markers}}" bindmarkertap="markertap" show-location style="width: 100%; height: 700px;"></map>
const app = getApp()
const markersize = 30
function range(start, edge, step) {
for (var ret = [];
(edge - start) * step > 0; start += step) {
ret.push(start);
}
return ret;
}
function markers(northeast, southwest, scale, width, height) {
const markerslng = (northeast.longitude - southwest.longitude) * markersize / width
const markerslat = (northeast.latitude - southwest.latitude) * markersize / height
const maxlon = northeast.longitude
const minlon = southwest.longitude
const maxlat = northeast.latitude
const minlat = southwest.latitude
const lons = range(minlon, maxlon, markerslng)
const lats = range(minlat, maxlat, markerslat)
let _markers = []
lons.forEach((lon, i) => {
lats.forEach((lat, j) => {
_markers.push({
id: lon + ',' + lat,
latitude: lat,
longitude: lon,
iconPath: '/marker.png',
alpha: 0.1, //将图片设置为透明,通过开发者工具看不出效果,但真机是有效果的
width: markersize,
height: markersize
})
})
})
return _markers
}
Page({
data: {
polygons: [],
controls: [{
id: 1,
position: {
left: 0,
top: 300 - 50,
width: 50,
height: 50
},
clickable: true
}],
markers: []
},
createMarkers() {
this.mapCtx = wx.createMapContext('map')
const query = wx.createSelectorQuery()
const map = query.select('#map').boundingClientRect()
let that = this
that.mapCtx.getRegion({
success(res1) {
that.mapCtx.getScale({
success(res2) {
query.exec((res) => {
let width = res[0].width;
let height = res[0].height;
let _markers = markers(res1.northeast, res1.southwest, res2.scale, width, height)
that.data.markers = _markers
that.setData(that.data)
})
}
})
}
})
},
regionchange(e) {
this.createMarkers()
},
markertap(e) {
console.log(e.markerId)
},
controltap(e) {
console.log(e.controlId)
},
onReady(e) {
this.createMarkers()
}
})
效果如图
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
DDR爱好者之家 Design By 杰米
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
DDR爱好者之家 Design By 杰米
暂无评论...
更新日志
2025年11月07日
2025年11月07日
- 小骆驼-《草原狼2(蓝光CD)》[原抓WAV+CUE]
- 群星《欢迎来到我身边 电影原声专辑》[320K/MP3][105.02MB]
- 群星《欢迎来到我身边 电影原声专辑》[FLAC/分轨][480.9MB]
- 雷婷《梦里蓝天HQⅡ》 2023头版限量编号低速原抓[WAV+CUE][463M]
- 群星《2024好听新歌42》AI调整音效【WAV分轨】
- 王思雨-《思念陪着鸿雁飞》WAV
- 王思雨《喜马拉雅HQ》头版限量编号[WAV+CUE]
- 李健《无时无刻》[WAV+CUE][590M]
- 陈奕迅《酝酿》[WAV分轨][502M]
- 卓依婷《化蝶》2CD[WAV+CUE][1.1G]
- 群星《吉他王(黑胶CD)》[WAV+CUE]
- 齐秦《穿乐(穿越)》[WAV+CUE]
- 发烧珍品《数位CD音响测试-动向效果(九)》【WAV+CUE】
- 邝美云《邝美云精装歌集》[DSF][1.6G]
- 吕方《爱一回伤一回》[WAV+CUE][454M]
