日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

reactnative 获取定位_[RN] React Native 获取地理位置

發布時間:2025/3/8 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 reactnative 获取定位_[RN] React Native 获取地理位置 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

import React, {Component} from 'react';

import {StyleSheet, Text, View}from 'react-native';

exportdefault classTestGeo extends Component {

state={

longitude:'',//經度

latitude: '',//緯度

city: '',

district:'',

street:'',

position:'',//位置名稱

};

componentWillMount= () =>{this.getPositions();

};

getPositions= () =>{return new Promise(() =>{/** 獲取當前位置信息*/navigator.geolocation.getCurrentPosition(

location=>{this.setState({

longitude: location.coords.longitude,//經度

latitude: location.coords.latitude,//緯度

});//通過調用高德地圖逆地理接口,傳入經緯度獲取位置信息

fetch(`http://restapi.amap.com/v3/geocode/regeo?key=97c933e33025b3843b40016900074704&location=${this.state.longitude},${this.state.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)=>{//console.log(jsonData)

try{this.setState({

city: jsonData.regeocode.addressComponent.city,

district: jsonData.regeocode.addressComponent.district,

street: jsonData.regeocode.addressComponent.township,

position: jsonData.regeocode.formatted_address,

});

}catch(e) {

}

})

.catch((error) =>{

console.error(error);

});

},

error=>{

console.error(error);

}

);

})

}

render() {return(

經度:{this.state.longitude}

緯度:{this.state.latitude}

城市:{this.state.city}

區域:{this.state.district}

街道:{this.state.street}

詳細位置:{this.state.position}

);

}

}const styles =StyleSheet.create({

container: {

flex:1,

justifyContent:'center',

alignItems:'center',

backgroundColor:'#F5FCFF',

},

instructions: {

textAlign:'center',

color:'#333333',

marginBottom:5,

},

});

總結

以上是生活随笔為你收集整理的reactnative 获取定位_[RN] React Native 获取地理位置的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。