微信小程序3種位置API的使用方法詳解
獲取位置
獲取當(dāng)前的地理位置、速度。當(dāng)用戶離開小程序后,此接口無法調(diào)用;當(dāng)用戶點(diǎn)擊“顯示在聊天頂部”時(shí),此接口可繼續(xù)調(diào)用。
wx.getLocation(object)
<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
wx.chooseLocation(object)
<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)置地圖查看位置。
wx.openLocation(OBJECT)
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
js動(dòng)態(tài)添加帶圓圈序號(hào)列表的實(shí)例代碼
這篇文章主要介紹了js動(dòng)態(tài)添加帶圓圈序號(hào)列表的實(shí)例代碼,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-02-02css和js實(shí)現(xiàn)彈出登錄居中界面完整代碼
這篇文章主要介紹了css和js實(shí)現(xiàn)彈出登錄居中界面,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-11-11JavaScript中的boolean布爾值使用學(xué)習(xí)及相關(guān)技巧講解
JavaScript中的boolean布爾值使用學(xué)習(xí)及相關(guān)技巧講解,特別是Boolean的用法以及適用!!將truthy或falsy值轉(zhuǎn)換為布爾值的用法非常巧妙,需要的朋友可以參考下2016-05-05老生常談document.ready和window.onload
這篇文章主要介紹了document.ready和window.onload的相關(guān)知識(shí),包括document.ready和window.onload的區(qū)別,要使用document.ready()或者document.onload()的原因分析,本文結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友參考下吧2024-01-01靈活使用console讓js調(diào)試更簡(jiǎn)單的方法步驟
這篇文章主要介紹了靈活使用console讓js調(diào)試更簡(jiǎn)單的方法步驟,適當(dāng)使用這些方法可以使調(diào)試更容易,更快速,更直觀,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-04-04js驗(yàn)證整數(shù)加保留小數(shù)點(diǎn)的簡(jiǎn)單實(shí)例
這篇文章主要介紹了js驗(yàn)證整數(shù)加保留小數(shù)點(diǎn)的簡(jiǎn)單實(shí)例,有需要的朋友可以參考一下2013-12-12