谷歌地图api自动完成限制/偏向我镇的街道地址

时间:2012-12-04 12:18:02

标签: jquery google-maps-api-3 google-places-api

我正在尝试为uni的最后一年项目编写一个Web应用程序,我需要解决的首要问题之一是获取一个自动完成的文本框,用于本地地址输入。我花了一些时间环顾互联网并花了一些时间玩谷歌地图V3放置自动完成插件。

我对api文档有一些我不太了解的东西,我希望有人可以为我清理,也许可以指出我正确的方向。

在文档中它说:

  

(regions)类型集合指示Place服务返回与以下类型匹配的任何结果:

locality
sublocality
postal_code
country
administrative_area1
administrative_area2

这是我目前不理解的,我住在康沃尔郡(确切地说是博德明),我想让结果偏向博德明。我做了以下事情:

var input = document.getElementById('inputOne');
var defaultBounds = new google.maps.LatLngBounds(
  new google.maps.LatLng(50.423394,-4.803829),
  new google.maps.LatLng(50.508623,-4.692593));

        //need to set bounds to cornwall/bodmin
        var options = {
            bounds: defaultBounds,
          types: ['geocode'],
          componentRestrictions: {country: 'GB'}
        };
var autocomplete = new google.maps.places.Autocomplete(input,options);

现在这样可行,但是如果我输入“st n”,我会期望它首先出现在bodmin的st nicolas街。但它会提出像几英里远的st neot之类的东西。

我有一部分认为我需要将类型设置为不同的值,但我现在很难过。

编辑: 我想要实现的目标是在maps.google.com页面上,如果你输入,Bodmin然后在逗号之前移动光标并开始键入街道名称甚至Asda它会将它保持在该区域内的本地。我宁愿不必输入,博德明,但这是我可以解决的问题

如果有人有任何想法会很棒。那么我需要弄清楚用户如何输入一些值"Asda"并获取本地asda地址。

4 个答案:

答案 0 :(得分:4)

目前无法将结果限制在特定的位置,除非您正如上所述使用边界。但是,这只会使结果偏向,但不限于指定范围内包含的位置。

如果您认为按地区划分的结果是有用的功能,请提交Places API - Feature Request

正如您所提到的,如果您在查询结束时键入,Bodmin,则更有可能返回本地结果。临时解决方案可能是使用AutocompleteService并将,Bodmin附加到getPlacePredictions()请求输入参数的末尾。

答案 1 :(得分:3)

将类型设置为区域

    var options = {
        bounds: defaultBounds,
       types: ['(regions)'],
       componentRestrictions: {country: 'GB'}
     };
    var autocomplete = new google.maps.places.Autocomplete(input,options);

希望它有效..

答案 2 :(得分:0)

<div class="well container">
    <form role="form">
      <div class="form-group">
        <label for="locality" class="sr-only">Stadt oder PLZ</label>
          <input type="text" class="form-control" id="locality" name="locality" placeholder="Stadt oder PLZ" />
          <p class="help-block">Bitte zuerst die Stadt oder PLZ eingeben.</p>
      </div>
      <div class="form-group">
        <label for="route" class="sr-only">Straße / Hausnummer</label>
        <div class="row">
          <div class="col-xs-6">
            <input type="text" class="form-control col-xs-8" id="route" name="route" placeholder="Straße" disabled="true" />
          </div>
          <div class="col-xs-6">
            <input type="text" class="form-control col-xs-4" id="street_number" name="street_number" placeholder="Nr." disabled="true" />
          </div>
        </div>
      </div>
    </form>
</div>

<script>
var localityInput = document.getElementById('locality');
var localityOptions = {
  types: ['geocode'], //['(cities)'], geocode to allow postal_code
  componentRestrictions: {country: 'de'}
};
autocomplete = new google.maps.places.Autocomplete(localityInput, localityOptions);

google.maps.event.addListener(autocomplete, 'place_changed', function() {
    $('#route').attr('disabled', false).focus();
    $('#street_number').attr('disabled', false);

    var boundsByCity = autocomplete.getPlace().geometry.viewport;
    var routeInput = document.getElementById('route');
    var routeOptions = {
        bounds: boundsByCity,
        types: ['geocode']
    };
    new google.maps.places.Autocomplete(routeInput, routeOptions);
});
</script>

演示:jsfiddle

答案 3 :(得分:0)

var input = document.getElementById('inputSearch');
var cityBounds = new google.maps.LatLngBounds(
        new google.maps.LatLng(12.864162, 77.438610),
        new google.maps.LatLng(13.139807, 77.711895));
var options = {
        bounds: cityBounds,
        strictBounds: true,
        componentRestrictions: {country: 'in'}
    };
var inputBox = new google.maps.places.Autocomplete(input, options);
google.maps.event.addListener(inputBox, 'place_changed', function() {
        var place = inputBox.getPlace();
        console.log(place.geometry.location.lat() +', '+ place.geometry.location.lng());
    });

这对我很有用!