如何用条件从getJson显示数组对象?

时间:2016-06-20 01:03:14

标签: javascript jquery google-maps

我想获得带有条件的数组对象结果(在我的情况下,如果管理区域级别为2,我只想获取数据)。我怎样才能做到这一点?我试过下面的情况,但有时我得到一个空白的警报。也许我必须创建一个循环,并在其中创建条件?任何帮助,将不胜感激。 :)

这是json结果:

{
 "results" : [
  {
     "address_components" : [
        {
           "long_name" : "77379",
           "short_name" : "77379",
           "types" : [ "postal_code" ]
        },
        {
           "long_name" : "Spring",
           "short_name" : "Spring",
           "types" : [ "locality", "political" ]
        },
        {
           "long_name" : "Harris County",
           "short_name" : "Harris County",
           "types" : [ "administrative_area_level_2", "political" ]
        },
        {
           "long_name" : "Texas",
           "short_name" : "TX",
           "types" : [ "administrative_area_level_1", "political" ]
        },
        {
           "long_name" : "United States",
           "short_name" : "US",
           "types" : [ "country", "political" ]
        }
     ],
     "formatted_address" : "Spring, TX 77379, USA",
     "geometry" : {
        "bounds" : {
           "northeast" : {
              "lat" : 30.088189,
              "lng" : -95.47364999999999
           },
           "southwest" : {
              "lat" : 29.9871611,
              "lng" : -95.5887879
           }
        },
        "location" : {
           "lat" : 30.0314279,
           "lng" : -95.5302337
        },
        "location_type" : "APPROXIMATE",
        "viewport" : {
           "northeast" : {
              "lat" : 30.088189,
              "lng" : -95.47364999999999
           },
           "southwest" : {
              "lat" : 29.9871611,
              "lng" : -95.5887879
           }
        }
     },
     "place_id" : "ChIJtZcGtDLNQIYRtGE9AgmSOPQ",
     "postcode_localities" : [ "Klein", "Spring" ],
     "types" : [ "postal_code" ]
    }
  ],
  "status" : "OK"
  }

这是我的代码:

var latLng = marker.latLng;
$latitude.value = latLng.lat();
$longitude.value = latLng.lng();
var urlgeocode = "https://maps.googleapis.com/maps/api/geocode/json?latlng=" + 
latLng.lat()+ "," +latLng.lng() + "&key=AIzaSyB0Yef9tL8vM148wqdMTmYMBEO7qjPtKXE";

$.getJSON(urlgeocode, function(data, textStatus){
var streetadress= data.results[0].address_components[2].long_name;
alert(streetadress);
});      
});

提前致谢

0 个答案:

没有答案