Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>First, "this" in javascript is a bit tricky and depends on where the function was called, not just where it was defined.</p> <p>The quickest cross browser solution is to use a closure. Google actually has a great article on closures with google maps here. </p> <p><a href="http://code.google.com/intl/en/apis/maps/documentation/javascript/v2/events.html#Event_Closures" rel="nofollow">http://code.google.com/intl/en/apis/maps/documentation/javascript/v2/events.html#Event_Closures</a></p> <p>You have access to the GEvent.bind method, which you could use to reference your call back correctly.</p> <pre><code> GEvent.bind(map, "click", myCounter, myCounter.increment); </code></pre> <p>But I don't know how to implement that with direction route. Also, it may be deprecated with v2. Here is a better link to the maps v3</p> <p><a href="http://code.google.com/intl/en/apis/maps/documentation/javascript/events.html" rel="nofollow">http://code.google.com/intl/en/apis/maps/documentation/javascript/events.html</a></p> <p>So, maybe consider something like this:</p> <pre><code> var directionsRequest = { origin:from, destination:to, travelMode: google.maps.DirectionsTravelMode.DRIVING }; var that = this; var planRouteCallback = function(result,status){ if(status == google.maps.DirectionsStatus.OK) { that.directionsRenderer.setDirections(result); that.directionsRenderer.setMap(google_map_object); } else { that.handleErrors(status); } }, this.directionsService.route(directionsRequest, planRouteCallback); </code></pre> <p>Simple closures really help with callbacks.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload