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

I would like to have an input field on my website with autoplete for places within a certain country. F

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)

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745629049a4636991.html

相关推荐

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

    I would like to have an input field on my website with autoplete for places within a certain country. F

    12小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信