欢迎光临,了解微信小程序开发,就上易用通!

获取当前的地理位置

发布:2018-01-25 11:53浏览: 来源:网络 作者:tianshu

JS代码
  1. //index.js
  2. //获取应用实例
  3. var app = getApp()
  4. Page({
  5.   data: {
  6.     motto: '示例小程序-获取当前地理位、速度',
  7.     userInfo: {},
  8.     hasLocation:false,
  9.     location:{}
  10.   },
  11.   //事件处理函数
  12.   bindViewTap: function() {
  13.     wx.navigateTo({
  14.       url: '../logs/logs'
  15.     })
  16.   },
  17.   onLoad: function () {
  18.     console.log('onLoad')
  19.     var that = this
  20.     //调用应用实例的方法获取全局数据
  21.     app.getUserInfo(function(userInfo){
  22.       //更新数据
  23.       that.setData({
  24.         userInfo:userInfo
  25.       })
  26.     }),
  27.      wx.getLocation( {
  28.       success: function( res ) {
  29.         console.log( res )
  30.         that.setData( {
  31.           hasLocation: true,
  32.           location: {
  33.             longitude: res.longitude,
  34.             latitude: res.latitude
  35.           }
  36.         })
  37.       }
  38.     })
  39.   }
  40. })


index.wxml
  1. <!--index.wxml-->
  2. <view class="container">
  3.   <view  bindtap="bindViewTap" class="userinfo">
  4.     <image class="userinfo-avatar" src="{{userInfo.avatarUrl}}" background-size="cover"></image>
  5.     <text class="userinfo-nickname">{{userInfo.nickName}}</text>
  6.   </view>
  7.   <view class="usermotto">
  8.    <!-- <text class="user-motto">{{motto}}\n</text>-->
  9.     <text>经度:{{location.longitude}}\n</text>
  10.     <text>纬度:{{location.latitude}}</text>
  11.   </view>
  12. </view>
运行效果 获取当前的地理位置(图1) 



免责声明:本站所有文章和图片均来自用户分享和网络收集,文章和图片版权归原作者及原出处所有,仅供学习与参考,请勿用于商业用途,如果损害了您的权利,请联系网站客服处理。