Terry Gilliver
Posts: 0
Joined: Fri Apr 18, 2014 8:45 pm

Problem with geocoder

I am using the following function to get latitude and longitude values:

pre//return lat & lng values of a postcode
var geocoder;
geocoder = new google.maps.Geocoder();

function codeAddress(address) {
var result = '';
geocoder.geocode( { 'address': address, 'region': 'uk' }, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
result['lat'] = results[0].geometry.location.lat();
console.log("results[0] = ",results[0]);
result['lng'] = results[0].geometry.location.lng();
} else {
result = "Unable to find address: " + status;
}
});
return result;
}/pre

It always returns an empty string. i am passing it a uk postcode

I think maybe part of my problem is that it is aynchronous which possibly means that 'result' is returned before it has allocated a value.

Serhii Kulibaba
Posts: 150
Joined: Tue Aug 27, 2013 1:47 pm

Problem with geocoder

Hello Terry,

You are right, it runs asynchronously, so that function returns the undefined value before Geocoder returns results

Terry Gilliver
Posts: 0
Joined: Fri Apr 18, 2014 8:45 pm

Problem with geocoder

Thanks, Sergiy,

I have decided that my design is flawed, and have put the latitude and longitude values directly into my database.

Calculating these on an ongoing basis would in any case make too many calls to the google geocode service, and as well as slow down my app, mean that I would quickly run out of my daily quota.

Please consider this solved.

Return to “Issues”