2017-02-26 4 views
1

私は入力フィールドを持っていますが、アドレスだけが返ってきます。Google Maps APIオートコンプリート - ストリートネームのみを出力する方法は?

しかし、私がstreetnameを入力するとき、入力フィールドはstreetnameとコンマと街の街を返します。

私は、次のコードしまっ:

<script src="http://maps.google.com/maps/api/js?libraries=places&region=de&language=de&sensor=false"></script> 
<input id="strasse" type="text" size="50"> 

$(function(){  

    var input = document.getElementById('strasse');   
    var autocomplete = new google.maps.places.Autocomplete(input, { 
     types: ["address"] 
    });   

    $("#strasse").focusin(function() { 
     $(document).keypress(function (e) { 
      if (e.which == 13) { 
       infowindow.close(); 

      } 
     }); 
    }); 
}); 

EG:

I入力して "Teststreet" 出力= "Teststreet、ミュンヘン"

リターンは唯一の "Teststreet" でなければなりません。

"、"(多分jQuery上)の後にすべてを削除する方法はありますか?

+0

http://stackoverflow.com/questions/10125189/google-maps-autocomplete-output-only-address-without-country-and-city –

+2

[Google maps Autocomplete:国と都市のない出力のみのアドレス](http://stackoverflow.com/questions/10125189/google-maps-autocomplete-output-only-address-without-country-and-city) –

答えて

2

あなたはplace_changedイベントリスナーを使用して、あなたが返されているものから欲しいものを選択することができます。

<script> 
    var input = document.getElementById('strasse'); 
    var autocomplete = new google.maps.places.Autocomplete(input, { 
      types: ["address"] 
     });  
    $(function(){  

     autocomplete.addListener('place_changed', fillInAddress); 

     $("#strasse").focusin(function() { 
      $(document).keypress(function (e) { 
       if (e.which == 13) { 
        infowindow.close(); 

       } 
      }); 
     }); 
    }); 

    function fillInAddress() { 
     // Get the place details from the autocomplete object. 
     var place = autocomplete.getPlace(); 
     if (typeof place.address_components[0] !== "undefined") { 
      $(input).val(place.address_components[0].long_name); 
     } 
    } 
    </script> 

hereを実現することができるものの非常に素敵なデモもあります。

この場合、返されるrouteタイプのlong_nameが使用されています。単純にconsole_log(place.address_components);を入力すると、返された完全なリストが表示されます。

+0

ありがとうございました!これは私が探していたものでした。 – Harvix

関連する問題