0

I am trying to retrieve the latitude and longitude of a location from google maps via jquery ajax.

Here's my JavaScript code:

    var lat;
    var lng;

    function setCordinates(address)
    {
        var ret = false;
        data = "address="+address;
        $.ajax({
            type: "GET",
            url: "https://maps.googleapis.com/maps/api/geocode/json",
            data: data,
            cache: false,
            dataType: 'json',
            success: function(json_data)
            {
                lat = json_data.results.geometry.location.lat;
                lng = json_data.results.geometry.location.lng;
                ret = true;
            },
            error: function()
            { 
                alert("There was an error!");
            }
        });
        return ret;
    }

When I previewed the response body from my console I got the json data returned as :

{
    "results" : [ 
        { 
            "address_components" : [ 
                { 
                    "long_name" : "Uyo", 
                    "short_name" : "Uyo", 
                    "types" : [ "locality", "political" ] 
                }, 
                { 
                    "long_name" : "Akwa Ibom", 
                    "short_name" : "Akwa Ibom", 
                    "types" : [ "administrative_area_level_1", "political" ] 
                }, 
                { 
                    "long_name" : "Nigeria", 
                    "short_name" : "NG", 
                    "types" : [ "country", "political" ] 
                } 
            ], 
            "formatted_address" : "Uyo, Nigeria", 
            "geometry" : 
            { 
                "bounds" : 
                { 
                    "northeast" : 
                    { 
                        "lat" : 5.0906023, 
                        "lng" : 8.0030251 
                    }, 
                    "southwest" : 
                    { 
                        "lat" : 4.9681658, 
                        "lng" : 7.8638077 
                    } 
                }, 
                "location" : 
                { 
                    "lat" : 5.0377396, 
                    "lng" : 7.9127945 
                }, 
                "location_type" : "APPROXIMATE", 
                "viewport" : 
                { 
                    "northeast" : 
                    { 
                        "lat" : 5.0906023, 
                        "lng" : 8.0030251 
                    }, 
                    "southwest" : 
                    { 
                        "lat" : 4.9681658, 
                        "lng" : 7.8638077 
                    } 
                } 
            }, 
            "partial_match" : true, 
            "types" : [ "locality", "political" ] 
        } 
    ], 
    "status" : "OK" 
} 

I am trying to get the values of lat and lng from location but I get an error:

TypeError: json_data.results.geometry is undefined

I have looked at this post, this post, this post, this post and this post but I don't really know what to do. They all seem unrelated. Please help I am still new to json. Thanks.

Community
  • 1
  • 1
Jevison7x
  • 709
  • 2
  • 14
  • 31
  • `json_data.results` is an array, so you need to include the index of the element. It has only one element, so `json_data.results[0].geometry ` – Kuba Jagoda Oct 25 '14 at 21:38

5 Answers5

2

what about this:

lat = json_data.results[0].geometry.location.lat;
lng = json_data.results[0].geometry.location.lng;
luchopintado
  • 899
  • 11
  • 15
1
success: function(json_data)
            {
                lat = json_data.results[0].geometry.location.lat;
                lng = json_data.results[0].geometry.location.lng;
                ret = true;
            }

Results is an array, you also might want to include a some logic to handle the case in which multiple results are returned. /edited removed snippet

roger
  • 1,091
  • 1
  • 7
  • 15
1

results is an array in the JSON response, so you have to access an element of the array first:

lat = json_data.results[0].geometry.location.lat;
Jorge Tite
  • 11
  • 1
0

Your result is array.

So you have to get values of lat/lng by the following way:

json_data.results[0].geometry.location.lat 

and

json_data.results[0].geometry.location.lng
Artyom Pranovich
  • 6,814
  • 8
  • 41
  • 60
0

Using json.parse will turn it into an object that you can use.

Try:

var json_dataObject = JSON.Parse(json_data);
var lat = json_dataObject.results[0].geometry.location.lat;
var lat = json_dataObject.results[0].geometry.location.lng;
stf
  • 31
  • 2