我试图从他们的iPhone获取用户在React Native中的位置坐标,但是当我按照Facebook的地理位置设置时,它没有返回该用户数据的响应( https://facebook.github.io/react-native/docs/geolocation.html#content).我不
如果这是找到用户位置的正确方法,还是有更好的反应原生方式?
谢谢 – 非常感谢任何和所有的帮助!
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