1. 程式人生 > >微信小程式——地圖map

微信小程式——地圖map

地圖的注意事項:

  1. tip: map 元件是由客戶端建立的原生元件,它的層級是最高的,不能通過 z-index 控制層級。
  2. tip: 請勿在 scroll-viewswiperpicker-viewmovable-view 中使用 map 元件。
  3. tip: css 動畫對 map 元件無效。
  4. tip: map 元件使用的經緯度是火星座標系,呼叫 wx.getLocation 介面需要指定 typegcj02

地圖的屬性:
屬性名 型別 預設值 說明 最低版本
longitude Number   中心經度  
latitude Number   中心緯度  
scale Number 16 縮放級別,取值範圍為5-18  
markers Array   標記點  
covers Array   即將移除,請使用 markers  
polyline Array   路線  
circles Array    
controls Array   控制元件  
include-points Array   縮放視野以包含所有給定的座標點  
show-location Boolean   顯示帶有方向的當前定位點  
bindmarkertap EventHandle   點選標記點時觸發,會返回marker的id  
bindcallouttap EventHandle   點選標記點對應的氣泡時觸發,會返回marker的id 1.2.0
bindcontroltap EventHandle   點選控制元件時觸發,會返回control的id  
bindregionchange EventHandle   視野發生變化時觸發  
bindtap EventHandle   點選地圖時觸發  
bindupdated EventHandle   在地圖渲染更新完成時觸發 1.6.0

標記點——markers屬性:標記點用於在地圖上顯示標記的位置

id:marker點選事件回撥會返回此id。建議為每個marker設定上Number型別id,保證更新marker時有更好的效能。

latitude:緯度

longitude:經度

title:標註點名

iconPath:顯示的圖示

rotate:旋轉角度

alpha:標註的透明度

width:標註圖示的寬度

height:標註圖示的高度

callout:自定義標記點上方的氣泡視窗

label:為標記點旁邊增加標籤

anchor:經緯度在標註圖示的錨點

marker 上的氣泡 callout
屬性 說明 型別 最低版本
content 文字 String 1.2.0
color 文字顏色 String 1.2.0
fontSize 文字大小 Number 1.2.0
borderRadius callout邊框圓角 Number 1.2.0
bgColor 背景色 String 1.2.0
padding 文字邊緣留白 Number 1.2.0
display 'BYCLICK':點選顯示; 'ALWAYS':常顯 String 1.2.0
textAlign 文字對齊方式。有效值: left, right, center String 1.6.0
marker 上的氣泡 label
屬性 說明 型別 最低版本
content 文字 String 1.2.0
color 文字顏色 String 1.2.0
fontSize 文字大小 Number 1.2.0
x label的座標(廢棄) Number 1.2.0
y label的座標(廢棄) Number 1.2.0
anchorX label的座標,原點是 marker 對應的經緯度 Number 2.1.0
anchorY label的座標,原點是 marker 對應的經緯度 Number 2.1.0
borderWidth 邊框寬度 Number 1.6.0
borderColor 邊框顏色 String 1.6.0
borderRadius 邊框圓角 Number 1.6.0
bgColor 背景色 String 1.6.0
padding 文字邊緣留白 Number 1.6.0
textAlign 文字對齊方式。有效值: left, right, center String 1.6.0

路線——polyline指定一系列座標點,從陣列第一項連線至最後一項

points:[{latitude: 0, longitude: 0}]經緯陣列

color:線的顏色

width:線的寬度

dottedLine:是否為虛線

arrowLine:帶箭頭的線

arrowIconPath:更換箭頭圖示

borderColor:線的邊框顏色

borderWidth:線的厚度

圓——circles

latitude:緯度

longitude:經度

color:描邊的顏色

fillColor:填充顏色

radius:半徑

strokWidth:描邊的寬度

舉個例子:

<map id="map" longitude="116.410440" latitude="39.970830" scale="14" controls="{{controls}}" bindtap="openMap" bindcontroltap="controltap" markers="{{markers}}" bindmarkertap="markertap" polyline="{{polyline}}" bindregionchange="regionchange" show-location style="width: 100%; height: 150px;"></map>

相關api:wx.createMapContext

這是api的例子:

<!-- map.wxml -->
<map id="myMap" show-location />

<button type="primary" bindtap="getCenterLocation">獲取位置</button>
<button type="primary" bindtap="moveToLocation">移動位置</button>
<button type="primary" bindtap="translateMarker">移動標註</button>
<button type="primary" bindtap="includePoints">縮放視野展示所有經緯度</button>
Page({
  onReady: function (e) {
    // 使用 wx.createMapContext 獲取 map 上下文
    this.mapCtx = wx.createMapContext('myMap')
  },
  getCenterLocation: function () {
    this.mapCtx.getCenterLocation({
      success: function(res){
        console.log(res.longitude)
        console.log(res.latitude)
      }
    })
  },
  moveToLocation: function () {
    this.mapCtx.moveToLocation()
  },
  translateMarker: function() {
    this.mapCtx.translateMarker({
      markerId: 0,
      autoRotate: true,
      duration: 1000,
      destination: {
        latitude:23.10229,
        longitude:113.3345211,
      },
      animationEnd() {
        console.log('animation end')
      }
    })
  },
  includePoints: function() {
    this.mapCtx.includePoints({
      padding: [10],
      points: [{
        latitude:23.10229,
        longitude:113.3345211,
      }, {
        latitude:23.00229,
        longitude:113.3345211,
      }]
    })
  }
})

wx.createMapContext(mapId, this)

方法 引數 說明 最低版本
getCenterLocation OBJECT 獲取當前地圖中心的經緯度,返回的是 gcj02 座標系,可以用於 wx.openLocation  
moveToLocation 將地圖中心移動到當前定位點,需要配合map元件的show-location使用  
translateMarker OBJECT 平移marker,帶動畫 1.2.0
includePoints OBJECT 縮放視野展示所有經緯度 1.2.0
getRegion OBJECT 獲取當前地圖的視野範圍 1.4.0
getScale OBJECT 獲取當前地圖的縮放級別 1.4.0