18

i have been trying to use some ajax to save venue location in my application and stumbled across the following code on stack overflow

function getLatLong(address) 
{
    var geocoder = new google.maps.Geocoder();
    var result = "";
    geocoder.geocode( { 'address': address, 'region': 'uk' }, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            result[lat] = results[0].geometry.location.Pa;
            result[lng] = results[0].geometry.location.Qa;
        } else {
            result = "Unable to find address: " + status;
        }
    });
    return result;
}

my problem is when i call the function it returns nothing and when i debug and set breakpoints in chrome it breaks on return result first before it breaks on the result[lat] = results[0].geometry.location.Pa;

I know the array should be declared as type array but even when i was just returning the results[0].geometry.location object nothing was being returned

what can i do to return the lat/long of the location so i can store in my db?

Chris Mccabe
  • 1,902
  • 6
  • 30
  • 61
  • possible duplicate of [Google Places API why does it returns Lat and Lon under a different variable each time?](http://stackoverflow.com/questions/13505928/google-places-api-why-does-it-returns-lat-and-lon-under-a-different-variable-eac) – Gajus Sep 01 '15 at 11:54

5 Answers5

20

This is not the answer but don't use Pa and Qa always use the lng() and lat() functions:

 place.geometry.location
{...}
    Pa: 56.240477
    Qa: -0.902655999999979
    toString: function(){return"("+this.lat()+", "+this.lng()+")"}
    equals: function(a){return!a?k:Cd(this.lat(),a.lat())&&Cd(this.lng(),a.lng())}
    lat: function(){return this[a]}
    lng: function(){return this[a]}
    toUrlValue: function(a){a=Hd(a)?a:6;return $d(this.lat(),a)+","+$d(this.lng(),a)}
Lee Smith
  • 6,339
  • 6
  • 27
  • 34
  • 14
    To add more clarity to this answer: Google map's geocoder sometimes returns the lat and lng values as Pa and QA, other times it's Sa and Ta... the only guaranteed way is to use geometry.location.lat() and geometry.location.lng() – NexusRex Feb 22 '12 at 06:40
17

The problem you're facing is that you're treating the geocoder.geocode function as immediately completing before you do the return result. What's really happening is that the geocoder.geocode is triggered, then you get an immediate return of result. Because the asynchronous result has most likely not returned, your result is empty. Think of the geocoding result as a push, not a pull. The storeResult function, not shown, is whatever code you need to do to save the information. Because you're combining a result with an error string, you have to handle that in your storeResult function. As an alternative, you can have a status in the result that indicates succcess or failure.

function getLatLong(address) {
var geocoder = new google.maps.Geocoder();
var result = "";
geocoder.geocode( { 'address': address, 'region': 'uk' }, function(results, status) {
     if (status == google.maps.GeocoderStatus.OK) {
         result[lat] = results[0].geometry.location.Pa;
         result[lng] = results[0].geometry.location.Qa;
     } else {
         result = "Unable to find address: " + status;
     }
     storeResult(result);
    });
}
GitaarLAB
  • 14,536
  • 11
  • 60
  • 80
Tony Miller
  • 9,059
  • 2
  • 27
  • 46
  • thanks for this- i used javascript to validate the address to start with then posted it to the server and used a http request to grab the geo code info. will probably refactor further down the road – Chris Mccabe Jan 16 '12 at 23:11
  • As Lee Smith says, don't use internal fields of results. I just used those because the OP did so. Google often changes internal variable names, so you can't rely on them. Use the APIs. – Tony Miller Jan 19 '12 at 17:05
  • 4
    See Lee Smith's answer... use the geometry.location.lat() and geometry.location.lng() functions instead. – NexusRex Feb 22 '12 at 06:36
2

I struggled hard with this and found the following to work.

  1. Loop through the object and push into a new array:

    var newArray = [];
    
    for (var key in latLng) {
    
       newArray.push(key);
    
    }
    
  2. Call your object with variable values with the square bracket syntax, dot notation breaks:

    var lat = latLng[newArray[0]];
    
    var lng = latLng[newArray[1]];
    
lakewood
  • 607
  • 1
  • 5
  • 21
2
function getLatLong(address) 
{
    var geocoder = new google.maps.Geocoder();
    var result = '';
    geocoder.geocode( { 'address': address }, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            // Call the lat/lng functions to return a computed value.
            result[lat] = results[0].geometry.location.lat();
            result[lng] = results[0].geometry.location.lng();
        } else {
            result = 'Unable to find address: ' + status;
        }
    });
    return result;
}
    
getLatLong('address'); 
Levi Fuller
  • 13,631
  • 4
  • 38
  • 44
0

In my case simplest solution for getting latitude and longitude from place object was:

  var latitude=place.geometry.location['k'];
  var longitude=place.geometry.location['A'];
Mr Br
  • 3,831
  • 1
  • 20
  • 24