简体   繁体   English

为什么地理位置无法在带有React Native的Android模拟器上运行?

[英]Why does geolocation not working on Android Emulator with React Native?

I'm trying to get the current user location in Android with React Native. 我正在尝试使用React Native在Android中获取当前用户位置。 I have written the following snippet in the App.js file: 我在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 ...
});

Since I use an Android emulator, I have also added the following line in the AndroidManifest.xml file located in android\\app\\src\\main , just above the application tag: 由于我使用的是Android模拟器,因此我还在android\\app\\src\\main中的AndroidManifest.xml文件中的application标记上方添加了以下行:

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

When I run it, no request for sharing the GPS location pops up and also the getCurrentPosition function seems to be just skipped. 当我运行它时,没有出现共享GPS位置的请求,而且getCurrentPosition函数似乎只是被跳过了。 The state is not updated, no error is displayed and the alert "SKIPPED" appears. 状态未更新,不显示任何错误,并且显示警报“ SKIPPED”。

That is what I see on the screen after the "SKIPPED" alert is dismissed: 这是我在取消“跳过”警报后在屏幕上看到的内容:

在此处输入图片说明

What am I missing here? 我在这里想念什么?

IOS IOS

If you are using Simulator for iOS, go to Simulator's menu bar and select Debug ‣ Location ‣ Custom Location. 如果您使用的是Simulator for iOS,请转到Simulator的菜单栏,然后​​选择“调试‣位置‣自定义位置”。 Enter A Coord for latitude and longitude. 输入经度和纬度坐标。

Android Android的

If you are using an Android virtual device, click the three dots, representing Extended controls, on the toolbar in the emulator. 如果使用的是Android虚拟设备,请在仿真器的工具栏上单击代表扩展控件的三个点。 In the Location section, Enter A Coord for latitude and longitude. 在位置部分中,输入经度和纬度坐标。

Apparently I had to restart my emulator and then it started working correctly. 显然,我必须重新启动模拟器,然后它才能正常工作。 Found a hint here . 在这里找到了提示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM