0

I am using the below code to try to get my current location:

componentDidMount() {
    this.watchID = navigator.geolocation.watchPosition((position) => {
        let region = {
            latitude:       position.coords.latitude,
            longitude:      position.coords.longitude,
            latitudeDelta:  0.00922*1.5,
            longitudeDelta: 0.00421*1.5,
            enableHighAccuracy: true
        }
        this.onRegionChange(region, region.latitude, region.longitude);
    });
}

And I am getting:

Attempt to invoke interface method 'boolean abi26_0_0.com.facebook.react.bridge.ReadableMap.hasKey(java.lang.String)' on a null object reference

Which is triggering on this line:

this.watchID = navigator.geolocation.watchPosition((position) => {

Is there something I need to import for this to work maybe? I can't find anything online about this.

3
  • Did you create your project using react-native init? If so, did you do all the required configurations? Commented Apr 12, 2018 at 15:32
  • I created the project using create-react-native-app. I am not sure how this makes the syntax of that particular line incorrect. Commented Apr 12, 2018 at 23:47
  • Not sure what else you guys need from me to get some help with this one. Would the entire class code help? Also @Michael Cheng, that guide you linked I have seen before but it lists iOS and Android development. I am using neither. Commented Apr 13, 2018 at 5:05

1 Answer 1

4

I finally nailed it after many hours of searching.

If you are using CRNA, like me (create-react-native-app was the way you built your app), you need to fulfill all parts of the function call. These are (success, error, options). The code below is how to fulfill all these parts and remove this error.

componentDidMount() {
this.watchId = navigator.geolocation.watchPosition(
  (position) => {
    this.setState({
      latitude: position.coords.latitude,
      longitude: position.coords.longitude,
      error: null,
    });
  },
  (error) => this.setState({ error: error.message }),
  { enableHighAccuracy: true, timeout: 20000, maximumAge: 1000, distanceFilter: 10 },
);
}
Sign up to request clarification or add additional context in comments.

2 Comments

More than one year later, this saved me! Thank you!! You should mark this answer as solved.
Hah, happy to have helped ;)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.