How to link geocoder.geocode () google maps callback function

I have a view that contains a google map accessible via this.map in the view area. All is well in the world.

Next, I want to update the position of the map using events in my view. To do this, I take text input, use google.maps.Geocoder.geocode (), then update the position with:

setMapLocation: function (location) {

    _.bind(this.setMapLocationCallback, this);

    console.log(this);

    var geocoder = new google.maps.Geocoder();
    geocoder.geocode({'address': location}, this.setMapLocationCallback);

},

The .log console (here) shows me the scope, with this .map properly accessible. Note that I explicitly bind the callback to this here. Here's the callback:

setMapLocationCallback: function (results, status) {

    console.log('this in the callback');
    console.log(this);

    if (status == google.maps.GeocoderStatus.OK) {
        this.map.setCenter(results[0].geometry.location);
        var marker = new google.maps.Marker({
            map: this.map,
            position: results[0].geometry.location
        });
        this.mapCanvas.css({visibility: 'visibile'});
    } else {
        this.mapCanvas.css({visibility: 'hidden'});
    }
},

The problems are that inside the callback console.log (this) shows that it is attached to the Window object, although I directly attached it to the view object of this area.

this.map , , .

? , ?

backbonejs underscorejs, .

,

+3
1

 geocoder.geocode({'address': location}, this.setMapLocationCallback);

call(), this setMapLocationCallback

 var _this = this;
 geocoder.geocode({'address': location}, function(result, status) {
     _this.setMapLocationCallback.call(_this, result, status);
 });

MDN ()

+5

All Articles