gpt4 book ai didi

android - react-native-maps 获取当前位置

转载 作者:行者123 更新时间:2023-12-04 04:20:34 27 4
gpt4 key购买 nike

我想显示我的当前位置,但我不能。

我将权限代码添加到 AndroidManifest.xml,如下所示。

    <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" />

但是我的模拟器没有询问我“允许 00 在您使用该应用程序时访问您的位置”。

下面是我的 app.js 代码。

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

export default class App extends Component {
state = {
location: null
};

findCoordinates = () => {
navigator.geolocation.getCurrentPosition(
position => {
const location = JSON.stringify(position);

this.setState({ location });
},
error => Alert.alert(error.message),
{ enableHighAccuracy: true, timeout: 20000, maximumAge: 1000 }
);
};

render() {
return (
<View style={styles.container}>
<TouchableOpacity onPress={this.findCoordinates}>
<Text style={styles.welcome}>Find My Coords?</Text>
<Text>Location: {this.state.location}</Text>
</TouchableOpacity>
</View>
);
}
}

const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF'
},
welcome: {
fontSize: 20,
textAlign: 'center',
margin: 10
}
});

最佳答案

我使用波纹管代码解决了这些问题。

async function requestGeolocationPermission() {
try {
const granted = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
{
'title': 'Ridesharer Geolocation Permission',
'message': 'Ridesharer needs access to your current location so you can share or search for a ride'
});
if (granted === PermissionsAndroid.RESULTS.GRANTED) {
console.log("You can use the geolocation")
} else {
console.log("Geolocation permission denied")
}
} catch (err) {
console.warn(err)
}
}
requestGeolocationPermission();

关于android - react-native-maps 获取当前位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59462259/

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