javascript - map 放大/缩小以动态包含所有引脚

标签 javascript jquery google-maps zooming pins

我有一张 map ,显示每个图钉的图钉和信息窗口。缩放级别为 9,在该缩放级别下,某些图钉不会显示。我需要动态控制缩放级别,以便一次显示 map Canvas 中的所有图钉。

最佳答案

有一个 LatLngBounds 对象。在创建每个标记时,您希望扩展边界以包含每个标记的位置。然后最后调用 fitBounds 方法来调整 map 大小以适合所有标记。

function initialize() {
        var arrPoints = [
            {
                lat: 51.498725,
                lng: -0.17312,
                description: "One",
                price: 1.11
            },
            {
                lat: 51.4754091676,
                lng: -0.186810493469,
                description: "Two",
                price: 2.22
            },
            {
                lat: 51.4996066187,
                lng: -0.113682746887,
                description: "Three",
                price: 3.33
            },
            {
                lat: 51.51531272,
                lng: -0.176296234131,
                description: "Four",
                price: 4.44
            }
        ];

        var centerLatLng = new google.maps.LatLng(51.532315,-0.1544);

        var map = new google.maps.Map(document.getElementById("map"), {
            zoom:               15,
            center:             centerLatLng,
            mapTypeId:          google.maps.MapTypeId.ROADMAP
        });

        // create the Bounds object
        var bounds = new google.maps.LatLngBounds();

        var homeMarker = new google.maps.Marker({
            position: centerLatLng,
            map: map,
            icon: "http://maps.google.com/mapfiles/ms/micons/green-dot.png"
        });

        for (var i = 0; i < arrPoints.length; i++) {
            position = new google.maps.LatLng(arrPoints[i].lat, arrPoints[i].lng);

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

            google.maps.event.addListener(marker, 'click', function() {
                infowindow.open(map, this);
            });

            // extend the bounds to include this marker's position
            bounds.extend(position);
        }

        // make sure we include the original center point
        bounds.extend(centerLatLng);

        // resize the map
        map.fitBounds(bounds);
    }

关于javascript - map 放大/缩小以动态包含所有引脚,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9214457/

相关文章:

jquery - 从 Google Charts 解析 Json

java - 当标记变得可见时,在标记周围添加一个圆圈

javascript - 按地点 ID 传递数组值反转地理编码

javascript - 使用本地模块,使用本地依赖

javascript - 在 Three.js 中使用具有多个重叠场景的轨道控件

javascript - 如何使用 OData 按部分值进行过滤?

ios - 如何在 ios 应用程序中添加 google map direction API?

javascript - 在 Lodash 2.x 中按多个字段排序

javascript - 一次应用多个翻译?

javascript - 如何使用 gulp-uncss 查看 javascript 文件和 .dust 模板