日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發(fā)布時(shí)間:2025/3/8 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 reactnative 获取定位_[RN] React Native 获取地理位置 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

import React, {Component} from 'react';

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

exportdefault classTestGeo extends Component {

state={

longitude:'',//經(jīng)度

latitude: '',//緯度

city: '',

district:'',

street:'',

position:'',//位置名稱

};

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

};

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

location=>{this.setState({

longitude: location.coords.longitude,//經(jīng)度

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

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

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(

經(jīng)度:{this.state.longitude}

緯度:{this.state.latitude}

城市:{this.state.city}

區(qū)域:{this.state.district}

街道:{this.state.street}

詳細(xì)位置:{this.state.position}

);

}

}const styles =StyleSheet.create({

container: {

flex:1,

justifyContent:'center',

alignItems:'center',

backgroundColor:'#F5FCFF',

},

instructions: {

textAlign:'center',

color:'#333333',

marginBottom:5,

},

});

總結(jié)

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

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。