0

私はこれを長い間理解しようとしてきたので、それが私を正面から見つめていると確信しています。

jqueryを使用して、ユーザーが入力したアドレスを次のコードでオートコンプリートしています。

    $(function() {
      $("#address").autocomplete({
      //This bit uses the geocoder to fetch address values
      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,
      latitude : item.geometry.location.lat(),
      longitude : item.geometry.location.lng(),
      streetNo : item.address_components[0].long_name,
      streetName : item.address_components[1].long_name,
      town : item.address_components[2].long_name,
      province : item.address_components[4].long_name,
             }
         }));
      })
  }

住所の構成要素、つまり郵便番号を具体的に抽出する必要があります。現在、数字を使用していますが、入力した住所によっては、フィールドが正しい場合と正しくない場合があります。

私はこの男がしたのとほとんど同じ質問があります:

Google Maps Geocoder:postal_codeを返す

その質問への答えは適切ですが、ループしようとしたとき、またはここですでに行われている方法で情報を抽出する以外のことをしようとしたときに私が抱えている問題は、スクリプトが失敗します

jqueryオートコンプリートがGoogleクエリを実行する方法に基づいて、「結果」を変数に割り当て、それをその方法で解析して正しい型名を取得できますか?

これまでに試したことはすべて失敗し、通常はスクリプトが実行されません。

私はここで必死になっています、うまくいけば私の質問は首尾一貫しています!:P

4

2 に答える 2

2

次のコードは、郵便番号を次の場所に抽出するために機能しますpostal_code

$(function() {
  var geocoder = new google.maps.Geocoder();

  $("#address").autocomplete({
    //This bit uses the geocoder to fetch address values
    source : function(request, response) {
      geocoder.geocode({'address' : request.term },

      function(results, status) {
        response($.map(results, function(item) {
          var postalCode = "";
          if (item.address_components) {
            for (var i in item.address_components) {
              if (typeof(item.address_components[i]) === "object" && item.address_components[i].types[0] == "postal_code") {
                postalCode = item.address_components[i].long_name;
              }
            }
          }

          return {
            label : item.formatted_address,
            value : item.formatted_address,
            latitude : item.geometry.location.lat(),
            longitude : item.geometry.location.lng(),
            streetNo : item.address_components[0].long_name,
            streetName : item.address_components[1].long_name,
            town : item.address_components[2].long_name,
            province : item.address_components[4].long_name,
            postal_code : postalCode
          }
        }));
      })
    }
  });
});

このjsfiddleを参照してください:http://jsfiddle.net/mccannf/uzvvq/3/

于 2012-11-14T03:37:28.397 に答える
0

さて、私はなんとか理解することができました。他の誰かがこのことに行き詰まった場合に備えて、ここに答えを投稿してください。

コーディング方法により、プログラマーは、stackoverflowの別の回答で記述された関数を使用して、探しているタイプを正確に抽出できます。

住所コンポーネントを抽出するためのより効率的な方法

私はユーザーを使用しました:ヨハンの答えですが、「ギャップを埋める」のに苦労するかもしれない私のような他の「趣味」プログラマーのために、全体的な「範囲」を大きくするためにコードを含めます。

$(function() {
    $("#address").autocomplete({
        //This bit uses the geocoder to fetch address values
        source : function(request, response) {  

            geocoder.geocode({
                'address' : request.term
            },function(results, status) {
                //Here we take the response and manipulate the map
                response($.map(results, function(item) {
                    //Function here to be called by the variables we want, essentially it loops through the array
                    //of address_components that come back from the geolocate, and loops until it finds the 'type'
                    //Entered in the "return" section below
                    function extractFromAdress(components, type){
                        for (var i=0; i<components.length; i++)
                            for (var j=0; j<components[i].types.length; j++)
                                if (components[i].types[j]==type) return components[i].long_name;
                            return "";
                            }
                    //Returns the variables, left of the ':' to the script
                    //Code right of the ':' is whatever we want to assign to the variable
                    return {
                        label : item.formatted_address,
                        value : item.formatted_address,
                        //Lat and Long from the geo-locate
                        latitude : item.geometry.location.lat(),
                        longitude : item.geometry.location.lng(),
                        //Typical street address values a user would input, and what a typical program would need.                          
                        streetNo : extractFromAdress(item.address_components, "street_number"),
                        streetName : extractFromAdress(item.address_components, "route"),
                        town : extractFromAdress(item.address_components, "administrative_area_level_3"),
                        province : extractFromAdress(item.address_components, "administrative_area_level_1"),
                        postal : extractFromAdress(item.address_components, "postal_code"),
                    }

                }));
            })
        }

アドレスから利用できるタイプのリファレンスについては、次のリンクを確認してください。

https://developers.google.com/maps/documentation/geocoding/index#JSON

みんな乾杯。

于 2012-11-16T12:29:51.117 に答える