1、定位用到 wx.getLocation 函數,地圖使用 map組件,詳細教程參照官網; 注意事項:在給經度 和緯度賦值時,千萬不能賦錯;
2、先看下效果圖:
3、在wxml頁面定義map組件,包含 longitude、latitude、markers、scale、
show-location屬性,並設置了 寬度和高度自適應(由於我設置了page是100%,所以我這里高度設置100%就是自適應容器高度了),代碼如下:
wxml:
<view class="" hover-class="none" hover-stop-propagation="false" style="width: 100%; height: 100%;"> <map id="map" longitude="{{longitude}}" latitude=" {{latitude}}" markers="{{markers}}" scale="13" show-location style="width: 100%; height: 100%;"></map> {{longitude}} </view>
wxss:
page{ height: 100%; }
js:
Page({ data:{ longitude: 113.324520, latitude: 23.099994, markers:[{ id: 0, iconPath: "../../images/icon_cur_position.png", latitude: 23.099994, longitude: 113.324520, width: 50, height: 50 }] }, onLoad: function(){ var that = this; wx.getLocation({ type: "wgs84", success: function(res){ var latitude = res.latitude; var longitude = res.longitude; //console.log(res.latitude); that.setData({ latitude: res.latitude, longitude: res.longitude, markers:[{ latitude: res.latitude, longitude: res.longitude }] }) } }) }, onReady: function(){ } })