我正在尝试使用 geolocation.getcurrentposition,但出现错误。以下是错误的屏幕截图:
下面是我的 App.Js 代码:
import React, { Component } from 'react';
import { View, Text } from 'react-native';
class GeolocationExample extends Component {
constructor(props) {
super(props);
this.state = {
latitude: null,
longitude: null,
error: null,
};
}
componentDidMount() {
// Instead of navigator.geolocation, just use Geolocation.
if (hasLocationPermission) {
Geolocation.getCurrentPosition(
(position) => {
console.log(position);
},
(error) => {
// See error code charts below.
console.log(error.code, error.message);
},
{ enableHighAccuracy: true, timeout: 15000, maximumAge: 10000 }
);
}
}
render() {
return (
<View style={{ flexGrow: 1, alignItems: 'center', justifyContent: 'center' }}>
<Text>Latitude: {this.state.latitude}</Text>
<Text>Longitude: {this.state.longitude}</Text>
{this.state.error ? <Text>Error: {this.state.error}</Text> : null}
</View>
);
}
}
export default GeolocationExample;
我还对 MainActivity.Java 文件进行了一些修改。以下是我在 MainActivity 的依赖项部分所做的更改。爪哇
import com.agontuk.RNFusedLocation.RNFusedLocationPackage;
@Override
protected List<ReactPackage> getPackages() {
return Arrays.<ReactPackage>asList(
// new MainReactPackage()
new RNFusedLocationPackage()
);
}
使用 getcUrrentposition 时,我的经度和纬度超时,这就是原因,我正在使用解决方法。此解决方法是安装 react-native-geolocation -service。我使用的 google play 服务版本是 12.2.21。下面是图片:
我还在 build.gradle 中添加了依赖项。我在 build.gradle 中插入了当前的 google play 服务版本
compile(project(':react-native-geolocation-service')) {
exclude group: 'com.google.android.gms', module: 'play-services-location'
}
compile 'com.google.android.gms:play-services-location:12.2.21'
当我试图获取用户当前位置的经度和纬度时,上面的代码超时。我进行了这些更改,以便我的 android 手机开始显示经度和纬度而不会超时。我不确定这个错误来自哪里。该错误未显示任何行号。
任何帮助将不胜感激