Page 1 of 1

Problem with geocoder

Posted: Tue Jan 31, 2017 4:11 pm
by Terry Gilliver

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.


Problem with geocoder

Posted: Wed Feb 01, 2017 6:37 am
by Serhii Kulibaba

Hello Terry,

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


Problem with geocoder

Posted: Thu Feb 02, 2017 12:01 pm
by Terry Gilliver

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.