【发布时间】:2019-02-05 10:01:26
【问题描述】:
我正在尝试使用 React Native 获取 Android 中的当前用户位置。我在 App.js 文件中写了如下 sn-p:
import React, { Component } from "react";
import {
Platform,
StyleSheet,
Text,
View,
Alert,
TouchableOpacity
} 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(error.message),
{ enableHighAccuracy: true, timeout: 20000, maximumAge: 1000 }
);
alert("SKIPPED");
};
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({
... some style ...
});
由于我使用的是 Android 模拟器,因此我还在位于 android\app\src\main 的 AndroidManifest.xml 文件中添加了以下行,就在 application 标记上方:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
当我运行它时,没有弹出共享 GPS 位置的请求,而且getCurrentPosition 功能似乎只是被跳过了。状态未更新,不显示错误并出现警告“SKIPPED”。
这是我在“跳过”警报被解除后在屏幕上看到的内容:
我在这里错过了什么?
【问题讨论】:
标签: android react-native geolocation