因此,我无法弄清楚为什么这行不通:

$.getJSON('http://www.mapquestapi.com/geocoding/v1/reverse?key=KEY&callback=renderReverse&json&location=' + lat + ',' + longi, function(geo) {
    $('#location').html(geo.response.results[0].locations[0].adminArea4);
});


返回的json是:

{
    "results": [
        {
            "locations": [
                {
                    "latLng": {
                        "lng": -1,
                        "lat": 51
                    },
                    "adminArea4": "Hamp",
                    "adminArea5Type": "City",
                    "adminArea4Type": "County",
                    "adminArea5": "Ba and De",
                    "street": "Teal Crescent",
                    "adminArea1": "GB",
                    "adminArea3": "England",
                    "type": "s",
                    "displayLatLng": {
                        "lng": -1.145731,
                        "lat": 51.240441
                    },
                    "linkId": 0,
                    "postalCode": "RG3",
                    "sideOfStreet": "N",
                    "dragPoint": false,
                    "adminArea1Type": "Country",
                    "geocodeQuality": "ADDRESS",
                    "geocodeQualityCode": "L1AAA",
                    "mapUrl": "http://www.mapquestapi.com/staticmap/v4/getmap?key=Fmjtd|luubnu0yn5,7g=o5-9072ur&type=map&size=225,160&pois=purple-1,51.2401563,-1.1463337,0,0|&center=51,-17&zoom=15&rand=-859825484",
                    "adminArea3Type": "State"
                }
            ],
            "providedLocation": {
                "latLng": {
                    "lng": -1.146334,
                    "lat": 51.240156
                }
            }
        }
    ],
    "options": {
        "ignoreLatLngInput": false,
        "maxResults": -1,
        "thumbMaps": true
    },
    "info": {
        "copyright": {
            "text": "© 2013 MapQuest, Inc.",
            "imageUrl": "http://api.mqcdn.com/res/mqlogo.gif",
            "imageAltText": "© 2013 MapQuest, Inc."
        },
        "statuscode": 0,
        "messages": []
    }
}


我猜这是因为我geo.response.results弄错了!

帮助将不胜感激。

最佳答案

返回的JSON中没有response键。尝试这个:

$.getJSON('http://www.mapquestapi.com/geocoding/v1/reverse?key=KEY&callback=renderReverse&json&location=' + lat + ',' + longi, function(geo) {
    $('#location').html(geo.results[0].locations[0].adminArea4);
});

关于javascript - 无法从MapQuest API返回的JSON中检索值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19662428/

10-12 07:00