首页 > 解决方案 > 获取 null 不是对象(评估 'RNFusedLocation.getCurrentPosition')

问题描述

我正在使用 react-native-geolocation-service 库,我收到了这个错误。我的 RN 版本是 0.63.4

null 不是对象(评估'RNFusedLocation.getCurrentPosition')

这是帮助我获取当前位置的 helper.js:

import { PermissionsAndroid, Platform } from 'react-native';
import Geolocation from 'react-native-geolocation-service';

export const getCurrentLocation = () =>
    new Promise((resolve, reject) => {
        try{Geolocation.getCurrentPosition(
            position => {
                const cords = {
                    latitude: position.coords.latitude,
                    longitude: position.coords.longitude,
                };
                resolve(cords);
            },
            error => {
                reject(error.message);
            },
            { enableHighAccuracy: true, timeout: 15000, maximumAge: 10000 },
        )}catch(error){
            console.log(error)
        }
    })


export const locationPermission = () => new Promise(async (resolve, reject) => {
    if (Platform.OS === 'ios') {
        try {
            const permissionStatus = await Geolocation.requestAuthorization('whenInUse');
            if (permissionStatus === 'granted') {
                return resolve("granted")
            }
            reject("permission not granted");
        } catch (error) {
            return reject(error);
        }
    }
    return PermissionsAndroid.request(
        PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
    ).then((granted) => {
        if (granted === PermissionsAndroid.RESULTS.GRANTED) {
            resolve('granted');
        }
        return reject("Location Permission Denied");
    }).catch((error) => {
        console.log('Ask Location permission error:', error);
        return reject(error);
    })
})

我在这里使用这个功能:


    import { getCurrentLocation, locationPermission } from './helper'

    componentDidMount() {
        this.getLiveLocation();
    }

    getLiveLocation = async () => {
        const locPermissionDenied = await locationPermission()
        console.log(locPermissionDenied);
        if(locPermissionDenied=="granted"){
            const res = await getCurrentLocation()
            console.log("res -->",res)
        }
    }

标签: javascriptreactjsreact-nativegeolocation

解决方案


推荐阅读