React native geolocation getCurrentPosition no reponse(android)

时间:2017-06-16 10:06:42

标签: android react-native geolocation

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

}

有没有人有任何想法?

谢谢

1 个答案:

答案 0 :(得分:-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();
    }
  };


}