0

When a button is clicked the updateLocation method is called but I'm getting an "coordinates" is null error.

Any ideas?

export class SharedLocationComponent {
  @Input() coordinates: CoordinateModel;

  updateLocation() {
    if (navigator.geolocation) {
      navigator.geolocation.getCurrentPosition(this.setCoordinates, function () {
        return true;
      });
    }
  }

  setCoordinates(coordinates) {
    let c = coordinates.coords
    if (c) {
      this.coordinates = new CoordinateModel(c.latitude, c.longitude);
    }


  }
}
John Churchley
  • 434
  • 4
  • 17
  • Possible duplicate of [How to access the correct \`this\` inside a callback?](https://stackoverflow.com/questions/20279484/how-to-access-the-correct-this-inside-a-callback) – ConnorsFan Oct 19 '18 at 14:09
  • Can you show the HTML where your component "SharedLocationComponent" is called ? – jug Oct 19 '18 at 14:10
  • 3
    Try: `navigator.geolocation.getCurrentPosition(this.setCoordinates.bind(this), ...` or define the callback as an arrow function: `setCoordinates = (coordinates) => { ... }`. – ConnorsFan Oct 19 '18 at 14:10
  • ^^^ fat arrow or that = this trick should sort ya. – Chris W. Oct 19 '18 at 14:16

0 Answers0