【发布时间】:2018-09-26 10:24:23
【问题描述】:
我正在尝试在我的基本反应应用程序中获取纬度/经度值,我尝试了以下方法:
1) 使用了 navigator.geolocation.getCurrentPosition() react 模块,但显示的是 null 值。代码如下:
class DetailsScreen extends React.Component {
constructor(props) {
super(props);
this.state = {
latitude: null,
longitude: null,
error: null,
};
}
componentDidMount() {
navigator.geolocation.getCurrentPosition(
({coords}) => {
const {latitude, longitude} = coords
this.setState({
position: {
latitude,
longitude,
},
region: {
latitude,
longitude,
latitudeDelta: 0.001,
longitudeDelta: 0.001,
}
})
},
(error) => alert(JSON.stringify(error)),
// 'enableHighAccuracy' sometimes causes problems
// If it does, just remove it.
{enableHighAccuracy: false}
);
}
render(){
return(
<View style = {styles.container}>
<Text style = {styles.boldText}>
Latitude:
</Text>
<Text>
{this.state.latitude}
</Text>
<Text style = {styles.boldText}>
Longitude:
</Text>
<Text>
{this.state.longitude}
</Text>
</View>
)
}
}
2) 使用 react-native-geolocation 服务获取 lat/long 值,但再次显示 null 值。这里是代码 sn-p:
class DetailsScreen extends React.Component {
constructor(props) {
super(props);
this.state = { latitude: null,
longitude: null,
error: null,
};
}
componentDidMount() {
Geolocation.getCurrentPosition(
(position) => {
this.setState({
latitude: position.coords.latitude,
longitude: position.coords.longitude,
error: null,
});
},
(error) => this.setState({ error: error.message }),
{ enableHighAccuracy: false, timeout: 30000, maximumAge: 10000 },
);
}
render(){
return(
<View style = {styles.container}>
<Text style = {styles.boldText}>
Latitude:
</Text>
<Text>
{this.state.latitude}
</Text>
<Text style = {styles.boldText}>
Longitude:
</Text>
<Text>
{this.state.longitude}
</Text>
</View>
)
}
}
3) 使用的 navigator.geolocation.watchCurrentPosition() 仍然没有获取 lat/long 值。
我确实继续添加了
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
在 android 模块的 manifest.xml 文件中。那么我想还有更多的 android 特定权限/更改以便获取 lat/long 值吗?请帮助。
【问题讨论】:
标签: android react-native geolocation react-native-android geocoder