Google使用边界映射地理编码

时间:2016-08-11 09:28:22

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

当我得到结果时,使用以下代码,我似乎没有获得结果的完整地址组件,我们正在使用其他服务来确定各种事物。

一旦我走出界限,它就可以了。但我仍然需要它限制在界限之内。

这是为什么?我该如何解决这个问题?

CODE:

_geocoder.geocode(_.extend(position, {
                bounds: {
                    west: -25.806884999999966,
                    east: 16.380615000000034,
                    south: 48.98742739340465,
                    north: 60.16884190739975
                }
            }), function (results, status) {
                if (status === google.maps.GeocoderStatus.OK) {
                    _lastResults = results;

                    deferred.resolve(results);
                } else {
                    deferred.reject('Geocode was not successful for the following reason: ' + status);
                }
            });

1 个答案:

答案 0 :(得分:0)

要将边界添加到GeocoderRequest,它会进入请求对象:

var data = {
  bounds: {
    west: -25.806884999999966,
    east: 16.380615000000034,
    south: 48.98742739340465,
    north: 60.16884190739975
  }
};
var bounds = new google.maps.LatLngBounds(
               new google.maps.LatLng(data.bounds.south, data.bounds.west), 
               new google.maps.LatLng(data.bounds.north, data.bounds.east));

geocoder.geocode({
  address: "London",
  bounds: bounds
}, function(results, status) {
  if (status === google.maps.GeocoderStatus.OK) {
    var marker = new google.maps.Marker({
      position: results[0].geometry.location,
      map: map
    });
  } else {
    alert('Geocode was not successful for the following reason: ' + status);
  }
});

注意但是:

  

可选参数:

     

界限 - LatLngBounds可以更加突出地偏向地理编码结果。 bounds参数只会影响(而非完全限制)地理编码器的结果。 (有关详细信息,请参阅下面的“视口偏差”。)

proof of concept fiddle

代码段



function initialize() {
  var map = new google.maps.Map(
    document.getElementById("map_canvas"), {
      center: new google.maps.LatLng(37.4419, -122.1419),
      zoom: 13,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    });
  var data = {
    bounds: {
      west: -25.806884999999966,
      east: 16.380615000000034,
      south: 48.98742739340465,
      north: 60.16884190739975
    }
  };
  var bounds = new google.maps.LatLngBounds(new google.maps.LatLng(data.bounds.south, data.bounds.west), new google.maps.LatLng(data.bounds.north, data.bounds.east));
  var rectangle = new google.maps.Rectangle({
    map: map,
    bounds: bounds
  });
  map.fitBounds(bounds);
  var geocoder = new google.maps.Geocoder();
  geocoder.geocode({
    address: "London",
    bounds: bounds
  }, function(results, status) {
    if (status === google.maps.GeocoderStatus.OK) {
      var marker = new google.maps.Marker({
        position: results[0].geometry.location,
        map: map
      });
    } else {
      alert('Geocode was not successful for the following reason: ' + status);
    }
  });
}
google.maps.event.addDomListener(window, "load", initialize);

html,
body,
#map_canvas {
  height: 100%;
  width: 100%;
  margin: 0px;
  padding: 0px
}

<script src="https://maps.googleapis.com/maps/api/js"></script>
<div id="map_canvas"></div>
&#13;
&#13;
&#13;