首页 > 解决方案 > 反应原生地理定位服务不适用于 iOS

问题描述

我正在尝试在本机反应中获取设备当前位置。我正在使用下面的库来获取位置:

react-native-geolocation-service

我的代码:

try {
        const granted = await PermissionsAndroid.request(
          PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
          {
            title: 'Device current location permission',
            message:
              'Allow app to get your current location',
            buttonNeutral: 'Ask Me Later',
            buttonNegative: 'Cancel',
            buttonPositive: 'OK',
          },
        );
        if (granted === PermissionsAndroid.RESULTS.GRANTED) {
          this.getCurrentLocation();
        } else {
          console.log('Location permission denied');
        }
      } catch (err) {
        console.warn(err);
      }
     }

getCurrentLocation(){
     Geolocation.requestAuthorization();
     Geolocation.getCurrentPosition(
        (position) => {
            alert(position.coords.latitude);
            this.socket.emit('position', {
               data: position,
               id: this.id,
             });
        },
        (error) => {
          alert("map error: "+error.message);
            console.log(error.code, error.message);
        },
        { enableHighAccuracy: true, timeout: 15000, maximumAge: 10000 }
    );
   }

Android 工作正常并获得正确的位置,但对于 IOS,它不工作此外,它不要求允许位置权限。我收到以下错误:

PERMISSION_DENIED: 1
POSITION_UNAVAILABLE: 2
TIMEOUT: 3
code: 3
message: "Unable to fetch location within 15.0s."

这是我的 info.plist:

<key>NSLocationWhenInUseUsageDescription</key>
<string>$(PRODUCT_NAME) would like to access your location.</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>$(PRODUCT_NAME) would like to access your location.</string>

这是来自 xcode 的背景模式

在此处输入图像描述

标签: react-nativegeolocationreact-native-ios

解决方案


我已经使用 react-native-geolocation-service 来获取用户当前位置这是我的代码

应用程序.js:

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

async componentDidMount() {
 if(Platform.OS === 'ios'){
      this.getCurrentLocation();
    }else{
      try {
       const granted = await PermissionsAndroid.request(
         PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
         {
           title: 'Device current location permission',
           message:
             'Allow app to get your current location',
           buttonNeutral: 'Ask Me Later',
           buttonNegative: 'Cancel',
           buttonPositive: 'OK',
         },
       );
       if (granted === PermissionsAndroid.RESULTS.GRANTED) {
         this.getCurrentLocation();
       } else {
         console.log('Location permission denied');
       }
     } catch (err) {
       console.warn(err);
     }
    }
}

getCurrentLocation(){
    Geolocation.requestAuthorization();
    Geolocation.getCurrentPosition(
       (position) => {
           console.log(position);
       },
       (error) => {
         console.log("map error: ",error);
           console.log(error.code, error.message);
       },
       { enableHighAccuracy: false, timeout: 15000, maximumAge: 10000 }
   );
  }

MapScreen.js

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

componentDidMount() {
  this.updateCurrentPosiiton();
}

updateCurrentPosiiton () {
    Geolocation.getCurrentPosition(
       (position) => {
          let d_lat  =  position.coords.latitude;
          let d_lng  =  position.coords.longitude;

          this.setState({
            current_lat: position.coords.latitude,
            current_lng: position.coords.longitude
          })
          // console.log('aayaaa');
          this.props.updateGps(d_lat,d_lng);

          this.getTrackData(d_lat,d_lng);

       },
       (error) => {
         console.log("map error: ",error);
           console.log(error.code, error.message);
       },
       { enableHighAccuracy: false, timeout: 15000, maximumAge: 10000 }
   );
  }

如果有任何问题,请告诉我。


推荐阅读