反应本机 GeoLocation 空引用错误
react native GeoLocation null reference error
我正在使用以下代码尝试获取我的当前位置:
componentDidMount() {
this.watchID = navigator.geolocation.watchPosition((position) => {
let region = {
latitude: position.coords.latitude,
longitude: position.coords.longitude,
latitudeDelta: 0.00922*1.5,
longitudeDelta: 0.00421*1.5,
enableHighAccuracy: true
}
this.onRegionChange(region, region.latitude, region.longitude);
});
}
我得到:
Attempt to invoke interface method 'boolean abi26_0_0.com.facebook.react.bridge.ReadableMap.hasKey(java.lang.String)' on a null object reference
在这条线上触发:
this.watchID = navigator.geolocation.watchPosition((position) => {
我是否需要导入一些东西才能让它工作?我在网上找不到任何关于此的信息。
经过几个小时的搜索,我终于搞定了。
如果您像我一样使用 CRNA(create-react-native-app 是您构建应用程序的方式),则需要完成函数调用的所有部分。这些是(成功、错误、选项)。下面的代码是如何完成所有这些部分并消除此错误。
componentDidMount() {
this.watchId = navigator.geolocation.watchPosition(
(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, distanceFilter: 10 },
);
}
我正在使用以下代码尝试获取我的当前位置:
componentDidMount() {
this.watchID = navigator.geolocation.watchPosition((position) => {
let region = {
latitude: position.coords.latitude,
longitude: position.coords.longitude,
latitudeDelta: 0.00922*1.5,
longitudeDelta: 0.00421*1.5,
enableHighAccuracy: true
}
this.onRegionChange(region, region.latitude, region.longitude);
});
}
我得到:
Attempt to invoke interface method 'boolean abi26_0_0.com.facebook.react.bridge.ReadableMap.hasKey(java.lang.String)' on a null object reference
在这条线上触发:
this.watchID = navigator.geolocation.watchPosition((position) => {
我是否需要导入一些东西才能让它工作?我在网上找不到任何关于此的信息。
经过几个小时的搜索,我终于搞定了。
如果您像我一样使用 CRNA(create-react-native-app 是您构建应用程序的方式),则需要完成函数调用的所有部分。这些是(成功、错误、选项)。下面的代码是如何完成所有这些部分并消除此错误。
componentDidMount() {
this.watchId = navigator.geolocation.watchPosition(
(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, distanceFilter: 10 },
);
}