我正在尝试使用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的依赖项部分中所做的更改。的java
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手机开始显示经度和纬度而不会超时。我不确定这个错误来自哪里。错误未显示任何行号。
任何帮助将不胜感激
答案 0 :(得分:0)
同样的问题,但是当我尝试自动链接时,我在该包的Android Studio中收到了警告
com.agontuk.RNFusedLocation
已弃用
所以我认为react-native-geolocation-service是越野车