小程式開發之map地圖實現教程,程式開發map
前言
小程式地圖操作比較簡單,api也很少,使用map組件來展示。說到地圖,那就先來看基礎定位:
定位用到wx.getLocation(OBJECT)函數,代碼如下:
wx.getLocation({ type: 'wgs84', success: function(res) { var latitude = res.latitude var longitude = res.longitude var speed = res.speed var accuracy = res.accuracy }})
定位成功會返回四個參數值,如下:
map屬性太多,先看一下:
如果用到地圖,基本上所有屬性都會用到。
下面一一看一下,我們先看吧,先看真相:
這裡我只用了一個markers,就是定位當前位置的紅色markers,用法如下:
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: "/assests/imgs/my.png", title: "哪裡" }], circles: [{ latitude: res.latitude, longitude: res.longitude, color: '#FF0000DD', fillColor: '#7cb5ec88', radius: 3000, strokeWidth: 1 }] }) } })
這裡加了circles,半徑是3000米,具體的api可自行看官網。
接下來看看controls,控制層,在地圖上顯示控制項,控制項不隨著地圖移動,看API:
注意看樣本圖的右上方,有兩個按鈕,加減號,是控制地圖scale的數值變化,動態縮放地圖的,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) { //設定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: "/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) }, //點擊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) { // if (this.data.scale === 13) { that.setData({ scale: --this.data.scale }) // } } else { // if (this.data.scale !== 13) { that.setData({ scale: ++this.data.scale }) // } } },})
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作能帶來一定的協助,如果有疑問大家可以留言交流,謝謝大家對幫客之家的支援。