2

Can someone tell me why the thelocation is returning as undefined in this example?

function codeAddress(business,address,i,locations) {
    var thelocation = geocoder.geocode( { 'address': address}, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            var loc = [business, results[0].geometry.location.lat(), results[0].geometry.location.lng(), i];
            return loc;
        } 
    });
    console.log(thelocation);
}

Many thanks for the help.

Edit

Here's the full context... I have some values coming out of the database as lat & lng but only an address for others. I have to populate my array to create my map with markers but some of them have to go through the geocoder. Here's what I have that clearly doesn't work:

var locations = [];

geocoder = new google.maps.Geocoder();

<?php
    if ( !empty($posts) ) {
        $i = 1;
        foreach ( $posts as $mPost ) {

            //If we find a lat/lng override value
            if ( get_field('lat_lng_override', $mPost->ID) ) {
                $latlng = explode(",",get_field('lat_lng_override', $mPost->ID));
                echo "locations[{$i}] = ['". addslashes(str_replace("&","&amp;",$mPost->post_title))."', ".$latlng[0].", ".$latlng[1].", ".$i."];";
            } else {
            //Can't find lat/lng so get it from google
                echo "codeAddress('".addslashes(str_replace("&","&amp;",$mPost->post_title))."','".get_field('business_address', $mPost->ID).", ".get_field('city', $mPost->ID).", ON',".$i.",locations, function(loc){ locations.push(loc); });";
            }

            $i++;
        }
    }
?>

console.log(locations);

function codeAddress(business,address,i,locations, callback) {
    var thelocation = geocoder.geocode( { 'address': address}, function(results, status) {
      if (status == google.maps.GeocoderStatus.OK) {
        var loc = [business, results[0].geometry.location.lat(), results[0].geometry.location.lng(), i];
        callback(loc);
      } 
    });
}

The idea is that my array looks similar to this when I'm done:

var locations = [
  ['Bondi Beach', -33.890542, 151.274856, 4],
  ['Coogee Beach', -33.923036, 151.259052, 5],
  ['Cronulla Beach', -34.028249, 151.157507, 3],
  ['Manly Beach', -33.80010128657071, 151.28747820854187, 2],
  ['Maroubra Beach', -33.950198, 151.259302, 1]
];

Thanks in advance.

4
  • possible duplicate of How to return the response from an Ajax call? Commented Oct 24, 2014 at 20:33
  • Because geocoder.geocode doesn't return a value. Related questions: stackoverflow.com/q/14220321/218196, stackoverflow.com/q/23667086/218196 Commented Oct 24, 2014 at 20:41
  • Your edit doesn't change anything for the given answers. Commented Oct 25, 2014 at 0:45
  • Okay fair enough. I'm just struggling with my logic and ending up with a full array like the example. Execution is the problem so I'm just looking for ideas, if anyone is inclined to offer them. Commented Oct 25, 2014 at 22:27

2 Answers 2

1

Because .geocode is an async function, so you need to use a callback!

function codeAddress(business,address,i,locations, callback) {
    var thelocation = geocoder.geocode( { 'address': address}, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            var loc = [business, results[0].geometry.location.lat(), results[0].geometry.location.lng(), i];
            callback(loc);
        } 
    });
    console.log(thelocation);
}

And then to use it:

codeAddress(business, address, i, locations, function(location) {
    console.log(location);
});
Sign up to request clarification or add additional context in comments.

Comments

1

Theoritically speaking you can't catch the location value outside the aync task, cause you just can't say when it will complete..so your gonna have to catch it inside your callback so,

function codeAddress(business,address,i,locations) {
var thelocation = geocoder.geocode( { 'address': address}, function(results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
        var loc = [business, results[0].geometry.location.lat(), results[0].geometry.location.lng(), i];
        //return location;
 console.log(loc);
 //continue with what you would like to do with the results, something small i assume! :)
    } 
  });

}

5 Comments

"Theoritically speaking" ?
good answer, not theoretical following on your point of when it will complete you could mention promises but it is outside the scope. Good answer
@FelixKling i wonder is there a way to catch it on the fly.. Client side of course... Interesting point.. Observables ?
even still you can only ever catch it inside the callback, no matter how you go about it, if the value ain't there you just ain't gonna see it.. lol
"on the fly" really makes no sense when applying to async calls - the callback is there because the call is async - you can't do anything with data that hasn't returned yet.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.