React native navigator.geolocation::位置请求超时问题反应本机
我正在构建一个React本机应用程序,并尝试使用navigator.geolocation获取我当前的地理代码。它显示了一个位置请求超时错误。基本上,我正在建立一个跟踪应用程序,我需要配置准确的位置纬度经度。要开始跟踪,有一个按钮,单击此跟踪将是开始,它应该是跟踪的初始点。我使用的是默认的本地地理定位API,Link 函数获取我的当前位置::React native navigator.geolocation::位置请求超时问题反应本机,react-native,geolocation,React Native,Geolocation,我正在构建一个React本机应用程序,并尝试使用navigator.geolocation获取我当前的地理代码。它显示了一个位置请求超时错误。基本上,我正在建立一个跟踪应用程序,我需要配置准确的位置纬度经度。要开始跟踪,有一个按钮,单击此跟踪将是开始,它应该是跟踪的初始点。我使用的是默认的本地地理定位API,Link 函数获取我的当前位置:: getLocation() { navigator.geolocation.getCurrentPosition(response => {
getLocation() {
navigator.geolocation.getCurrentPosition(response => {
console.log(response);
if (response && response.coords) {
this.saveLogs(response.coords);
}
},
(error) => {
this.onGeolocationErrorOccurCallback(error);
},
GeolocationOptions
)
}
它向我显示了一个错误::
{“超时”:3,“位置不可用”:2,“权限被拒绝”:1,“消息”:“位置”
请求超时,“代码”:3}
我已经为Android设备添加了权限,并通过将enableHighAccurance值设置为false进行了检查。但这对我不起作用。我需要将EnableHighAccurance设置为true,因为我需要精确的地理代码来跟踪某人。请说明此处发生位置请求超时的原因???以下代码适用于我:
componentDidMount(){
if(Platform.OS === 'android'){
this.requestLocationPermission()
}else{
this._getCurrentLocation()
}
}
async requestLocationPermission() {
try {
const granted = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
{
'title': 'Location Permission',
'message': 'MyMapApp needs access to your location'
}
)
if (granted === PermissionsAndroid.RESULTS.GRANTED) {
this._getCurrentLocation()
console.log("Location permission granted")
} else {
console.log("Location permission denied")
}
} catch (err) {
console.warn(err)
}
}
_getCurrentLocation = () =>{
navigator.geolocation.getCurrentPosition(
(position) => {
this.setState({
latitude: position.coords.latitude,
longitude: position.coords.longitude,
})
},
(error) => {
this.setState({ error: error.message })},
{ enableHighAccuracy: true, timeout: 200000, maximumAge: 1000 },
);
}
清单文件中的权限:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
只需确保您的位置已启用。似乎@react native community/geolocation无法正常工作 您可以(在官方的GitHub页面上)获得未来的答案 但现在,您可以毫无问题地使用。尝试使用: EnableHighAccurance:错误 为我工作
const getUserCurrentLocation = () => {
let latitude, longitude
Geolocation.getCurrentPosition(
info => {
const { coords } = info
latitude = coords.latitude
longitude = coords.longitude
setLat(latitude)
setLng(longitude)
getUserCurrentAddress(latitude, longitude)
},
error => console.log(error),
{
enableHighAccuracy: false,
timeout: 2000,
maximumAge: 3600000
}
)
}
更新:2021年6月12日
这种方法适合我在Android Emulator中使用<代码>“react native”:“0.64.1”和Android API 30
import Geolocation from "@react-native-community/geolocation";
const getGeoLocaation = () => {
const config = {
enableHighAccuracy: true,
timeout: 2000,
maximumAge: 3600000,
};
Geolocation.getCurrentPosition(
info => console.log("INFO", info),
error => console.log("ERROR", error),
config,
);
};
我已经试过了,但它不适用于Redmi note 4 | Android 7,MIUI 9 |它适用于任何其他设备吗@HarleenKauraRorao只有moto E-4 plus,但我需要一个在所有设备上都能工作的软件。它在ios设备上工作,但大多数Android设备都有问题。我已经设置了权限,并在授予权限时调用了上述函数。您能告诉我如何解决Android设备的问题吗