微信小程序開發(fā)之map地圖實(shí)現(xiàn)教程
前言
微信小程序地圖操作比較簡單,api也很少,使用map組件來展示。說到地圖,那就先來看基礎(chǔ)定位:
定位用到wx.getLocation(OBJECT)
函數(shù),代碼如下:
wx.getLocation({ type: 'wgs84', success: function(res) { var latitude = res.latitude var longitude = res.longitude var speed = res.speed var accuracy = res.accuracy } })
定位成功會(huì)返回四個(gè)參數(shù)值,如下:
map屬性太多,先看一下:
如果用到地圖,基本上所有屬性都會(huì)用到。
下面一一看一下,我們先看效果圖吧,先看真相:
這里我只用了一個(gè)markers,就是定位當(dāng)前位置的紅色markers,用法如下:
wx.getLocation({ type: 'wgs84', // 默認(rèn)為 wgs84 返回 gps 坐標(biāo),gcj02 返回可用于 wx.openLocation 的坐標(biāo) 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: "/assests/imgs/my.png", title: "哪里" }], circles: [{ latitude: res.latitude, longitude: res.longitude, color: '#FF0000DD', fillColor: '#7cb5ec88', radius: 3000, strokeWidth: 1 }] }) } })
這里加了circles,半徑是3000米,具體的api可自行看官網(wǎng)。
接下來看看controls,控制層,在地圖上顯示控件,控件不隨著地圖移動(dòng),看API:
注意看示例圖的右上角,有兩個(gè)按鈕,加減號(hào),是控制地圖scale的數(shù)值變化,動(dòng)態(tài)縮放地圖的,controls用法也很簡單:
controls: [{ id: 1, iconPath: '/assests/imgs/jian.png', position: { left: 320, top: 100 - 50, width: 20, height: 20 }, clickable: true }, { id: 2, iconPath: '/assests/imgs/jia.png', position: { left: 340, top: 100 - 50, width: 20, height: 20 }, clickable: true } ]
最后我們看一張gif圖:
最后上一下具體代碼:
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>
js:
Page({ data: { Height: 0, scale: 13, latitude: "", longitude: "", markers: [], controls: [{ id: 1, iconPath: '/assests/imgs/jian.png', position: { left: 320, top: 100 - 50, width: 20, height: 20 }, clickable: true }, { id: 2, iconPath: '/assests/imgs/jia.png', position: { left: 340, top: 100 - 50, width: 20, height: 20 }, clickable: true } ], circles: [] }, onLoad: function () { var _this = this; wx.getSystemInfo({ success: function (res) { //設(shè)置map高度,根據(jù)當(dāng)前設(shè)備寬高滿屏顯示 _this.setData({ view: { Height: res.windowHeight } }) } }) wx.getLocation({ type: 'wgs84', // 默認(rèn)為 wgs84 返回 gps 坐標(biāo),gcj02 返回可用于 wx.openLocation 的坐標(biāo) 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: "/assests/imgs/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) }, //點(diǎn)擊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) } }) }, //點(diǎn)擊縮放按鈕動(dòng)態(tài)請求數(shù)據(jù) controltap(e) { var that = this; console.log("scale===" + this.data.scale) if (e.controlId === 1) { // if (this.data.scale === 13) { that.setData({ scale: --this.data.scale }) // } } else { // if (this.data.scale !== 13) { that.setData({ scale: ++this.data.scale }) // } } }, })
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
相關(guān)文章
uniapp退出關(guān)閉當(dāng)前小程序或APP的簡單實(shí)現(xiàn)
最近通過Uniapp開發(fā)APP又一個(gè)非常實(shí)用的功能,這篇文章主要給大家介紹了關(guān)于uniapp退出關(guān)閉當(dāng)前小程序或APP的簡單實(shí)現(xiàn),文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-12-12淺談redux, koa, express 中間件實(shí)現(xiàn)對比解析
這篇文章主要介紹了淺談redux, koa, express 中間件實(shí)現(xiàn)對比解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05JSON.parse損壞大數(shù)字的原因解析及解決方案
從10多年前JSON在線編輯器的早期開始,用戶經(jīng)常反映編輯器有時(shí)會(huì)破壞他們JSON文檔中的大數(shù)字的問題,這篇文章主要介紹了為什么JSON.parse會(huì)損壞大數(shù)字,如何解決這個(gè)問題,需要的朋友可以參考下2022-10-10JavaScript實(shí)現(xiàn)單擊下拉框選擇直接跳轉(zhuǎn)頁面的方法
這篇文章主要介紹了JavaScript實(shí)現(xiàn)單擊下拉框選擇直接跳轉(zhuǎn)頁面的方法,涉及javascript控制頁面跳轉(zhuǎn)的相關(guān)技巧,需要的朋友可以參考下2015-07-07使兩個(gè)iframe的高度與內(nèi)容自適應(yīng),且相等
使兩個(gè)iframe的高度與內(nèi)容自適應(yīng),且相等...2006-11-11javascript function調(diào)用時(shí)的參數(shù)檢測常用辦法
js中并不直接支持類似c#的方法重載,所以只能變相的來解決,示意代碼:(利用了內(nèi)置屬性arguments)2010-02-02微信小程序與后臺(tái)PHP交互的方法實(shí)例分析
這篇文章主要介紹了微信小程序與后臺(tái)PHP交互的方法,結(jié)合實(shí)例形式分析了微信小程序基于wx.request(OBJECT)方法與后臺(tái)php程序交互相關(guān)操作技巧與注意事項(xiàng),需要的朋友可以參考下2018-12-12原生js實(shí)現(xiàn)計(jì)算購物車總金額的示例
本文主要介紹了原生js實(shí)現(xiàn)計(jì)算購物車總金額的示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04