溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

微信小程序3種位置API的使用方法詳解

發(fā)布時(shí)間:2020-08-22 13:17:22 來源:腳本之家 閱讀:406 作者:dq_095 欄目:web開發(fā)

獲取位置

獲取當(dāng)前的地理位置、速度。當(dāng)用戶離開小程序后,此接口無法調(diào)用;當(dāng)用戶點(diǎn)擊“顯示在聊天頂部”時(shí),此接口可繼續(xù)調(diào)用。

微信小程序3種位置API的使用方法詳解

微信小程序3種位置API的使用方法詳解

wx.getLocation(object)

微信小程序3種位置API的使用方法詳解

微信小程序3種位置API的使用方法詳解

<view class="container">
 <button bindtap='getLocation'>獲取位置</button>
 <view wx:if="{{latitude !=''}}">
  <view>緯度:{{latitude}}</view>
  <view>經(jīng)度:{{longitude}}</view>
  <view>速度:{{speed}}</view>
  <view>位置的精確度:{{accuracy}}</view>
  <view>高度:{{altitude}}</view>
  <view>垂直精度:{{accuracy}}</view>
  <view>水平精度:{{accuracy}}</view>
 </view>
</view>
//index.js
//獲取應(yīng)用實(shí)例
const app = getApp()
Page({
 data: {
  latitude: '',
  longitude: '',
  speed: '',
  accuracy: '',
  altitude:'',
  verticalAccuracy: '',
  horizontalAccuracy:''
 },
 onLoad: function () {
 },
 getLocation:function(){
  var _this=this;
  wx.getLocation({
   type: 'wgs84',
   success: function (res) {
    var latitude = res.latitude
    var longitude = res.longitude
    var speed = res.speed
    var accuracy = res.accuracy
    var altitude = res.altitude
    var verticalAccuracy = res.verticalAccuracy
    var horizontalAccuracy = res.horizontalAccuracy
    _this.setData({
     latitude: latitude,
     longitude: longitude,
     speed: speed,
     accuracy: accuracy,
     altitude: altitude,
     verticalAccuracy: verticalAccuracy,
     horizontalAccuracy: horizontalAccuracy
    })
   }
  })
 }
})

打開地圖選擇位置

wx.chooseLocation(OBJECT)

打開地圖選擇位置。

需要用戶授權(quán) scope.userLocation

微信小程序3種位置API的使用方法詳解

微信小程序3種位置API的使用方法詳解

微信小程序3種位置API的使用方法詳解

微信小程序3種位置API的使用方法詳解

wx.chooseLocation(object)

微信小程序3種位置API的使用方法詳解

微信小程序3種位置API的使用方法詳解

<view class="container">
 <button bindtap='getLocation'>打開地圖選擇位置</button>
 <view wx:if="{{address !=''}}">
  <view>位置名稱:{{name}}</view>
  <view>詳細(xì)地址:{{address}}</view>
  <view>緯度:{{latitude}}</view>
  <view>經(jīng)度:{{longitude}}</view>
 </view>
</view>
//index.js
//獲取應(yīng)用實(shí)例
const app = getApp()
Page({
 data: {
  name: '',
  address: '',
  latitude: '',
  longitude: ''
 },
 onLoad: function () {
 },
 getLocation:function(){
  var _this=this;
  wx.chooseLocation({
   success: function (res) {
    var name = res.name
    var address = res.address
    var latitude = res.latitude
    var longitude = res.longitude
    _this.setData({
     name: name,
     address: address,
     latitude: latitude,
     longitude: longitude
    })
   }
  })
 }
})

​使用微信內(nèi)置地圖查看位置

使用微信內(nèi)置地圖查看位置。

微信小程序3種位置API的使用方法詳解

微信小程序3種位置API的使用方法詳解

wx.openLocation(OBJECT)

微信小程序3種位置API的使用方法詳解

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI