最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Geo-Autocomplete for a certain country - Stack Overflow

matteradmin7PV0评论

I would like to have an input field on my website with autoplete for places within a certain country. For example in the code below I want to get only places within the country 'The Netherlands' as suggestions in the autoplete.

In the code below I'm getting suggestions for places in all countries, not specified for a certain country.

<!DOCTYPE html>
<html>

  <head>
<title>Google Maps JavaScript API v3 Example: Places Autoplete</title>
<script src=";libraries=places"
  type="text/javascript"></script>

<style type="text/css">
  body {
    font-family: sans-serif;
    font-size: 14px;
  }
  #map_canvas {
    height: 400px;
    width: 600px;
    margin-top: 0.6em;
  }
</style>

<script type="text/javascript">
  function initialize() {

    var input = document.getElementById('searchTextField');
    var autoplete = new google.maps.places.Autoplete(input, {country: 'NL'});

    autoplete.bindTo('bounds', map);

    var infowindow = new google.maps.InfoWindow();
    var marker = new google.maps.Marker({
      map: map
    });

    google.maps.event.addListener(autoplete, 'place_changed', function() {
      infowindow.close();
      var place = autoplete.getPlace();
      if (place.geometry.viewport) {
        map.fitBounds(place.geometry.viewport);
      } else {
        map.setCenter(place.geometry.location);
        map.setZoom(17);  // Why 17? Because it looks good.
      }

    });

    setupClickListener('changetype-all', []);

  }
  google.maps.event.addDomListener(window, 'load', initialize);
</script>

</head>
<body>
<div>
  <input id="searchTextField" type="text" size="50">
  <label for="changetype-all"></label>
</div>
</body>
</html>

I would like to have an input field on my website with autoplete for places within a certain country. For example in the code below I want to get only places within the country 'The Netherlands' as suggestions in the autoplete.

In the code below I'm getting suggestions for places in all countries, not specified for a certain country.

<!DOCTYPE html>
<html>

  <head>
<title>Google Maps JavaScript API v3 Example: Places Autoplete</title>
<script src="http://maps.googleapis./maps/api/js?sensor=false&libraries=places"
  type="text/javascript"></script>

<style type="text/css">
  body {
    font-family: sans-serif;
    font-size: 14px;
  }
  #map_canvas {
    height: 400px;
    width: 600px;
    margin-top: 0.6em;
  }
</style>

<script type="text/javascript">
  function initialize() {

    var input = document.getElementById('searchTextField');
    var autoplete = new google.maps.places.Autoplete(input, {country: 'NL'});

    autoplete.bindTo('bounds', map);

    var infowindow = new google.maps.InfoWindow();
    var marker = new google.maps.Marker({
      map: map
    });

    google.maps.event.addListener(autoplete, 'place_changed', function() {
      infowindow.close();
      var place = autoplete.getPlace();
      if (place.geometry.viewport) {
        map.fitBounds(place.geometry.viewport);
      } else {
        map.setCenter(place.geometry.location);
        map.setZoom(17);  // Why 17? Because it looks good.
      }

    });

    setupClickListener('changetype-all', []);

  }
  google.maps.event.addDomListener(window, 'load', initialize);
</script>

</head>
<body>
<div>
  <input id="searchTextField" type="text" size="50">
  <label for="changetype-all"></label>
</div>
</body>
</html>
Share Improve this question edited Nov 9, 2011 at 14:34 duncan 31.9k15 gold badges81 silver badges101 bronze badges asked Nov 9, 2011 at 14:26 BastiaanWWBastiaanWW 1,2994 gold badges20 silver badges35 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 2

You can set a bias (which you already have) but without actually checking each autoplete result with additional code you cannot limit the autoplete list to a certain country or area, to my knowledge.

Here is some more information on Google's Autoplete: http://code.google./apis/maps/documentation/places/autoplete.html#place_autoplete_requests

Ah, if you want to use the geo-autoplete plugin, then take a look at this example:

view-source:http://geo-autoplete.googlecode./svn/trunk/demo/ui.demo.html

This is from their example:

$('#demo3_location').geo_autoplete({
    geocoder_region: 'Africa',
    geocoder_types: 'country',
    mapheight: 100, mapwidth: 200, maptype: 'hybrid'
});

No need to change anything - you just have to create a Global variable option instead of {country: 'NL'} put options at there. Now declare Option before use.

options = {types: ['(cities)'],ponentRestrictions: { country: 'US' }};

Now Your line will be:

var autoplete = new google.maps.places.Autoplete(input, options);

Hope work for you - ! Thanks, Kavi (India)

Post a comment

comment list (0)

  1. No comments so far