Kevin
Kevin

Reputation: 2688

Google Maps GeoCode Callback Parameter error

I've read, read, and read some more, and as a result I've come up with the following:

// Place Markers on the Map
var PlaceMarkers = function (iw, adds, gc) {
    var image = {url: "http://mywebstte.com/Images/star2.png", size: new google.maps.Size(24, 24)};
    var aCt = adds.length;
    for(var i = 0; i < aCt; ++i) {
        GetLatLng(gc, adds[i].address, function(pos){
            if(pos){
                var ipop = '<h1>' + adds[i].title + '</h1>';
                if(!isBlank(adds[i].url)){
                    ipop += '<a href="' + adds[i].url + '" target="_blank">' + adds[i].url + '</a><br />';
                }
                ipop += '<div class="map_item_content" id="mi_content' + i + '">' + adds[i].content + '</div>';
                if(!isBlank(adds[i].mainphone)){
                    ipop += '<br /><strong>Phone:</strong> <a href="tel:'+adds[i].mainphone+'">' + adds[i].mainphone + '</a>';
                }
                if(!isBlank(adds[i].mainemail)){
                    ipop += '<br /><strong>Email:</strong> <a href="mailto:'+adds[i].mainemail+'">' + adds[i].mainemail + '</a>';
                }
                console.log('HEY NOW: ' + pos.toString() + ' - Location Found!'); // Never displays, as a result neither do the markers
                var mark = new google.maps.Marker({title: adds[i].title, position: pos, map: map, icon: image, html: ipop});            
                google.maps.event.addListener(mark, 'click', function(){
                    iw.setContent(this.html);
                    iw.open(map, this);
                });
            }
        });
    }
};
// Get Lat/Lng Location
var GetLatLng = function(gc, add, callback) {
    var ret = '';
    gc.geocode({'address': add}, function(res, status) {
        if (status == 'OK') {
            ret = res[0].geometry.location;
            console.log('Found Here: ' + ret.toString()); // Always displays
        } else {
            ret = null;
            console.log('Nothing Found For: ' + add);
        }
    });
    callback(ret);
};

Now, in GetLatLng, my console will show the found lat/lng for the address passed. I've read that I need the callback because these calls are asynchonous, however, even after using the callback PlaceMarkers is still blank and will not place the markers.

EDIT To GetLatLng

// Get Lat/Lng Location
var GetLatLng = function(gc, add, f) {
var ret = '';
gc.geocode({'address': add}, function(res, status) {
    if (status == 'OK') {
        f(res[0].geometry.location);
        console.log('Found Here: ' + ret.toString()); // Always displays
    }
});
return -1;
};

Now returns, but am getting an error with TypeError: adds[i] is undefined

Upvotes: 0

Views: 50

Answers (1)

geocodezip
geocodezip

Reputation: 161404

You need to use the data in the geocoder callback where it is available:

// Get Lat/Lng Location
var GetLatLng = function(gc, add, callback) {
    var ret = '';
    gc.geocode({'address': add}, function(res, status) {
        if (status == 'OK') {
            ret = res[0].geometry.location;
            console.log('Found Here: ' + ret.toString()); // Always displays
        } else {
            ret = null;
            console.log('Nothing Found For: ' + add);
        }
        callback(ret);
    }  /* end of geocoder callback function */ );
};

Upvotes: 2

Related Questions