谷歌地图v3 fitBounds()缩放太近,无法使用单个标记 [英] Google Maps v3 fitBounds() Zoom too close for single marker

查看:535
本文介绍了谷歌地图v3 fitBounds()缩放太近,无法使用单个标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法为 fitBounds()设置最大缩放级别?我的问题是,当地图只被送到一个位置时,它会尽可能地放大,这确实会使地图脱离上下文并使其无法使用。或许我采取了错误的做法?

Is there a way to set a max zoom level for fitBounds()? My problem is that when the map is only fed one location, it zooms in as far as it can go, which really takes the map out of context and renders it useless. Perhaps I am taking the wrong approach?

预先感谢!

Thanks in advance!

推荐答案

我喜欢mrt的解决方案(特别是当你不知道你将要绘制或调整多少点时),除非它将标记关闭,以便它不再位于地图的中心。我简单地将它延长了一个额外的点,从lat和lng中减去.01,因此它将标记保持在中心。很好,感谢mrt!

I like mrt's solution (especially when you don't know how many points you will be mapping or adjusting for), except it throws the marker off so that it isn't in the center of the map anymore. I simply extended it by an additional point subtracting .01 from the lat and lng as well, so it keeps the marker in the center. Works great, thanks mrt!

// Pan & Zoom map to show all markers
function fitToMarkers(markers) {

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

    // Create bounds from markers
    for( var index in markers ) {
        var latlng = markers[index].getPosition();
        bounds.extend(latlng);
    }

    // Don't zoom in too far on only one marker
    if (bounds.getNorthEast().equals(bounds.getSouthWest())) {
       var extendPoint1 = new google.maps.LatLng(bounds.getNorthEast().lat() + 0.01, bounds.getNorthEast().lng() + 0.01);
       var extendPoint2 = new google.maps.LatLng(bounds.getNorthEast().lat() - 0.01, bounds.getNorthEast().lng() - 0.01);
       bounds.extend(extendPoint1);
       bounds.extend(extendPoint2);
    }

    map.fitBounds(bounds);

    // Adjusting zoom here doesn't work :/

}

这篇关于谷歌地图v3 fitBounds()缩放太近,无法使用单个标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆