我正在尝试从 iPhone 获取用户在 React Native 中的位置坐标,但是当我按照 Facebook 的地理位置设置进行操作时,它不会返回包含该用户数据的响应 ( https://facebook.github.io/react-native/docs/geolocation.html#content )。我不确定代码是否存在问题(我的 GeoLocation 组件的代码如下)。我还在通过 iOS 模拟器进行测试 - 所以不确定拉取地理定位是否只能通过实际设备工作,或者模拟器是否可以基于 wifi 使用您的位置。
如果这是查找用户位置的正确方法,或者在 native react 中是否有更好的方法?
谢谢 - 非常感谢任何和所有的帮助!
const React = require('react-native');
const {
StyleSheet,
Text,
View,
} = React;
exports.framework = 'React';
exports.title = 'Geolocation';
exports.description = 'Examples of using the Geolocation API.';
exports.examples = [
{
title: 'navigator.geolocation',
render: function(): ReactElement {
return <GeolocationExample />;
},
}
];
const GeolocationExample = React.createClass({
watchID: (null: ?number),
getInitialState: function() {
return {
initialPosition: 'Can\'t find',
lastPosition: 'Can\'t find',
};
},
componentDidMount: function() {
navigator.geolocation.getCurrentPosition(
(initialPosition) => this.setState({initialPosition}),
(error) => alert(error.message),
{enableHighAccuracy: true, timeout: 20000, maximumAge: 1000}
);
this.watchID = navigator.geolocation.watchPosition((lastPosition) => {
this.setState({lastPosition});
});
},
componentWillUnmount: function() {
navigator.geolocation.clearWatch(this.watchID);
},
render: function() {
return (
<View>
<Text>
<Text style={styles.title}>Initial position: </Text>
{JSON.stringify(this.state.initialPosition)}
</Text>
<Text>
<Text style={styles.title}>Current position: </Text>
{JSON.stringify(this.state.lastPosition)}
</Text>
</View>
);
}
});
const styles = StyleSheet.create({
title: {
fontWeight: '500',
},
});
module.exports = GeolocationExample;
最佳答案
您需要在模拟器中设置位置。模拟器菜单中有一个选项,如本问题所述:Set the location in iPhone Simulator
关于reactjs - 使用 React Native 检索地理位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32361632/