Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Permission denied - geolocation in React Native

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!

like image 693
Peter Qiu Avatar asked Jan 09 '17 01:01

Peter Qiu


People also ask

How do I get location permissions in react native?

Geolocation is enabled by default when you create a project with react-native init . In order to enable geolocation in the background, you need to include the 'NSLocationAlwaysUsageDescription' key in Info. plist and add location as a background mode in the 'Capabilities' tab in Xcode.

What does User denied geolocation mean?

"User denied Geolocation" If you receive this error, it means your device supports geolocation but your mobile browser is unable to access it. To fix this, you need to have two things right: Your mobile device's location services (GPS) needs to be enabled.


2 Answers

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

like image 170
Santosh Sharma Avatar answered Oct 24 '22 07:10

Santosh Sharma


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

like image 10
Tuan Nguyen Avatar answered Oct 24 '22 06:10

Tuan Nguyen