Jevison7x
Jevison7x

Reputation: 737

How do I retrieve these values from json?

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.

Upvotes: 0

Views: 267

Answers (5)

roger
roger

Reputation: 1141

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

Upvotes: 1

Jorge Tite
Jorge Tite

Reputation: 11

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;

Upvotes: 1

stf
stf

Reputation: 31

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;

Upvotes: 0

Artyom Pranovich
Artyom Pranovich

Reputation: 6962

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

Upvotes: 0

luchopintado
luchopintado

Reputation: 939

what about this:

lat = json_data.results[0].geometry.location.lat;
lng = json_data.results[0].geometry.location.lng;

Upvotes: 2

Related Questions