我有一组点,我想在嵌入式谷歌地图(API v3)上绘制。我希望边界容纳所有的点,除非缩放级别太低(即,缩小太多)。我的方法是这样的:

var bounds = new google.maps.LatLngBounds();

// extend bounds with each point

gmap.fitBounds(bounds); 
gmap.setZoom( Math.max(6, gmap.getZoom()) );

这行不通。最后一行“gmap.setZoom()”如果在fitBounds之后直接调用,则不会改变地图的缩放级别。

有没有一种方法来获得缩放级别的边界而不应用到地图?还有其他解决方法吗?


当前回答

计算完边界后,你可以检查左上角和右下角之间的距离;然后你可以通过测试距离来了解缩放级别(如果距离太远,缩放级别会很低),然后你可以选择是否使用setbound方法或setZoom..

其他回答

有同样的问题,需要适应地图上的许多标记。 这解决了我的问题:

申报范围 koderoid提供的使用方案(用于每个标记集bounds.extend(objLatLng)) 执行fitbounds后映射完成: google.maps.event。addListenerOnce(map, 'idle', function() { 地图。fitBounds(边界); });

我只是通过提前设置maxZoom来修复这个问题,然后在之后删除它。例如:

map.setOptions({ maxZoom: 15 });
map.fitBounds(bounds);
map.setOptions({ maxZoom: null });

我使用:

gmap.setZoom(24); //this looks a high enough zoom value
gmap.fitBounds(bounds); //now the fitBounds should make the zoom value only less

这将使用较小的24和必要的缩放级别根据你的代码,但它可能会改变缩放无论如何,并不关心你有多少缩小。

请尝尝这个。

// Find out what the map's zoom level is
zoom = map.getZoom();
if (zoom == 1) {
  // If the zoom level is that low, means it's looking around the
world.
  // Swap the sw and ne coords
  viewportBounds = new
google.maps.LatLngBounds(results[0].geometry.location, initialLatLng);
  map.fitBounds(viewportBounds);
}

如果这对你有帮助的话。

愿一切都好!

google.maps.event.addListener(marker, 'dblclick', function () {
    var oldZoom = map.getZoom(); 
    map.setCenter(this.getPosition());
    map.setZoom(parseInt(oldZoom) + 1);
});