gpt4 book ai didi

react-native - react native : Cannot access location using geolocation API

转载 作者:行者123 更新时间:2023-12-04 16:04:40 25 4
gpt4 key购买 nike

Not actual screenshot of the code. But error is same
错误:该应用似乎无权访问位置信息。将以下行添加到您应用的 AndroidManifest.xml

即使我在 manifest.xml 中添加了权限并要求运行时权限,我也无法访问 GPS。

我的 AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.location">

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
.....
.....

完整代码:

import React, { Component } from 'react';
import { Platform, StyleSheet, Text, View,
PermissionsAndroid } from 'react-native';

export default class App extends Component {
async requestLocationPermission() {
try {
const granted = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
{
'title': 'Location Access Permission',
'message': 'Expensify would like to use your location ' +
'so you we track you.'
}
)
if (granted === PermissionsAndroid.RESULTS.GRANTED) {
console.log("You can use the location")
} else {
console.log("Location permission denied")
}
} catch (err) {
console.warn(err)
}
}
componentDidMount() {
console.log("compdidmount");
this.requestLocationPermission();
//Crashes here on getCurrentPosition
navigator.geolocation.getCurrentPosition(
(position) => {
console.log(position.coords.latitude);
},
(error) => {
console.log(error)
},
{enableHighAccuracy: true, timeout: 20000, maximumAge: 10000}
);
}
render() {
return (
<View style>
<Text style>
Tracking App
</Text>
</View>
);
}
}

最佳答案

我在 CameraRoll 上遇到了同样的问题.我修复了这个在应用程序启动时请求许可的问题。你应该使用这个:

import { PermissionsAndroid } from 'react-native';

async function requestCameraPermission() {
try {
const granted = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
{
'title': 'Location Permission',
'message': 'Location Permission' +
'so you can use GPS location.'
}
)
if (granted === PermissionsAndroid.RESULTS.GRANTED) {
console.log("You can use GPS")
} else {
console.log("GPS location denied")
}
} catch (err) {
console.warn(err)
}
}

关于react-native - react native : Cannot access location using geolocation API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50948412/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com