【发布时间】:2017-10-23 18:56:47
【问题描述】:
我正在尝试使用react-native-geocoder库通过设备的经纬度返回地址。
通过对另一个问题的回答和更多研究,我想出了以下代码:
import React, { Component } from 'react';
import {
AppRegistry,
View,
Text
} from 'react-native';
import Geocoder from 'react-native-geocoder'; // 0.5.0
Geocoder.apiKey = '__API__KEY__';
export default class testeGeocoder extends Component {
constructor(props) {
super(props);
this.state = {
latitude: null,
longitude: null,
place: 'Localizando endereço...',
error: null,
};
}
componentDidMount() {
navigator.geolocation.getCurrentPosition(
(position) => {
this.setState({
latitude: position.coords.latitude,
longitude: position.coords.longitude,
error: null,
});
},
(error) => this.setState({ error: error.message }),
{ enableHighAccuracy: true, timeout: 20000, maximumAge: 1000 },
);
Geocoder.geocodePosition(this.state.latitude,this.state.longitude)
.then(res => {
this.setState({
place: res[0].formatedAddress
});
console.log(res[0].formattedAddress)
});
}
render() {
return (
<View style={{ flexGrow: 1, alignItems: 'center', justifyContent: 'center' }}>
<Text>Latitude: {this.state.latitude}</Text>
<Text>Longitude: {this.state.longitude}</Text>
<Text>{this.state.place.toString()}</Text>
{this.state.error ? <Text>Error: {this.state.error}</Text> : null}
</View>
);
}
}
AppRegistry.registerComponent('testeGeocoder', () => testeGeocoder);
但这会将我返回到正确的纬度和经度,但会一直定位地址......并且永远不会返回。
编辑:
在 bennygenel 和 Michael Cheng 的帮助下,我设法消除了警告并得到了以下代码:
import React, { Component } from 'react';
import {
AppRegistry,
View,
Text
} from 'react-native';
import Geocoder from 'react-native-geocoder'; // 0.5.0
Geocoder.apiKey = '__API__KEY__';
export default class teste47 extends Component {
constructor(props) {
super(props);
this.state = {
latitude: null,
longitude: null,
place: 'Localizando endereço...',
error: null,
};
}
componentDidMount() {
navigator.geolocation.getCurrentPosition(
position => {
this.setState(
{
latitude: position.coords.latitude,
longitude: position.coords.longitude,
error: null,
},
() => {
Geocoder.geocodePosition({
lat: position.coords.latitude,
lng: position.coords.longitude
}).then(res => {
this.setState({
place: res[0].formattedAddress,
});
});
}
);
},
error => this.setState({ error: error.message }),
{
enableHighAccuracy: true, timeout: 20000
});
}
render() {
return (
<View style={{ flexGrow: 1, alignItems: 'center', justifyContent: 'center' }}>
<Text>Latitude: {this.state.latitude}</Text>
<Text>Longitude: {this.state.longitude}</Text>
<Text>{this.state.place.toString()}</Text>
{this.state.error ? <Text>Error: {this.state.error}</Text> : null}
</View>
);
}
}
AppRegistry.registerComponent('teste47', () => teste47);
但是当我执行它时它返回错误:
【问题讨论】:
-
最后一条评论,你可能想从问题中编辑掉那个 API 密钥,或者只是撤销它并创建一个新的。这可能不是您想要公开的内容;) 撤销可能是更安全的选择,因为我不确定是否可以进行不会保存在 StackOverflow 历史中的编辑。
-
@MichaelCheng 看不到key的问题?它是我创建的免费公钥吗,例如,在我使用另一个的应用程序中,有什么可以做会伤害我的事情吗?
-
我不知道您如何生成密钥的具体细节,但在一般实践中,即使您不重复使用 API 密钥,您也不希望任何人使用您的密钥。您的密钥通常存在限制,过度使用可能会导致您达到阈值。请参阅:Google Maps Geocoding API Usage Limits。
标签: react-native geocode