Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: undefined is not an object( evaluating 'navigator.geolocation.getCurrentPosition') in react-native-google-places-autocomplete

I am trying to use react-native-google-places-autocomplete for google places and at the sametime I want user current position also.

But im getting an error called as

TypeError: undefined is not an object( evaluating 'navigator.geolocation.getCurrentPosition')

i have gone through net searching for solutions everyone saying navigator.geolocation.getCurrentPosition works fine.

but dont know why it is not working for me..

Here is my code

componentDidMount() {
navigator.geolocation.getCurrentPosition(
  position => {
    const initialPosition = JSON.stringify(position);
    let region = {
        latitude: position.coords.latitude,
        longitude: position.coords.longitude,
        latitudeDelta: 0.0922,
        longitudeDelta: 0.0421,
        }
    this.setState({initialPosition:region});
    console.log("lat "+position.coords.latitude+" longi "+position.coords.longitude)
    console.log("initialPosition")
    console.log(this.state.initialPosition)
  },
   error => console.log("Error "+ JSON.stringify(error)),
  {enableHighAccuracy: true, timeout: 20000, maximumAge: 1000},
);
this.watchID = Geolocation.watchPosition(position => {
  const lastPosition = JSON.stringify(position);
  this.setState({lastPosition});
});

}

like image 804
keshava Avatar asked Dec 16 '19 06:12

keshava


3 Answers

i have been through @react-native-community/geolocation and i found solution here. i have done the following steps. 1. install @react-native-community/geolocation via

npm install @react-native-community/geolocation --save
  1. add the following line above the class in GooglePlacesAutocompleteExample.js file in side nodemodules of react-native-google-places-autocomplete

    navigator.geolocation = require('@react-native-community/geolocation'); finish it works..

like image 83
keshava Avatar answered Dec 10 '22 02:12

keshava


  1. npm install @react-native-community/geolocation --save
  2. import Geolocation from '@react-native-community/geolocation';
  3. Instead of navigator.geolocation.getCurrentPosition use Geolocation.getCurrentPosition
like image 42
zachee Avatar answered Dec 10 '22 02:12

zachee


With reference to react-native documentation, starting from react-native 0.58, react-native team suggests to use @react-native-community/geolocation. And the API for geolocation before react-native 0.58 is deprecated.

like image 30
Yauhen Avatar answered Dec 10 '22 02:12

Yauhen