2

Jquery UIを使用して、Google Maps Geocoderを使用して住所のオートコンプリート検索を行っています。住所を選択すると、ジオコード情報が返されます。スニペットは次のとおりです。

$('#address-dropdown').autocomplete({
  //Use geocoder to fetch lat+long values from an address input using google maps API
  source: function(request, response) {
    geocoder.geocode( {'address': request.term }, function(results, status) {
      response($.map(results, function(item) {
        return {
          label: item.formatted_address,
          value: item.formatted_address,
          location: item.geometry.location,
          latitude: item.geometry.location.lat(),
          longitude: item.geometry.location.lng()
        }
      }));
    })
  },

使用「postal_code」を返して、許容範囲内にあるかどうかを確認する必要があります。出力例を次に示します。http://maps.googleapis.com/maps/api/geocode/json?address = 1601 + Main + St + Eaton + Rapids + MI + 48864&sensor = false

postal_code情報をターゲットにするにはどうすればよいですか?

4

1 に答える 1

3

私の例を見てください:http://jsfiddle.net/nEKMK/

oあなたのオブジェクトです。

if (o.results[0].address_components) {
    for (var i in o.results[0].address_components) {
        if (typeof(o.results[0].address_components[i]) === "object" && o.results[0].address_components[i].types[0] == "postal_code") {
            var result = o.results[0].address_components[i].long_name;
        }
    }
}
if (!result) {
    alert("Error, there is no postal code");
} else {
   alert(result);
}
于 2011-12-05T03:07:34.210 に答える