9

I've been playing around with React Native, getting custom locations to work and setting the "NSLocationWhenInUseUsageDescription" key. The error, when running on the ios simulator, is this:

{
 "code": 2,
 "message": "Unable to retrieve location.",
 "PERMISSION_DENIED": 1,
 "POSITION_UNAVAILABLE": 2,
 "TIMEOUT": 3 
}

This is what I have, pretty much straight from the Geolocation example page https://facebook.github.io/react-native/docs/geolocation.html

/* eslint no-console: 0 */
'use strict';

var React = require('react');
var ReactNative = require('react-native');
var {
  StyleSheet,
  Text,
  View,
} = ReactNative;

export default class GeolocationExample extends React.Component {
  state = {
    initialPosition: 'unknown'
  };

  componentDidMount() {
    navigator.geolocation.getCurrentPosition(
      (position) => {
        var initialPosition = JSON.stringify(position);
        this.setState({initialPosition});
      },
      (error) => alert(JSON.stringify(error)),
      {enableHighAccuracy: true, timeout: 20000, maximumAge: 1000}
    );
  }

  render() {
    return (
      <View>
        <Text>
          <Text style={styles.title}>Initial position: </Text>
          {this.state.initialPosition}
        </Text>
      </View>
    );
  }
}

var styles = StyleSheet.create({
  title: {
    fontWeight: '500',
  },
});

Any help would be appreciated!

Peter Qiu
  • 932
  • 2
  • 7
  • 13

8 Answers8

20

You need to set the location in the Simulator. There is an option for that in simulator menus as described in this question: Set the location in iPhone Simulator

Community
  • 1
  • 1
Santosh Sharma
  • 2,114
  • 1
  • 17
  • 28
  • Thank you! So the simulator can't get your current location? You have to put in a custom one in the Debug menu? – Peter Qiu Jan 09 '17 at 07:03
10

In Simulator navigator, Choose Debug, at bottom choose Location, next choose Apple, then CMD+R to reload and it worked. enter image description here

Tuan Nguyen
  • 2,542
  • 19
  • 29
4

As Santosh suggested, you need to set the location in the iOS Simulator: Debug > Location.

Keep in my mind that from time to time, you will get the "PERMISSION_DENIED " error even tho you set the location in the simulator. If this is happening, you need to select a different predefined location and then the error goes away.

1

Adding an update to Santosh's answer, please note that in my current version of simulator (11.4) the option to set location is found in:

Features -> Location

(Posting this as a new answer since I am not allowed to comment yet).

1

I was testing on a real device and it was giving the same error. I just restarted my device and it started working. Anyone who is still facing the issue can give it a try.

Muhammad Amir
  • 593
  • 2
  • 7
  • 21
0

So, for those who are still looking for the answer in Physical device

if you're using

import Geolocation from '@react-native-community/geolocation';

then

 Geolocation.getCurrentPosition(
                info => {console.log(info)},error=>{console.log(error)}});

if you don't handle the error it will throw error or make your app crash.

Atul Raj
  • 234
  • 5
  • 13
0

In my case

I'm using

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

so just used

const requestCameraPermission = async () => {
if (Platform.OS === 'android') {
  try {
    const granted = await PermissionsAndroid.request(
      PermissionsAndroid.PERMISSIONS.CAMERA,
      {
        title: `Allow Yarrow to access this devices's location.`,
        message: '',
        buttonNeutral: 'Ask Me Later',
        buttonNegative: 'Cancel',
        buttonPositive: 'OK',
      },
    );
    if (granted === PermissionsAndroid.RESULTS.GRANTED) {
      console.log('You can use the location');
    } else {
      console.log('location permission denied');
    }
  } catch (err) {
    console.warn(err);
  }
} else {
  const auth = await Geolocation.requestAuthorization('whenInUse');
  setIosLocation(auth);
  console.log(auth);
}
};


 useEffect(() => {
   requestCameraPermission();
 }, []);
 
  const locationGet = async () => {
setLoading(true);
if (Platform.OS === 'ios') {
  if (iosLocation !== 'granted') {
    return Alert.alert(
      'Alert!',
      `We're unable to connect to your location. Please provide the location access.`,
      [{text: 'Ok'}],
      {cancelable: true},
    );
  }
}

Geolocation.getCurrentPosition(
  position => {
    console.log(position);
    setTimeout(() => {
      setLoading(false);
    }, 1000);
  },
  error => {
    // See error code charts below.
    console.log('error in location :->> ', error.code, error.message);
  },
  {enableHighAccuracy: true, timeout: 15000, maximumAge: 10000},
);
};
Deepak Singh
  • 749
  • 4
  • 16
-1

Now it's iOS Simulator: Features > Location.Add location in simulator

Saurabh
  • 331
  • 4
  • 12