android - react native 地理位置getCurrentPosition无响应(android)

标签 android react-native geolocation

我已经阅读并测试了很多问题,但我仍然无法在 Android 上获得地理位置。

我使用 navigator.geolocation.getCurrentPosition,在 iOS 上一切正常,但在 Android 上,我对这个函数没有答案,无论是成功还是错误。

我已经安装了 react-native-permissions 以确保用户已经激活了权限,但它没有改变任何东西,因为它说一切都是“授权的”。

我注意到它来自设备的 GPS。如果我手动激活它,一切正常。但是,我找不到为用户激活它的方法。在 iOS 上,如果 GPS 未激活,我会陷入错误回调并告诉用户激活它,但在 android 上,什么都没有发生。

我不明白为什么我不能仅使用 getCurrentPosition 获得地理位置(我在 list 中有 ACCESS_COARSE_LOCATION 和 ACCESS_FINE_LOCATION)。

这是我的代码的一部分:

componentDidMount() {

navigator.geolocation.getCurrentPosition(
  (position) => {
    //do my stuff with position value
  },
  (error) => {
    Permissions.getPermissionStatus('location')
    .then((response) => {
      if (response !== "authorized") {
        Alert.alert(
          "Error",
          "We need to access to your location",
          [
            {
              text: "Cancel",
              onPress: () => {
                // do my stuff
              }, style: 'cancel'
            },
            {
              text: "Open Settings",
              onPress: () => Permissions.openSettings()
            }
          ]
        );
      } else {
        // do my stuff
      }
    });
  },
  { enableHighAccuracy: true, timeout: 2000, maximumAge: 1000 }
);

}

有人有什么主意吗 ?

谢谢

最佳答案

您应该需要启用 全局定位系统 在安卓上

为了在 Android 上启用位置/gps,我可以推荐这个模块:

https://github.com/Richou/react-native-android-location-enabler

它使用标准的 Android 对话框定位:

像这样

import React, { Component } from "react";
import { Text, StyleSheet, View, Platform } from "react-native";
import RNAndroidLocationEnabler from "react-native-android-location-enabler";

export default class index extends Component {
  componentDidMount() {
    this.getLocation();
  }

  onLocationEnablePressed = () => {
    if (Platform.OS === "android") {
      RNAndroidLocationEnabler.promptForEnableLocationIfNeeded({
        interval: 10000,
        fastInterval: 5000,
      })
        .then((data) => {
          this.getLocation();
        })
        .catch((err) => {
          alert("Error " + err.message + ", Code : " + err.code);
        });
    }
  };

  getLocation = () => {
    try {
      navigator.geolocation.getCurrentPosition(
        (position) => {
          //do my stuff with position value
        },
        (error) => {
          Permissions.getPermissionStatus("location").then((response) => {
            if (response !== "authorized") {
              Alert.alert("Error", "We need to access to your location", [
                {
                  text: "Cancel",
                  onPress: () => {
                    // do my stuff
                  },
                  style: "cancel",
                },
                {
                  text: "Open Settings",
                  onPress: () => Permissions.openSettings(),
                },
              ]);
            } else {
              // do my stuff
            }
          });
        },
        { enableHighAccuracy: true, timeout: 2000, maximumAge: 1000 }
      );
    } catch (error) {
      this.onLocationEnablePressed();
    }
  };


}


关于android - react native 地理位置getCurrentPosition无响应(android),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44586560/

相关文章:

android - 编译时验证失败(Account Kit Android)

react-native - react 导航 5.0 标题按钮

React-Native iOS应用构建失败-打印: Entry, ":CFBundleIdentifier",不存在

javascript - JSON 获取地理位置未在 HTML 中返回值

mongodb - Mongodb 复合索引与地理定位不起作用

geolocation - 通过IP地址识别国家

android - 将 SD 卡中的 ImageView 设置为 match_parent 高度?

java - 从另一个类获取按钮调用

android - OpenCV 4.1.0 Android Sdk 找不到匹配的配置Android studio

javascript - 我可以将 Backbone 与 React Native 一起使用吗?