溫馨提示×

溫馨提示×

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

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

react native 獲取地理位置的方法示例

發(fā)布時間:2020-09-01 19:07:13 來源:腳本之家 閱讀:239 作者:JsLin_ 欄目:web開發(fā)

react native 官網(wǎng)介紹了這個 api Geolocation 但是這個api只能返回 經緯度 所以要把經緯度 通過逆地理位置轉義 http://recode.ditu.aliyun.com/jsdoc/geocode_api.html 可通過這個阿里的開放接口

在 react native 中,我們所用的是react native 自帶的api定位功能,無需引入第三方js。

react native 定位是通過Geolocation這個模塊來實現(xiàn)的。想了解更多關于Geolocation的知識請點擊下面 Geolocation自行了解,這里我們主要將他的幾個方法。

static getCurrentPosition(geo_success, geo_error?, geo_options?)
Invokes the success callback once with the latest location info. Supported options: timeout (ms), maximumAge (ms), enableHighAccuracy (bool) On Android, this can return almost immediately if the location is cached or request an update, which might take a while.

static watchPosition(success, error?, options?)
Invokes the success callback whenever the location changes. Supported options: timeout (ms), maximumAge (ms), enableHighAccuracy (bool), distanceFilter(m)

static clearWatch(watchID)

第一個方法是獲取第一次定位時的位置信息,第一個為成功時的回掉函數(shù),還有error時的回掉,第三個是傳狀態(tài)的。
在請求成功函數(shù)中有以下屬性:

  • 經度 : coords.longitude
  • 緯度 : coords.latitude
  • 準確度 : coords.accuracy
  • 海拔 : coords.altitude
  • 海拔準確度 : coords.altitudeAcuracy
  • 行進方向 : coords.heading
  • 地面速度 : coords.speed
  • 時間戳 : new Date(position.timestamp)

在請求失敗函數(shù)中有4種情況(err.code狀態(tài)值):

1為用戶拒絕定位請問
2暫時獲取不到位置信息
3為請求超時
4未知錯誤

第三個options是可選參數(shù),屬性如下:

enableHighAccuracy——指示瀏覽器獲取高精度的位置,默認為false。當開啟后,可能沒有任何影響,也可能使瀏覽器花費更長的時間獲取更精確的位置數(shù)據(jù)。

timeout——指定獲取地理位置的超時時間,默認不限時。單位為毫秒。

maximumAge——最長有效期,在重復獲取地理位置時,此參數(shù)指定多久再次獲取位置。默認為0,表示瀏覽器需要立刻重新計算位置。

static watchPosition(success, error?, options?) 

是多次改變了位置信息時才會觸發(fā),一般觸發(fā)的可能性可能用戶多次刷新數(shù)據(jù),如一個人行車到其他城市,這時如果設置一個監(jiān)聽函數(shù),只要watchid不一樣,就會不斷的觸發(fā)

由于可能會出現(xiàn)緩存的情況,所以Geolocation 為我們提供了一個可以清除緩存的方法watchPosition(),改方法是 用于上一次的定位信息進行清除的。

對了,要啟動react native 的定位功能的話,如果你是android 用戶,你需要先在AndroidManifest.xml中加入以下權限

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

具體實現(xiàn)

import Geolocation from 'Geolocation';

  ......

 getlocal() {
  Geolocation.getCurrentPosition(
   val => {
    let ValInfo =
     '速度:' +
     val.coords.speed +
     '\n經度:' +
     val.coords.longitude +
     '\n緯度:' +
     val.coords.latitude +
     '\n準確度:' +
     val.coords.accuracy +
     '\n行進方向:' +
     val.coords.heading +
     '\n海拔:' +
     val.coords.altitude +
     '\n海拔準確度:' +
     val.coords.altitudeAccuracy +
     '\n時間戳:' +
     val.timestamp;
    this.setState({ LocalPosition: ValInfo });
    console.log("打印地理位置:"+`${val.coords.longitude},${val.coords.latitude}`)
    GET_GPRS({
     "l":`${val.coords.latitude},${val.coords.longitude}`,
     "type":111,
    }).then(res => {
     console.log(JSON.stringify(res))
    })
   },
   val => {
    let ValInfo = '獲取坐標失敗:' + val;
    this.setState({ LocalPosition: ValInfo }); //如果為空的話 沒允許開啟定位服務

   },
  );
 }

這里的 GET_GPRS 是自己封裝的 fech請求

記得開啟 位置訪問權限

打印結果如下:

react native 獲取地理位置的方法示例

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。

向AI問一下細節(jié)

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

AI