2011-11-09 121 views
1

我想在我的网站上有一个输入字段,并在某个国家/地区的地方自动填充。例如,在下面的代码中,我希望获得国家/地区“荷兰”内的地点作为自动填充中的建议。某个国家/地区的地理自动完成功能

在下面的代码中,我得到了所有国家/地区的地方建议,而不是针对某个国家/地区。

<!DOCTYPE html> 
<html> 

    <head> 
<title>Google Maps JavaScript API v3 Example: Places Autocomplete</title> 
<script src="http://maps.googleapis.com/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 autocomplete = new google.maps.places.Autocomplete(input, {country: 'NL'}); 

    autocomplete.bindTo('bounds', map); 

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

    google.maps.event.addListener(autocomplete, 'place_changed', function() { 
     infowindow.close(); 
     var place = autocomplete.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> 

回答

2

你可以设置一个偏置(你已经有了),但实际上不检查与其他代码自动完成每一个结果,你可以自动完成列表不局限于某一个国家或地区,据我所知。

这是对谷歌的自动完成一些更多的信息:http://code.google.com/apis/maps/documentation/places/autocomplete.html#place_autocomplete_requests

啊,如果你想使用地理自动完成插件,再看看这个例子:

视图出处:http:/ /geo-autocomplete.googlecode.com/svn/trunk/demo/ui.demo.html

这是他们的榜样:

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

在这个链接:http://code.google.com/p/geo-autocomplete/它说:geocoder_region ==>过滤建议位置到特定区域,例如, '欧洲'这就是为什么我认为它应该是可能的..我也尝试过geocoder_region – BastiaanWW

+0

geo-autocomplete需要额外的插件。我已经使用geocoder_region为您提供了一个更绝对的过滤器的演示程序。 – Derek

+0

这个例子是不工作(FF和IE)我也试过你的代码与ID:“searchTextField”,但也不工作.. – BastiaanWW

2

无需改变任何东西 - 你只需要创建一个全局变量选项,而不是 {country: 'NL'}options放在那里。 现在在使用前声明选项。

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

现在你的行会:

var autocomplete = new google.maps.places.Autocomplete(input, options); 

希望为你工作 - ! 谢谢, Kavi(印度)

相关问题