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("&","&",$mPost->post_title))."', ".$latlng[0].", ".$latlng[1].", ".$i."];";
} else {
//Can't find lat/lng so get it from google
echo "codeAddress('".addslashes(str_replace("&","&",$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.