简体   繁体   中英

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.

what about this:

lat = json_data.results[0].geometry.location.lat;
lng = json_data.results[0].geometry.location.lng;
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

结果是JSON响应中的数组,因此您必须首先访问该数组的元素:

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

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

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM