React Native GeoLocation 不适用于 Android [英] React Native GeoLocation is not working on Android

查看:68
本文介绍了React Native GeoLocation 不适用于 Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Can anyone confirm if React Native Geolocation is actually working on Android? I am stuck with location request timed out while using getCurrentPosition() method and no error message while using watchPosition() method.

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

I have added the required permission (FINE LOCATION) on AndroidManifest.xml and location permission is allowed for the app.

这是我的实现:

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

      },

    );
}

反应原生版本:0.42

React Native Version : 0.42

推荐答案

你需要把enableHighAccuracy改为false

you need to change enableHighAccuracy to 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 Native GeoLocation 不适用于 Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆