反应本机地理定位 getCurrentPosition 无响应(android)

Mar*_*a.A 6 android geolocation react-native

我已经阅读并测试了很多问题,但我仍然无法在 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 }
);

}
Run Code Online (Sandbox Code Playgroud)

有谁有想法吗 ?

谢谢

Muh*_*man -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();
    }
  };


}


Run Code Online (Sandbox Code Playgroud)