微信小程序實例-獲取當前的地理位置、速度

微信小程序官方文檔 

https://mp.weixin.qq.com/debug/wxadoc/dev/api/location.htmlhtml

JS代碼

//index.js
//獲取應用實例
var app = getApp()
Page({
  data: {
    motto: '示例小程序-獲取當前地理位、速度',
    userInfo: {},
    hasLocation:false,
    location:{}
  },
  //事件處理函數
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onLoad: function () {
    console.log('onLoad')
    var that = this
    //調用應用實例的方法獲取全局數據
    app.getUserInfo(function(userInfo){
      //更新數據
      that.setData({
        userInfo:userInfo
      })
    }),
     wx.getLocation( {
      success: function( res ) {
        console.log( res )
        that.setData( {
          hasLocation: true,
          location: {
            longitude: res.longitude,
            latitude: res.latitude
          }
        })
      }
    })
  }
})

index.wxmlgit

<!--index.wxml-->
<view class="container">
  <view  bindtap="bindViewTap" class="userinfo">
    <image class="userinfo-avatar" src="{{userInfo.avatarUrl}}" background-size="cover"></image>
    <text class="userinfo-nickname">{{userInfo.nickName}}</text>
  </view>
  <view class="usermotto">
   <!-- <text class="user-motto">{{motto}}\n</text>-->
    <text>經度:{{location.longitude}}\n</text>
    <text>緯度:{{location.latitude}}</text>
  </view>
</view>

運行效果

相關文章
相關標籤/搜索