【问题标题】:React Native GeoLocation is not working on AndroidReact Native GeoLocation 无法在 Android 上运行
【发布时间】:2017-03-11 07:57:02
【问题描述】:

谁能确认 React Native Geolocation 是否真的在 Android 上运行? 我在使用 getCurrentPosition() 方法时遇到位置请求超时,而在使用 watchPosition() 方法时没有错误消息。

我已在 AndroidManifest.xml 上添加了所需的权限(FINE LOCATION),并且允许该应用使用位置权限。

这是我的实现:

componentWillMount() {
 navigator.geolocation.getCurrentPosition((position) => {
        console.log('+++++++');
        console.log(position.coords.longitude);
        this.setState({
          longitude: position.coords.longitude,
          error: null,
        });
      },
      (error) => {
        console.log(error);

      },

    );
}

React Native 版本:0.42

【问题讨论】:

    标签: android react-native


    【解决方案1】:

    您需要将 enableHighAccuracy 更改为 false

    const LATITUDE_DELTA = 0.0922;
    const LONGITUDE_DELTA = LATITUDE_DELTA * ASPECT_RATIO; 
    
    this.state = {
            region:{}}
    
     navigator.geolocation.getCurrentPosition(
                    (position) => {
                        console.log(position);
                        this.setState({
                            region: {
                                longitude: position.coords.longitude,
                                latitude: position.coords.latitude,
                                latitudeDelta: LATITUDE_DELTA,
                                longitudeDelta: LONGITUDE_DELTA
                            }
                        });
                    },
                    (error) => console.log(new Date(), error),
                    {enableHighAccuracy: false, timeout: 10000, maximumAge: 3000}
                );
    

    【讨论】:

    • 谢谢!为我工作。
    • 感谢您的帮助:)
    • react 不再支持这一点
    • 我同意 enableHighAccuracy: false 它仍在工作,谢谢!
    【解决方案2】:

    实施上述所有答案后 我必须重新启动我的新手机,然后它才能开始工作

    【讨论】:

      【解决方案3】:

      是的,位置功能在我们的 Android 版 React Native 应用中完美运行。

      首先,getCurrentPosition(..) 不返回 watchId。您应该为此使用watchPosition(..)。检查您的设备是否启用了定位服务,如果您在模拟器中对其进行测试,您是否已在模拟器设置中设置了位置。另请注意:在 Android M 及更高版本上,您还需要使用 PermissionsAndroid.check(...) 和 PermissionsAndroid.request(...) API 调用请求权限。

      【讨论】:

      • 对不起。 watchId 是个错误。我刚刚更新了代码。我正在真实设备上运行该应用程序。我正在使用react-native-permissions 处理权限问题。定位服务已启用。我已经手动授予该应用程序的位置权限以进行测试。 @david-xu
      • 您能提供适合您的代码的 sn-ps 吗? @david-xu
      【解决方案4】:

      不引用position 对象,只处理coords 对象。

       navigator.geolocation.getCurrentPosition(
            ({coords}) => {
              const {latitude, longitude} = coords
              this.setState({
                position: {
                  latitude,
                  longitude,
                },
                region: {
                  latitude,
                  longitude,
                  latitudeDelta: 0.001,
                  longitudeDelta: 0.001,
                }
              })
            },
            (error) => alert(JSON.stringify(error)),
            // 'enableHighAccuracy' sometimes causes problems
            // If it does, just remove it.
            {enableHighAccuracy: true} 
          )
      

      【讨论】:

      • 不工作。我不知道如何,但从昨天开始,它开始在我的设备上运行,但尚未在我同事的设备上运行。该死的
      • 你试过消除{enableHighAccuracy: true}吗?
      • 另外,您使用的是同一个 Google Maps API 密钥吗?这可能会导致问题。
      • 我根本不使用地图。我现在坚持获取经度和纬度。是的,我确实消除了 {enableHighAccuracy: true}
      • 如果您将 android 设备中的位置设置从高精度调整为低精度会怎样?
      【解决方案5】:

      我遇到了类似的问题,因为我在建筑物中,导致我的设备无法正确捕获 gps 信号。如果您在封闭的建筑物中,请尝试移到外面。

      【讨论】:

        【解决方案6】:

        我建议获得完美的位置数据是创建 2 个位置处理程序。

        我使用react-native-location 作为第一个处理程序,

        如果无法获取位置,我们使用本地地理定位的第二个处理程序。

        永不失败,兼容性:

        • IOS - 所有版本
        • ANDROID - 全部高于 SDK 20

        查看以下示例:

        配置

        RNLocation.configure({
             distanceFilter: 0.5,
             desiredAccuracy: {
                 ios: "best",
                 android: "highAccuracy"
             },
             headingOrientation: "portrait",
             // Android ONLY
             androidProvider: "auto",
             interval: 5000, // Milliseconds
             fastestInterval: 10000, // Milliseconds
             maxWaitTime: 5000, // Milliseconds
             // IOS ONLY
             allowsBackgroundLocationUpdates: false,
             headingFilter: 1, // Degrees
             pausesLocationUpdatesAutomatically: false,
             showsBackgroundLocationIndicator: false,
        })
        

        函数getUserLocation

        static getUserLocation = async () => {
            let locationSubscription: any = null
            let locationTimeout: any = null
            const options = { enableHighAccuracy: true, timeout: 25000 }
            return new Promise<any>((resolve, reject) => {
                // request permissions using RN Location
                RNLocation.requestPermission({
                    ios: "whenInUse",
                    android: {
                      detail: "fine"
                    }
                }).then(granted => {
                    console.log("Location Permissions: ", granted)
                    // if has permissions try to obtain location with RN location
                    if (granted) {
                        locationSubscription && locationSubscription()
                        locationSubscription = RNLocation.subscribeToLocationUpdates(([locations]: any) => {
                            locationSubscription()
                            locationTimeout && clearTimeout(locationTimeout)
                            console.log("location fetched with RN Geolocation")
                            resolve({ coords: { latitude: locations.latitude, longitude: locations.longitude }})
                        })
                    } else {
                        locationSubscription && locationSubscription()
                        locationTimeout && clearTimeout(locationTimeout)
                        console.log("no permissions to obtain location")
                        resolve(null)
                    }
        
                    // if RN Location cant resolve the request use Native Location instead
                    locationTimeout = setTimeout(() => {
                        navigator.geolocation.getCurrentPosition((locations) => {
                            locationSubscription()
                            locationTimeout && clearTimeout(locationTimeout)
                            console.log("location fetched with Native Geolocation")
                            resolve(locations)
                        }, error => {
                            locationSubscription && locationSubscription()
                            locationTimeout && clearTimeout(locationTimeout)
                            console.log("location error", error)
                            resolve(null)
                        }, options);
                    }, 15000)
                })
            })
        }
        

        【讨论】:

          【解决方案7】:

          cd 安卓 毕业清洁

          依赖 “反应原生”:“0.62.0”, "@react-native-community/geolocation": "^2.0.2",

          【讨论】:

            【解决方案8】:

            如果你试试这个代码会更好,我已经在 Android OS 8, RN 0.49.5 中测试过 React Native current location 诀窍是设置

            { enableHighAccuracy: true, timeout: 25000, maximumAge: 3600000 }

            【讨论】:

            • 好吧,现在它不适用于 android RN 0.55.4,react 16.4,
            猜你喜欢
            • 2016-03-30
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2020-11-16
            • 2020-05-16
            • 1970-01-01
            • 1970-01-01
            • 2021-05-10
            相关资源
            最近更新 更多