0

Here is the problem I have: I need to build an app that geolocates you and fetch the weather for your area. I have a problem with the geolocation that I can't solve.

I put everything in a "Place" object that I build with a constructor (I put here only the part that builds the URL of the Google map that will be displayed):

function Place() {

    this.mapURL = "#";

    this.setMapURL = function(location) {

        var lat = location.coords.latitude;
        var long = location.coords.longitude;

        this.mapURL = 'https://maps.googleapis.com/maps/api/staticmap?center=' + lat + ',' + long + '&zoom=12&size=300x300&sensor=false';
    };

    this.displayMap = function() {

        var map = new Image();
        map.src = this.mapURL;

        mapContainer.html('Retrieving your position...');
        mapContainer.html(map);
    };
}

In the main code, I create a Place object and then call "getCurrentPosition" on this object, which itself calls "setMapURL":

var myPos = new Place();
navigator.geolocation.getCurrentPosition(myPos.setMapURL, error, geoOptions);

NB: the "error" and "geoOptions" are defined elsewhere.

The problem I have is that the property "mapURL" of the "myPos" object doesn't get updated. Its value stays "#".

There might be something I miss here but I really can't find what it is.

If you have any idea, let me know!

Thanks!!

macadam
  • 31
  • 5
  • what is navigator, can you show us it's API ? – Rosmarine Popcorn May 12 '16 at 15:16
  • https://developer.mozilla.org/en-US/docs/Web/API/Geolocation/getCurrentPosition – dlopez May 12 '16 at 15:17
  • Your function is being invoked such that `this` is not what you expect it to be. – Pointy May 12 '16 at 15:17
  • Should be `myPos.setMapURL.bind(myPos)` when you pass it to the `getCurrentPosition()` function. – Pointy May 12 '16 at 15:18
  • Is there a canonical Q/A for this common question? –  May 12 '16 at 15:23
  • @squint I don't know of a particularly good one. It's tricky because there are many variations, and for people who are unfamiliar with the semantics of `this` and function calls it's weird and confusing and hard to see the generalization. – Pointy May 12 '16 at 15:24
  • Thanks all. @Pointy, you mean that the `this` in the `setMapURL` function doesn't refer to my original `myPos` object? – macadam May 12 '16 at 15:27
  • @Pointy: Yeah, that makes it tough to search. Still, a quick go-to answer would be nice. –  May 12 '16 at 15:28
  • @macadam: The value of `this` in a method/function is defined based on how it is invoked. So in your case, you're passing the method and then invoking it apart from the object from whence it came. So because there's no longer a direct object reference to be found at the call site, it substitutes `window` for the value of `this` *(except in 'strict mode', where it substitutes `undefined`)*. –  May 12 '16 at 15:37
  • @squint thanks, I think I understand. I'm having a look and how `bind()` works now. – macadam May 12 '16 at 15:53

0 Answers0