2012-02-01 3 views
1

Google지도 API에서 일부 정보를 JS와의 내 응용 프로그램으로 읽으려고합니다. 하지만 조용한 havent는 반환 된 jSON 객체의 정보에 액세스하는 것에 대해 머리가났습니다. 내 JS 내가 주소 구성 요소에 액세스하려고 ...하지만 객체를 제대로 통과하는 방법이 확실하고자바 스크립트로 jSON 데이터를 읽는 중

 var site = "./maps/scripts/reverseGeocode/locale.php"; 
     var params = "lat="+lat+"&"+"lng="+lng; 

     var object = system.functions().ajax(site, params); 

      var result = object.address_components[0]; 

      alert(result); 

json으로 개체는 다음과 같다 ...

{ 
    "results" : [ 
     { 
    "address_components" : [ 
     { 
      "long_name" : "24", 
      "short_name" : "24", 
      "types" : [ "street_number" ] 
     }, 
     { 
      "long_name" : "Carabella St", 
      "short_name" : "Carabella St", 
      "types" : [ "route" ] 
     }, 
     { 
      "long_name" : "Kirribilli", 
      "short_name" : "Kirribilli", 
      "types" : [ "locality", "political" ] 
     }, 
     { 
      "long_name" : "New South Wales", 
      "short_name" : "NSW", 
      "types" : [ "administrative_area_level_1", "political" ] 
     }, 
     { 
      "long_name" : "Australia", 
      "short_name" : "AU", 
      "types" : [ "country", "political" ] 
     }, 
     { 
      "long_name" : "2061", 
      "short_name" : "2061", 
      "types" : [ "postal_code" ] 
     } 
    ], 
    "formatted_address" : "24 Carabella St, Kirribilli NSW 2061, Australia", 
    "geometry" : { 
     "bounds" : { 
      "northeast" : { 
       "lat" : -33.84886950, 
       "lng" : 151.21871730 
      }, 
      "southwest" : { 
       "lat" : -33.84960880, 
       "lng" : 151.21810640 
      } 
     }, 
     "location" : { 
      "lat" : -33.84892950, 
      "lng" : 151.21817770 
     }, 
     "location_type" : "RANGE_INTERPOLATED", 
     "viewport" : { 
      "northeast" : { 
       "lat" : -33.84789016970850, 
       "lng" : 151.2197608302915 
      }, 
      "southwest" : { 
       "lat" : -33.85058813029150, 
       "lng" : 151.2170628697085 
      } 
     } 
    }, 
    "types" : [ "street_address" ] 
    }, 
    { 
    "address_components" : [ 
     { 
      "long_name" : "Kirribilli", 
      "short_name" : "Kirribilli", 
      "types" : [ "locality", "political" ] 
     }, 
     { 
      "long_name" : "New South Wales", 
      "short_name" : "NSW", 
      "types" : [ "administrative_area_level_1", "political" ] 
     }, 
     { 
      "long_name" : "Australia", 
      "short_name" : "AU", 
      "types" : [ "country", "political" ] 
     }, 
     { 
      "long_name" : "2061", 
      "short_name" : "2061", 
      "types" : [ "postal_code" ] 
     } 
    ], 
    "formatted_address" : "Kirribilli NSW 2061, Australia", 
    "geometry" : { 
     "bounds" : { 
      "northeast" : { 
       "lat" : -33.84245090, 
       "lng" : 151.22032150 
      }, 
      "southwest" : { 
       "lat" : -33.85270230, 
       "lng" : 151.21152060 
      } 
     }, 
     "location" : { 
      "lat" : -33.84753980000001, 
      "lng" : 151.21314540 
     }, 
     "location_type" : "APPROXIMATE", 
     "viewport" : { 
      "northeast" : { 
       "lat" : -33.84245090, 
       "lng" : 151.22032150 
      }, 
      "southwest" : { 
       "lat" : -33.85270230, 
       "lng" : 151.21152060 
      } 
     } 
    }, 
    "types" : [ "locality", "political" ] 
    }, 
    { 
    "address_components" : [ 
     { 
      "long_name" : "2061", 
      "short_name" : "2061", 
      "types" : [ "postal_code" ] 
     }, 
     { 
      "long_name" : "New South Wales", 
      "short_name" : "NSW", 
      "types" : [ "administrative_area_level_1", "political" ] 
     }, 
     { 
      "long_name" : "Australia", 
      "short_name" : "AU", 
      "types" : [ "country", "political" ] 
     } 
    ], 
    "formatted_address" : "New South Wales 2061, Australia", 
    "geometry" : { 
     "bounds" : { 
      "northeast" : { 
       "lat" : -33.84234370, 
       "lng" : 151.22032150 
      }, 
      "southwest" : { 
       "lat" : -33.85270230, 
       "lng" : 151.20932150 
      } 
     }, 
     "location" : { 
      "lat" : -33.84839570, 
      "lng" : 151.21461550 
     }, 
     "location_type" : "APPROXIMATE", 
     "viewport" : { 
      "northeast" : { 
       "lat" : -33.84234370, 
       "lng" : 151.22032150 
      }, 
      "southwest" : { 
       "lat" : -33.85270230, 
       "lng" : 151.20932150 
      } 
     } 
    }, 
    "types" : [ "postal_code" ] 
    }, 
    { 
    "address_components" : [ 
     { 
      "long_name" : "New South Wales", 
      "short_name" : "NSW", 
      "types" : [ "administrative_area_level_1", "political" ] 
     }, 
     { 
      "long_name" : "Australia", 
      "short_name" : "AU", 
      "types" : [ "country", "political" ] 
     } 
    ], 
    "formatted_address" : "New South Wales, Australia", 
    "geometry" : { 
     "bounds" : { 
      "northeast" : { 
       "lat" : -28.15619210, 
       "lng" : 153.63873290 
      }, 
      "southwest" : { 
       "lat" : -37.50503180, 
       "lng" : 140.99921220 
      } 
     }, 
     "location" : { 
      "lat" : -31.25321830, 
      "lng" : 146.9210990 
     }, 
     "location_type" : "APPROXIMATE", 
     "viewport" : { 
      "northeast" : { 
       "lat" : -28.15619210, 
       "lng" : 153.63873290 
      }, 
      "southwest" : { 
       "lat" : -37.50503180, 
       "lng" : 140.99921220 
      } 
     } 
    }, 
    "types" : [ "administrative_area_level_1", "political" ] 
    }, 
    { 
    "address_components" : [ 
     { 
      "long_name" : "Australia", 
      "short_name" : "AU", 
      "types" : [ "country", "political" ] 
     } 
    ], 
    "formatted_address" : "Australia", 
    "geometry" : { 
     "bounds" : { 
      "northeast" : { 
       "lat" : -9.22108360, 
       "lng" : 159.10545890 
      }, 
      "southwest" : { 
       "lat" : -54.77721850, 
       "lng" : 112.92145430 
      } 
     }, 
     "location" : { 
      "lat" : -25.2743980, 
      "lng" : 133.7751360 
     }, 
     "location_type" : "APPROXIMATE", 
     "viewport" : { 
      "northeast" : { 
       "lat" : -9.22108360, 
       "lng" : 159.10545890 
      }, 
      "southwest" : { 
       "lat" : -54.77721850, 
       "lng" : 112.92145430 
      } 
     } 
    }, 
    "types" : [ "country", "political" ] 
    } 
    ], 
    "status" : "OK" 
} 

입니다 ... .. 누군가 나를 도울 수 있겠 어.

  "long_name" : "Kirribilli", 
      "short_name" : "Kirribilli", 
+0

네이 내 집 주소는 ... 스토킹하지 마십시오 나 – Christopher

답변

3

이 그것을 수행해야합니다

var long_name = object.address_components[2].long_name; 
var short_name = object.address_components[2].short_name; 

편집 : 당신이 사용하는 것이 좋습니다 JQuery와 아약스가 가져 오는 대신 위해 :

var site = "./maps/scripts/reverseGeocode/locale.php"; 
var params = "lat="+lat+"&"+"lng="+lng; 

$.get(site + '/' + params, function(data) { 
    var long_name = data.address_components[2].long_name; 
    var short_name = data.address_components[2].short_name; 
}); 
+0

신속한 응답을 주셔서 감사합니다 ... 그것은 반환 object.address_components 정의되지 않습니다. 이게 내가 가진 문제 야. 그 생각이 좀 궁금해? – Christopher

+0

고마워하지만이 유형의 jQuery를 피하려고합니다. – Christopher

+0

그런 다음 json의 구식을 해독해야합니다. =) 여기 튜토리얼 http://www.json.org/js.html입니다. –

관련 문제