【问题标题】:Why does geolocation not working on Android Emulator with React Native?为什么地理定位在带有 React Native 的 Android 模拟器上不起作用?
【发布时间】: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\mainAndroidManifest.xml 文件中添加了以下行,就在 application 标记上方:

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

当我运行它时,没有弹出共享 GPS 位置的请求,而且getCurrentPosition 功能似乎只是被跳过了。状态未更新,不显示错误并出现警告“SKIPPED”。

这是我在“跳过”警报被解除后在屏幕上看到的内容:

我在这里错过了什么?

【问题讨论】:

    标签: android react-native geolocation


    【解决方案1】:

    IOS

    如果您使用的是 iOS 版模拟器,请转到模拟器的菜单栏并选择调试 ‣ 位置 ‣ 自定义位置。输入经纬度坐标。

    安卓

    如果您使用的是 Android 虚拟设备,请单击模拟器工具栏上代表扩展控件的三个点。在“位置”部分,输入纬度和经度坐标。

    【讨论】:

    • 坐标已经在我的模拟器中设置好了。请看我的编辑。
    【解决方案2】:

    显然我必须重新启动我的模拟器,然后它才开始正常工作。找到提示here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多