React本机地理位置服务不适用于iOS

时间:2019-08-20 09:37:25

标签: react-native geolocation react-native-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的后台模式

enter image description here

2 个答案:

答案 0 :(得分:1)

我已经解决了这个问题

async requestPermission() {
try {
  const granted = await PermissionsAndroid.requestMultiple([
    PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
    PermissionsAndroid.PERMISSIONS.ACCESS_COARSE_LOCATION,
  ]).then((result) => {
    console.log(result);
    if (
      result['android.permission.ACCESS_COARSE_LOCATION'] &&
      result['android.permission.ACCESS_FINE_LOCATION'] === 'granted'
    ) {
      this.getLocation();
      this.setState({
        permissionsGranted: true,
      });
    }
  });
} catch (err) {
  console.warn(err);
  }
}

 getLocation() {
Geolocation.getCurrentPosition(
  (position) => {
    console.log(position);
  },
  (error) => {
    // See error code charts below.
    console.log(error.code, error.message);
  },
  {enableHighAccuracy: true, timeout: 15000, maximumAge: 10000},
  );
}

 componentDidMount() {
if (Platform.OS === 'ios') {
  Geolocation.requestAuthorization('always').then((res) => {
    console.log(res);
  });
}
if (Platform.OS === 'android') {
  this.requestPermission();
} else {
  this.getLocation();
 }
}

确保您使用参数 Geolocation.requestAuthorization('always') Geolocation.requestAuthorization('whenInUse')调用了此函数

答案 1 :(得分:0)

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

App.js:

pipe

MapScreen.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 }
   );
  }

请让我知道是否有问题。