国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

微信小程序地圖(map)組件點擊(tap)獲取經(jīng)緯度的方法

2019-11-19 12:18:04
字體:
供稿:網(wǎng)友

微信小程序中使用地圖(map)組件,通過點擊(tap)獲取經(jīng)緯度,按照官方的回應(yīng),暫時是沒法做到的,從地圖組件API多有殘缺判斷,懷疑是個實習(xí)生干的...

做個變通,適用性有限,請大家參考。基本思路就是在地圖上鋪滿一層marker,從而通過點擊marker獲得經(jīng)緯度。

復(fù)制代碼 代碼如下:
<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 = 30function 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, //將圖片設(shè)置為透明,通過開發(fā)者工具看不出效果,但真機是有效果的    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() }})

效果如圖

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 石渠县| 临安市| 长葛市| 常宁市| 佛山市| 蚌埠市| 白银市| 沁水县| 长兴县| 诏安县| 巢湖市| 密山市| 左权县| 砀山县| 宜良县| 渝北区| 分宜县| 仙桃市| 津南区| 宿迁市| 丹凤县| 永和县| 九江县| 宝山区| 织金县| 红河县| 和静县| 靖远县| 南澳县| 万载县| 三门峡市| 阿勒泰市| 濮阳县| 宁安市| 乐昌市| 泸水县| 嘉峪关市| 芜湖市| 垣曲县| 普兰店市| 宁陵县|