javascript - Google Maps API 3 - 在屏幕上显示所有标记,但保留中心点

标签 javascript cordova google-maps-api-3

这与 this question 非常相似.我想确保所有标记都显示在当前缩放级别。但是,我也想事先选择中心点(用户当前位置)。如果圆圈是标记,正方形是我想要的中心点,在下图中,链接的解决方案将创建第一个(左上)图像。我想要第二张(右下)图片。

What I don't want What I do want

最佳答案

您可以创建一个 LatLngBounds 对象并使用您的每个标记坐标扩展它。然后获取您的边界对象东北和西南坐标,并检查这些坐标是否包含在当前 map 边界内。如果不是,请缩小并重试。

下面的大部分代码是在一定范围内生成随机标记。真正有趣的部分是我调用 bounds.extend(position)fitAllBounds 函数的地方。

var map, bounds;

function initialize() {

  var southWest = new google.maps.LatLng(40, -70);
  var northEast = new google.maps.LatLng(35, -80);
  var lngSpan = northEast.lng() - southWest.lng();
  var latSpan = northEast.lat() - southWest.lat();

  var center = new google.maps.LatLng(40, -70);

  map = new google.maps.Map(document.getElementById("map-canvas"), {
    zoom: 12,
    center: center,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  });

  // Add center marker
  new google.maps.Marker({
    position: center,
    label: 'C',
    map: map
  });

  // Create the bounds object
  bounds = new google.maps.LatLngBounds();

  // Create random markers
  for (var i = 0; i < 20; i++) {

    // Calculate a random position
    var position = new google.maps.LatLng(southWest.lat() + latSpan * Math.random(), southWest.lng() + lngSpan * Math.random());

    var marker = new google.maps.Marker({
      position: position,
      map: map
    });

    google.maps.event.addListener(marker, 'click', (function(marker, i) {
      return function() {
        map.setZoom(5);
        map.setCenter(marker.position);
      }
    })(marker, i));

    // Extend the bounds with the last marker position
    bounds.extend(position);
  }

  // Fit all bounds once, when the map is ready
  google.maps.event.addListenerOnce(map, 'idle', function() {

    fitAllBounds(bounds);
  });
}

function fitAllBounds(b) {

  // Get north east and south west markers bounds coordinates
  var ne = b.getNorthEast();
  var sw = b.getSouthWest();

    // Get the current map bounds
  var mapBounds = map.getBounds();

  // Check if map bounds contains both north east and south west points
  if (mapBounds.contains(ne) && mapBounds.contains(sw)) {

    // Everything fits
    return;

  } else {

    var mapZoom = map.getZoom();

    if (mapZoom > 0) {

      // Zoom out
      map.setZoom(mapZoom - 1);

      // Try again
      fitAllBounds(b);
    }
  }
}

initialize();
#map-canvas {
  height: 200px;
  width: 200px;
}
<div id="map-canvas"></div>

<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk"></script>

它也在 JSFiddle 上:

JSFiddle demo

关于javascript - Google Maps API 3 - 在屏幕上显示所有标记,但保留中心点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43944655/

相关文章:

javascript - 具有方向的 Google map 在移动设备上不起作用 : "No Routes Found"

javascript - 如何对齐谷歌自定义搜索框?

javascript - 如何从模式中拆分字符串

iphone - 在Google JSON之外获取邮政编码

android - 软键盘使 Cordova View 缩小

cordova - 后退按钮 Cordova

javascript - 确定谷歌地图 V3 中的 Angular 点坐标

javascript - Rails js.erb 追加 block link_to

javascript - 在 js 中检查(读取)消息到控制台

ios - 从 PhoneGap 应用程序中删除未使用的启动画面和图标