其他API

来自CloudWiki
Awei讨论 | 贡献2020年5月17日 (日) 05:22的版本 (创建页面,内容为“== 其他API == ==== wx.getLocation函数 ==== 作用:获取当前的地理位置、速度。当用户离开小程序后,此接口无法调用。 ====== 参…”)
(差异) ←上一版本 | 最后版本 (差异) | 下一版本→ (差异)
跳转至: 导航搜索

其他API

wx.getLocation函数

作用:获取当前的地理位置、速度。当用户离开小程序后,此接口无法调用。

参数

属性1.png

object.success 回调函数

Success.png

代码示例
 //index.js
Page({
 data: {
   demo_la: 48.1545,
   demo_lo: 82.3555907,
   latitude: 0,
   longitude: 0,
   begin_la: 0,
   begin_lo: 0,
   interval: 0,
   speed: 0,
   accuracy: 0,
   markers: [{
     iconPath: "../../imgs/pic.jpg",
     id: 0,
     latitude: 39.9205,
     longitude: 116.4605,
     width: 30,
     height: 30
   }]
 },
 openLoc() {
   this.mapCtx.includePoints({
     padding: [10],
     points: [{
       latitude: 23.10229,
       longitude: 113.3345211,
     }, {
       latitude: 23.00229,
       longitude: 113.3345211,
     }]
   })
 },
 geoInfo() {
   var that = this
   wx.getLocation({
     type: 'gcj02',
     scale:18,
     success(res) {
       that.setData({
         latitude: res.latitude,
         longitude: res.longitude,
         speed: res.speed,
         accuracy: res.accuracy,
       })
     }
   })
   console.log(this.data.latitude, this.data.longitude)
 },
 startRun() {
   var that = this
   clearInterval(this.data.interval)
   wx.getLocation({
     type: 'gcj02',
     success(res) {
       var la = res.latitude
       var lo = res.longitude
       that.setData({
         begin_la: la,
         begin_lo: lo,
         'markers[1]': {
           iconPath: "../../imgs/red.png",
           id: 1,
           latitude: la,
           longitude: lo,
           width: 20,
           height: 20
         }
       })
     }
   })
   var interval = setInterval(() => {
     this.running()
   }, 1000)
 },
 stopRun() {
   var that = this
   clearInterval(this.data.interval)
   wx.getLocation({
     type: 'gcj02',
     success(res) {
       var la = res.latitude
       var lo = res.longitude
       that.setData({
         latitude: la,
         longitude: lo,
         'markers[2]': {
           iconPath: "../../imgs/yellow.png",
           id: 2,
           latitude: la,
           longitude: lo,
           width: 20,
           height: 20
         }
       })
     }
   })
   console.log('开始位置', this.data.begin_la, this.data.begin_lo)
   console.log('结束位置', this.data.latitude, this.data.longitude)
   console.log(this.data)
 },
 running() {
   this.data.latitude += 0.0002
   this.data.longitude += 0.0002
 },
 center() {
   this.mapCtx.getCenterLocation({
     success: function (res) {
       console.log(res.latitude,res.longitude)
     }
   })
 },
 trans() {
   this.mapCtx.translateMarker({
     markerId: 0,
     autoRotate: false,
     duration: 3000,
     destination: {
       latitude: 40.30229,
       longitude: 116.7545211,
     },
     animationEnd() {
       console.log('animation end')
     }
   })
 },
 onReady: function () {
   this.mapCtx = wx.createMapContext('myMap')
 },
})
 //index.wxml
 <view class='top'>
   <button bindtap='geoInfo'>位置</button>
   <button bindtap='openLoc'>打开</button>
   <button bindtap='center'>定心</button>
   <button bindtap='trans'>动</button>
 </view>
 
 <map id='myMap' style="width:100%; height:350px" longitude='模板:Longitude' latitude='模板:Latitude' markers='模板:Markers' polyline="模板:Polyline" show-location subkey='IWLBZ-CK6R4-YXDUO-DQCXK-J5H4Z-DSBCI'>
   <cover-view calss='cover'>
     经度:模板:Longitude 纬度:模板:Latitude 速度:模板:Speed
   </cover-view>
 </map>
 
 <button bindtap='startRun'>开始</button>
 <button bindtap='stopRun'>结束</button>