0

I have a function where I need to return a url that I am getting via an ajax call.

var heatmap = new google.maps.ImageMapType({
    getTileUrl: function(coord, zoom) {
        var tileURL;
        $.get('getimage.php', { zoom: zoom, x: coord.x, y: coord.y }, function(data) {
            if(data.status) { tileURL=data.image; }
        }, "json");
        return "tileURL";
    },
       tileSize: new google.maps.Size(256, 256),
       opacity:0.55,
       isPng: true
});

Obviously, the ajax call is asynchronous so I understand why the above code will return tileURL as undefined. I know in general people use callback functions to solve this issue, but I don't know how to get a call back to RETURN a value to the parent function. I'm working with the google maps API, so I don't really have any flexibility to change how that works.

user435281
  • 79
  • 2
  • 6
  • 1
    See [ How do I return a variable from Google Maps JavaScript geocoder callback? ](http://stackoverflow.com/questions/2993563/how-do-i-return-a-variable-from-google-maps-javascript-geocoder-callback) – Matthew Flaschen Sep 20 '10 at 22:39

1 Answers1

4

Because the Ajax request is asynchronous, your only option is to use a callback. If you could return a value to the "parent" (really, "calling") function, then the request wouldn't be asynchronous; it would block the calling function. Also, there is no way to get a reference to the calling function from within a closure in that function. (i.e., you can't return to a function higher up in the call stack).

Chris Laplante
  • 29,338
  • 17
  • 103
  • 134
  • So, are you saying its not possible to do what I want, assuming I can't change the way the getTileUrl function works in the google maps api? Because a callback wouldn't help me actually return the value to that getTileUrl function. – user435281 Sep 20 '10 at 22:58
  • Check the link @Matthew Flaschen posted above, it will help you. – Chris Laplante Sep 20 '10 at 23:02