react native 獲取地理位置的方法示例
react native 官網介紹了這個 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ù)據。
timeout——指定獲取地理位置的超時時間,默認不限時。單位為毫秒。
maximumAge——最長有效期,在重復獲取地理位置時,此參數(shù)指定多久再次獲取位置。默認為0,表示瀏覽器需要立刻重新計算位置。
static watchPosition(success, error?, options?)
是多次改變了位置信息時才會觸發(fā),一般觸發(fā)的可能性可能用戶多次刷新數(shù)據,如一個人行車到其他城市,這時如果設置一個監(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基于FlatList下拉刷新上拉加載實現(xiàn)代碼示例
- react-native動態(tài)切換tab組件的方法
- react-native android狀態(tài)欄的實現(xiàn)
- react-native封裝插件swiper的使用方法
- react-native-video實現(xiàn)視頻全屏播放的方法
- React Native 自定義下拉刷新上拉加載的列表的示例
- ReactNative實現(xiàn)Toast的示例
- React Native之prop-types進行屬性確認詳解
- ReactNative中使用Redux架構總結
- React Native react-navigation 導航使用詳解
- react native 仿微信聊天室實例代碼
相關文章
react反向代理使用http-proxy-middleware問題
這篇文章主要介紹了react反向代理使用http-proxy-middleware問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-07-07React中的setState使用細節(jié)和原理解析(最新推薦)
這篇文章主要介紹了React中的setState使用細節(jié)和原理解析(最新推薦),前面我們有使用過setState的基本使用, 接下來我們對setState使用進行詳細的介紹,需要的朋友可以參考下2022-12-12