React-native 獲取當(dāng)前定位地理位置

0.60以下的RN通過(guò)高德的逆地理編碼獲取當(dāng)前所在位置的信息
不需要任何插件!

// 獲取位置并逆地理轉(zhuǎn)換
  handleGetLocation=()=>{
    // 當(dāng)前定位經(jīng)緯度
    navigator.geolocation.getCurrentPosition(
      (position) => {
        const initialPosition = position
        console.log(initialPosition)
        const { longitude } = initialPosition.coords
        const { latitude } = initialPosition.coords
        console.log(`${longitude},${latitude}`)
        //通過(guò)調(diào)用高德地圖逆地理接口,傳入經(jīng)緯度獲取位置信息
        fetch(`http://restapi.amap.com/v3/geocode/regeo?key=你得高德KEY&location=${longitude},${latitude}&radius=1000&extensions=all&batch=false&roadlevel=0`, {
          method: "POST",
          headers: {
            "Content-Type": "application/x-www-form-urlencoded"
          },
          body: ``
        })
          .then((response) => response.json())
          .then((jsonData) => {
            try {
              console.log(jsonData)
            }catch (e) {

            }
          })
          .catch((error) => {
            console.error(error);
          })
      },
      (error) => console.log(error),
      {  timeout: 20000, maximumAge: 1000 },
    )
  }

0.60版本RN以上的需要下載@react-native-community/geolocation
官網(wǎng)地址:
https://reactnative.cn/docs/0.62/geolocation

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容