简体   繁体   English

如何在 Google 地图上搜索邮政编码和城市

[英]How to Search Post Code and Cities Google Maps

Hi I have a code that searching only the cities on Australia.嗨,我有一个只搜索澳大利亚城市的代码。

How can I able to search the Post Code also?我怎样才能搜索邮政编码?

Please see my code:请看我的代码:

 function initialize() { var ac = new google.maps.places.Autocomplete( (document.getElementById('autocomplete')), { types: ['(cities)'], componentRestrictions: { country: 'AU' } }); ac.addListener('place_changed', function() { var place = ac.getPlace(); if (!place.geometry) { // User entered the name of a Place that was not suggested and // pressed the Enter key, or the Place Details request failed. window.alert("No details available for input: '" + place.name + "'"); return; } // Alert the selected place window.alert("You selected: '" + place.formatted_address + "'"); }); }
 #autocomplete { width: 300px; }
 <script src="https://maps.googleapis.com/maps/api/js?libraries=places&callback=initialize" async defer></script> <input id="autocomplete" placeholder="Enter your address" type="text"></input>

use regions instead cities in types cause cities limited to locality or administrative_area_level_3 where regions includes postal_code along with类型中使用regions而不是cities会导致cities仅限于localityadministrative_area_level_3 regions postal_code ,其中regions包括postal_code以及

  1. locality地方
  2. sublocality次区域性
  3. country国家
  4. administrative_area_level_1行政区域_级别_1
  5. administrative_area_level_2行政区域_级别_2

check source : ( https://developers.google.com/places/web-service/autocomplete#place_types )检查来源:( https://developers.google.com/places/web-service/autocomplete#place_types

use below updated code使用下面的更新代码

  `var ac = new google.maps.places.Autocomplete(
    (document.getElementById('autocomplete')), {
      types: ['(regions)'],
      componentRestrictions: {
        country: 'AU'
      }
    });`

also check updated code snippet.还要检查更新的代码片段。

 function initialize() { var ac = new google.maps.places.Autocomplete( (document.getElementById('autocomplete')), { types: ['(regions)'], componentRestrictions: { country: 'AU' } }); ac.addListener('place_changed', function() { var place = ac.getPlace(); if (!place.geometry) { // User entered the name of a Place that was not suggested and // pressed the Enter key, or the Place Details request failed. window.alert("No details available for input: '" + place.name + "'"); return; } // Alert the selected place window.alert("You selected: '" + place.formatted_address + "'"); }); }
 #autocomplete { width: 300px; }
 <script src="https://maps.googleapis.com/maps/api/js?libraries=places&callback=initialize" async defer></script> <input id="autocomplete" placeholder="Enter your address" type="text"></input>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM