【问题标题】:Google maps Places API V3 autocomplete - restrict only allow to select hotels in canberra谷歌地图 Places API V3 自动完成 - 限制只允许选择堪培拉的酒店
【发布时间】:2014-12-16 01:34:37
【问题描述】:

我的新项目需要从谷歌地图获取地址信息,但我们的服务只提供堪培拉所有酒店的接送服务。 我的问题是我如何限制自动完成只选择堪培拉的酒店。

这是我的代码:

var defaultBounds = new google.maps.LatLngBounds(
  new google.maps.LatLng(-35.3345571, 149.0334956, 15),
  new google.maps.LatLng(-35.1910655, 149.1817188, 12));

    var options = {
        bounds: defaultBounds,
        types: ['establishment'],
        keyword:'hotel',
        componentRestrictions: {
            country: 'au'
        }
    };

var input = document.getElementById('dropoff_address2Fromgoogle');    

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

google.maps.event.addListener(autocomplete, 'place_changed', function () {
    var place = autocomplete.getPlace();
    document.getElementById('dropoff_address2Fromgoogle').value = place.name 
     + ", " + place.formatted_address
      + ", " + place.address_components[5].short_name;

});

我在堪培拉使用bounds来限制区域,但搜索结果仍然会返回其他区域,并且类型过滤器只有建立似乎没有返回我期望的更多结果。

【问题讨论】:

  • 没有将结果限制为城市的选项。使用边界只会更喜欢这些结果,但不会将结果限制在边界内。

标签: javascript google-maps google-maps-api-3


【解决方案1】:

这里是sample code,用于搜索选定城市的酒店。

基本上,示例代码分两个阶段调用 Google Maps API:一个是搜索城市(调用了onPlaceChanged()),另一个是搜索酒店。在后面的搜索中,您将对具有来自城市搜索的 bounds 参数和值为 'lodging'types 参数的 Places 对象调用 nearbySearch()

【讨论】:

    【解决方案2】:

    问题在于LatLngBounds 预计会出现西南角和东北角。

    而不是:

    var defaultBounds = new google.maps.LatLngBounds(
        new google.maps.LatLng(-35.3345571, 149.0334956, 15),
        new google.maps.LatLng(-35.1910655, 149.1817188, 12));
    

    应该是:

    var defaultBounds = new google.maps.LatLngBounds(
        new google.maps.LatLng(-35.1910655, 149.0334956),
        new google.maps.LatLng(-35.3345571, 149.1817188));
    

    【讨论】:

      猜你喜欢
      • 2023-01-31
      • 2012-12-22
      • 1970-01-01
      • 1970-01-01
      • 2016-11-17
      • 1970-01-01
      • 1970-01-01
      • 2014-04-17
      • 1970-01-01
      相关资源
      最近更新 更多