【问题标题】:React native geolocation getCurrentPosition no reponse (android)反应本机地理位置getCurrentPosition无响应(android)
【发布时间】:2017-11-19 01:44:24
【问题描述】:

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

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

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

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

我不明白为什么我不能仅使用 getCurrentPosition 获取地理位置(我在清单中有 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 }
);

}

有人知道吗?

谢谢

【问题讨论】:

  • 模拟器还是设备?设置坐标时,鸸鹋的地理位置似乎不起作用。我无法让他们通过我的 RN 应用程序。
  • 你找到解决办法了吗?
  • @Marina.A 你在 batiment 里面吗?如果是这种情况,GPS 定位可能需要超过 1 分钟(在 android 上)。

标签: android react-native geolocation


【解决方案1】:

您应该需要在 Android 上启用 GPS

为了在 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();
    }
  };


}


【讨论】:

  • 即使定位服务关闭,模块也会抛出异常,code = 2 显示service is unavailable。关键是模块根本没有显示任何东西,它没有成功,也没有失败。
  • navigator.geolocation.getCurrentPosition 取决于 GPS。您可以在 try-catch 异常处理和位置函数的递归调用中处理 GPS 启用
  • tnx 的答案,我试过了。不工作。关键是我认为它的 RN 本机代码错误。不知何故,相同的代码在其他项目中有效,但在这里不适用。
  • @Amas 但我们必须在 android 上以编程方式启用 GPS
  • @Amas 你能在 git repo 上分享演示项目吗?所以我可以看到你遇到了什么问题。也许你有另一个我没有遇到的错误
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-01-03
  • 1970-01-01
  • 1970-01-01
  • 2015-07-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多