为什么地理定位无法在 Android Emulator with React Native 上运行?
Why does geolocation not working on Android Emulator with React Native?
我正在尝试使用 React Native 在 Android 中获取当前用户位置。我在 App.js 文件中写了以下片段:
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"。
这就是 "SKIPPED" 警报解除后我在屏幕上看到的内容:
我在这里错过了什么?
IOS
如果你正在为 iOS 使用模拟器,请转到模拟器的菜单栏和 select 调试 ‣ 位置 ‣ 自定义位置。输入纬度和经度的坐标。
Android
如果您使用的是 Android 虚拟设备,请单击模拟器工具栏上的三个点,代表扩展控件。在“位置”部分,输入纬度和经度坐标。
显然我不得不重新启动我的模拟器然后它开始正常工作。找到提示 here.
我正在尝试使用 React Native 在 Android 中获取当前用户位置。我在 App.js 文件中写了以下片段:
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"。
这就是 "SKIPPED" 警报解除后我在屏幕上看到的内容:
我在这里错过了什么?
IOS
如果你正在为 iOS 使用模拟器,请转到模拟器的菜单栏和 select 调试 ‣ 位置 ‣ 自定义位置。输入纬度和经度的坐标。
Android
如果您使用的是 Android 虚拟设备,请单击模拟器工具栏上的三个点,代表扩展控件。在“位置”部分,输入纬度和经度坐标。
显然我不得不重新启动我的模拟器然后它开始正常工作。找到提示 here.