我怎么能限制谷歌地图的地方图书馆的自动填充的只有一个城市的地方建议? [英] How can I restrict the Google Maps Places Library's Autocomplete to suggestions of only one city's places?

查看:506
本文介绍了我怎么能限制谷歌地图的地方图书馆的自动填充的只有一个城市的地方建议?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前使用给定的code,但这只限制的建议,一个国家。
我见过一个实现,但其使用jQuery,我想这样做,而不使用jQuery。

  VAR输入=的document.getElementById('searchTextField');
VAR的选择= {
    //类型:['(市)'],
    componentRestrictions:{国家:'PK'}
};
无功自动完成=新google.maps.places.Autocomplete(输入,期权);


解决方案

尝试添加边界属性设置为选项如本code表示要传递到自动完成构造对象:

  VAR西南=新google.maps.LatLng(25.341233,68.289986);
VAR东北=新google.maps.LatLng(25.450715,68.428345);
VAR hyderabadBounds =新的google.maps.LatLngBounds(西南,东北);VAR的选择= {
    范围:hyderabadBounds,
    类型:'建立[或任何适合您的用法]'],
    componentRestrictions:{国家:'PK'}
};

如果你想在自动完成范围由地图的当前视口驱动,可以绑定映射到自动完成的边界是这样的:

  autocomplete.bindTo('界限',地图);

或者,如果它在你的应用程序更好,你还必须明确设置范围的选项必要时:

  autocomplete.setBounds(someOtherBounds);

这可能意味着你创建一个默认的城市,我Hyberabad没有在上面的例子。或者说,你允许你的用户从城市列表中选择启动,然后设置地图视所选择的城市。在自动完成如果你只用 componentRestrictions 国开始了将建议城市名PK 因为你已经在做。你会知道什么最适合你的应用程序。

最后,这并不真实,完整地限制自动完成只是城市,但它会达到你希望尽可能接近目前可能的结果。

I am currently using the given code, but this only restricts suggestions to one country. I have seen one implementation but its using jQuery and I want to do it without using jQuery.

var input = document.getElementById('searchTextField');
var options = {
    //types: ['(cities)'],
    componentRestrictions: { country: 'pk' }
};
var autocomplete = new google.maps.places.Autocomplete( input, options );

解决方案

Try adding a bounds property to the options object you are passing to the Autocomplete constructor, as shown in this code:

var southWest = new google.maps.LatLng( 25.341233, 68.289986 );
var northEast = new google.maps.LatLng( 25.450715, 68.428345 );
var hyderabadBounds = new google.maps.LatLngBounds( southWest, northEast );

var options = {
    bounds: hyderabadBounds,
    types:  ['establishment [or whatever fits your usage]'],
    componentRestrictions: { country: 'pk' }
};

If you want the Autocomplete bounds to be driven by the current viewport of the map, you can bind the bounds of the map to the Autocomplete this way:

autocomplete.bindTo('bounds', map);

Or, if it works better in your application, you also have the option to set the bounds explicitly whenever necessary:

autocomplete.setBounds( someOtherBounds );

This may mean that you create a default city, as I did with Hyberabad in the example above. Or that you allow your users to select from a list of cities to start and then set the map viewport to the selected city. The Autocomplete will suggest city names if you start out with just the componentRestrictions of country 'PK' as you are already doing. You will know what works best for your application.

Finally, this does not truly and completely restrict the Autocomplete to just the city, but it will achieve the result you wish as closely as currently possible.

这篇关于我怎么能限制谷歌地图的地方图书馆的自动填充的只有一个城市的地方建议?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆