反应原生安卓地理位置不返回 - 死线程



在 React Native 中获取用户在 Android 上的位置时遇到问题我已经设置了正确的权限,并且正在获取该位置,但它没有返回到我的组件。在日志中看到以下内容

 W/MessageQueue: Handler (android.location.LocationManager$ListenerTransport$1) {6456d95} sending message to a Handler on a dead thread
                                                           java.lang.IllegalStateException: Handler (android.location.LocationManager$ListenerTransport$1) {6456d95} sending message to a Handler on a dead thread
                                                               at android.os.MessageQueue.enqueueMessage(MessageQueue.java:543)
                                                               at android.os.Handler.enqueueMessage(Handler.java:631)
                                                               at android.os.Handler.sendMessageAtTime(Handler.java:600)
                                                               at android.os.Handler.sendMessageDelayed(Handler.java:570)
                                                               at android.os.Handler.sendMessage(Handler.java:507)
                                                               at android.location.LocationManager$ListenerTransport.onLocationChanged(LocationManager.java:248)
                                                               at android.location.ILocationListener$Stub.onTransact(ILocationListener.java:58)
                                                               at android.os.Binder.execTransact(Binder.java:461)

使用 react-native 0.25.01, targetSdk 23

关于为什么会发生这种情况的任何想法?希望:)一些输入我的实现只是从文档(http://facebook.github.io/react-native/docs/geolocation.html#content)复制粘贴

我不确定这是否会对您有很大帮助,但我创建了一个适用于 Android(Genymotion 模拟器)的示例。

它运行在 react-native 0.26.3 上,你可以在 GitHub 上看到它 https://github.com/AidenMontgomery/react-native-sample

从未见过您遇到的实际错误,但我知道拥有一个工作示例通常可以帮助我解决代码问题。

为了后代的缘故,请使用以下 npm 包,这是在 ReactNative 0.60.4 中最后一次测试的。

import Geolocation from 'react-native-geolocation-service';

请务必请求位置服务的权限,这应该是:

    //Request location permission
    async componentWillMount(){
        try {
            await PermissionsAndroid.request(PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION);
        } catch (err) {
            console.warn(err);
        }
    }

定义用于拉取当前位置的异步函数。

 _mapLocation = async () => {
        await Geolocation.getCurrentPosition(
            position => {
                var x: LatLng = {
                    latitude: position.coords.latitude,
                    longitude: position.coords.longitude,
                };
                //Here I am collecting gps coordinates in a collection.
                var tempMarkers = this.state.markers;
                tempMarkers.push({latlng: x, title: '', description: ''});
                this.setState({markers: tempMarkers});
            },
            error => {
                console.warn(error.message);
            },
            { enableHighAccuracy: true, timeout: 20000, maximumAge: 10000 }
        )
    }

最后,下面是如何执行新创建的异步函数的示例:

<Button title='Map Location' type='solid' onPress={() => this._mapLocation()} />

最新更新