使用地理定位反应本机时出错
Error when using geolocation react-native
我正在使用地理定位,但出现错误:“undefined is not an object(evaluating 'navigator.geolocation.getCurrentPosition')
根据文档,没有必要使用 react native 0.60 或更高版本导入 Geolocation。
版本:
反应本机 cli:2.0.1
本机反应:0.60.4
import React, { Component } from "react";
import {
StyleSheet,
Text,
View,
Alert,
TouchableOpacity
} from "react-native";
export default class App extends Component {
state = {
location: null
};
findCoordinates = () => {
navigator.geolocation.getCurrentPosition(
position => {
const location = JSON.stringify(position);
this.setState({ location });
},
error => Alert.alert(error.message),
{ enableHighAccuracy: true, timeout: 20000, maximumAge: 1000 }
);
};
render() {
return (
<View style={styles.container}>
<TouchableOpacity onPress={this.findCoordinates}>
<Text style={styles.text}>Find My Coords?</Text>
<Text>Location: {this.state.location}</Text>
</TouchableOpacity>
</View>
);
}
}
我遇到了同样的问题,我认为从 0.60 及更高版本 navigator
开始,它不受支持,因此您必须使用 Geolocation
我正在使用地理定位,但出现错误:“undefined is not an object(evaluating 'navigator.geolocation.getCurrentPosition') 根据文档,没有必要使用 react native 0.60 或更高版本导入 Geolocation。
版本: 反应本机 cli:2.0.1 本机反应:0.60.4
import React, { Component } from "react";
import {
StyleSheet,
Text,
View,
Alert,
TouchableOpacity
} from "react-native";
export default class App extends Component {
state = {
location: null
};
findCoordinates = () => {
navigator.geolocation.getCurrentPosition(
position => {
const location = JSON.stringify(position);
this.setState({ location });
},
error => Alert.alert(error.message),
{ enableHighAccuracy: true, timeout: 20000, maximumAge: 1000 }
);
};
render() {
return (
<View style={styles.container}>
<TouchableOpacity onPress={this.findCoordinates}>
<Text style={styles.text}>Find My Coords?</Text>
<Text>Location: {this.state.location}</Text>
</TouchableOpacity>
</View>
);
}
}
我遇到了同样的问题,我认为从 0.60 及更高版本 navigator
开始,它不受支持,因此您必须使用 Geolocation