微信小程序地圖(map)組件點(diǎn)擊(tap)獲取經(jīng)緯度的方法
微信小程序中使用地圖(map)組件,通過點(diǎn)擊(tap)獲取經(jīng)緯度,按照官方的回應(yīng),暫時(shí)是沒法做到的,從地圖組件API多有殘缺判斷,懷疑是個(gè)實(shí)習(xí)生干的...
做個(gè)變通,適用性有限,請(qǐng)大家參考?;舅悸肪褪窃诘貓D上鋪滿一層marker,從而通過點(diǎn)擊marker獲得經(jīng)緯度。
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, //將圖片設(shè)置為透明,通過開發(fā)者工具看不出效果,但真機(jī)是有效果的 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() } })
效果如圖
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
JavaScript實(shí)現(xiàn)列出數(shù)組中最長(zhǎng)的連續(xù)數(shù)
這篇文章主要介紹了JavaScript實(shí)現(xiàn)列出數(shù)組中最長(zhǎng)的連續(xù)數(shù)的方法及使用,需要的朋友可以參考下2014-12-12基于Bootstrap的后臺(tái)管理面板 Bootstrap Metro Dashboard
這篇文章主要介紹了基于Bootstrap的后臺(tái)管理面板:Bootstrap Metro Dashboard,對(duì)Bootstrap的后臺(tái)管理面板感興趣的小伙伴們可以參考一下2016-06-06JS加jquery簡(jiǎn)單實(shí)現(xiàn)標(biāo)簽元素的顯示或隱藏
標(biāo)簽元素的顯示或隱藏在使用中還是挺頻繁的,下面有個(gè)不錯(cuò)的示例,大家可以參考下,或許有所幫助2013-09-09JS使用ActiveXObject實(shí)現(xiàn)用戶提交表單時(shí)屏蔽敏感詞功能
這篇文章主要介紹了JS采用ActiveXObject實(shí)現(xiàn)用戶在提交表單時(shí)屏蔽敏感詞的功能的相關(guān)資料,需要的朋友可以參考下2017-06-06JavaScript中的new的使用方法與注意事項(xiàng)
JavaScript中的new的使用方法與注意事項(xiàng)...2007-05-05