Android 如何在地图上获得固定位置名称?
我已经用CRNA创建了一个android应用程序,我可以通过世博会的位置API获取用户的当前位置。但我不知道如何获得固定位置的城市名称?这是我目前的代码:Android 如何在地图上获得固定位置名称?,android,react-native,expo,react-native-maps,create-react-native-app,Android,React Native,Expo,React Native Maps,Create React Native App,我已经用CRNA创建了一个android应用程序,我可以通过世博会的位置API获取用户的当前位置。但我不知道如何获得固定位置的城市名称?这是我目前的代码: class Map extends Component { constructor(props) { super(props); this.state = { region: { latitude: 41.0141977, longitude: 28.9638121,
class Map extends Component {
constructor(props) {
super(props);
this.state = {
region: {
latitude: 41.0141977,
longitude: 28.9638121,
latitudeDelta: 0.1,
longitudeDelta: 0.05,
},
x: {
latitude: 41.0238343,
longitude: 29.0335236,
},
regionName: "",
}
}
onDragEnd(e) {
this.setState({x: e.nativeEvent.coordinate});
}
onRegionChange(region) {
this.setState({region});
}
render() {
return (
<Modal
animationType={"slide"}
transparent={true}
visible={this.props.visible}
onRequestClose={this.props.changeState}
style={styles.modal}
>
<MapView
region={this.state.region}
onRegionChange={this.onRegionChange.bind(this)}
style={styles.map}
><MapView.Marker draggable
coordinate={this.state.x}
onDragEnd={this.onDragEnd.bind(this)}
/></MapView>
</Modal>
);
}
}
类映射扩展组件{
建造师(道具){
超级(道具);
此.state={
地区:{
纬度:41.0141977,
经度:28.9638121,
纬度德尔塔:0.1,
纵向德尔塔:0.05,
},
x:{
纬度:41.0238343,
经度:29.0335236,
},
区域名称:“”,
}
}
昂德拉根(e){
this.setState({x:e.nativeEvent.coordinate});
}
onRegionChange(地区){
this.setState({region});
}
render(){
返回(
);
}
}
为此,您需要使用一些地理编码
大体上看一下谷歌地图页面。
这里有一个react native。为此,您需要使用一些地理编码 大体上看一下谷歌地图页面。
这是一个针对react native的应用程序。随着我在谷歌上搜索的次数越来越多,我了解到它被称为反向地理编码。因此,我创建了一个这样的redux操作,它很好地解决了我的问题:
export function fetchCityName(lat, lng) {
const link = `http://maps.googleapis.com/maps/api/geocode/json?latlng=${lat},${lng}`;
return dispatch => {
dispatch(startFetch());
return fetch(link)
.then(response => response.json())
.then(responseJson => {
const addressComponent = _.get(responseJson, 'results[0].address_components', []);
const getAreaName = zone => _.get(
addressComponent,
`[${_.findIndex(addressComponent, obj => _.includes(obj.types, zone))}].long_name`
);
const region = ' ' + getAreaName('administrative_area_level_1');
const country = ' ' + getAreaName('country');
const region2 = getAreaName('administrative_area_level_2');
const location = region2 ?
[region2, region, country].toString() :
region.concat(',' + country);
dispatch(getCityName(location));
})
.catch(console.error)
随着我在谷歌上搜索的越来越多,我知道这叫做反向地理编码。因此,我创建了一个这样的redux操作,它很好地解决了我的问题:
export function fetchCityName(lat, lng) {
const link = `http://maps.googleapis.com/maps/api/geocode/json?latlng=${lat},${lng}`;
return dispatch => {
dispatch(startFetch());
return fetch(link)
.then(response => response.json())
.then(responseJson => {
const addressComponent = _.get(responseJson, 'results[0].address_components', []);
const getAreaName = zone => _.get(
addressComponent,
`[${_.findIndex(addressComponent, obj => _.includes(obj.types, zone))}].long_name`
);
const region = ' ' + getAreaName('administrative_area_level_1');
const country = ' ' + getAreaName('country');
const region2 = getAreaName('administrative_area_level_2');
const location = region2 ?
[region2, region, country].toString() :
region.concat(',' + country);
dispatch(getCityName(location));
})
.catch(console.error)