0

我有代码,我想将几​​个地址发送到谷歌地图。但是,由于我正在执行一系列地理编码,我怎样才能使视口至少居中并在组上正确缩放?

function doBatchGeocodeAndSearch() {
$('#loading').css('visibility', 'visible');

var lines = $('#styled').val().split('\n');

for(var i = 0;i < lines.length;i++){
    geocoder.geocode( { 'address': lines[i]}, function(results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
        map.fitBounds(results[0].geometry.viewport); 
        // map.setCenter(bounds.getCenter(),
        // map.getBoundsZoomLevel(bounds));
        map.setCenter(results[0].geometry.location);
4

1 回答 1

0

删除 map.setCenter 调用。最后一次调用 map.fitBounds 将显示所有标记,如果只有一个标记,将放大非常接近,但可能需要特别处理。

var bounds = new google.maps.LatLngBounds();
for(var i = 0;i < lines.length;i++){
  geocoder.geocode( { 'address': lines[i]}, function(results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
      bounds.extend(results[0].geometry.location);
      map.fitBounds(bounds);
于 2013-08-19T17:44:14.653 回答