javascript - Google Map API v3 - 以最大缩放显示所有可见标记

标签 javascript google-maps google-maps-api-3

我有一个页面,我可以在其中打开/关闭标记类别。一个标记(“属性”)将始终可见。我想尽可能放大以显示所有可见标记。

因此,如果我有 3 个靠得很近的标记,我想尽可能放大所有标记,同时仍然适合 3 个标记。

var fullBounds = new google.maps.LatLngBounds();
var map;
var infowindow;
var markers = {};
var nearbyPlaces = {{#property}}{{{stringify nearbyPlaces}}}{{/property}};
var property = new google.maps.LatLng({{#property}}{{address.geo.lat}},{{address.geo.lng}}{{/property}});
var name = {{#property}}{{{stringify name}}}{{/property}}
var prop = {{#property}}{{{stringify address}}}{{/property}};


function initialize() {

    map = new google.maps.Map(document.getElementById('map-canvas'), {
        center: property,
        zoom: 15
    });

    var marker = new google.maps.Marker({
        map: map,
        icon: '../../../../img/map/property.png', // Set Property to a green marker
        position: property
    });

    infowindow = new google.maps.InfoWindow();

    // Set infowindow for the property
    google.maps.event.addListener(marker, 'click', function () {
        infowindow.setContent(name + '<br/>' + prop.street + '<br/>' + prop.city + ', ' + prop.state.name);
        infowindow.open(map, this);
    });

    var l = nearbyPlaces.length;
    while (l--) {
        markers[nearbyPlaces[l].category] = [];
        createCategory(nearbyPlaces[l]);
    }

    console.log(map.getZoom());
}

function createCategory(item) {
    var l = item.places.length;
    while (l--) {
        var marker = createMarker(item.places[l]);
        markers[item.category].push(marker);
        console.log(item.category);

        switch(item.category){
            case 'Public Schools':
                marker.icon = '../../../../img/map/public_school.png';
                break;
            case 'Colleges':
                marker.icon = '../../../../img/map/college.png';
                break;
            case 'Preferred Employers':
                marker.icon = '../../../../img/map/work.png';
                break;
            default:
                marker.icon = '../../../../img/map/star.png'
        }

    }
}

function toggleCategory(name, el) {
    //map.fitBounds(fullBounds);
    var button = $(el);
    var visible = true;

    if (button.hasClass('active')) {
        visible = false;
    }

    button.toggleClass('active');


    var category = markers[name];
    for (var x = 0; x < category.length; x++){
        var lat = category[x].position.k;
        var lng = category[x].position.B;
        var point = new google.maps.LatLng(lat,lng);
        fullBounds.extend(point);
    }

    var l = category.length;
    console.log('cagegory length: ' + category.length);
    while (l--) {
        category[l].setVisible(visible);
    }

    showVisible();

}

function createMarker(place) {
//var lat = parseFloat(place.address.geo.lat);
//var lng = parseFloat(place.address.geo.lng);
//var point = new google.maps.LatLng(lat,lng);
//fullBounds.extend(point);
//console.log(place);

    var marker = new google.maps.Marker({
        map: map,
        title: place.title,
        icon: 'http://maps.google.com/mapfiles/ms/micons/red-dot.png',  // Set all other markers to red...
        position: new google.maps.LatLng(place.address.geo.lat, place.address.geo.lng)
    });

    google.maps.event.addListener(marker, 'click', function () {
        infowindow.setContent(place.title + '<br/>' + place.address.street + '<br/>' + place.address.city + ', ' + place.address.state.name);
        infowindow.open(map, this);
    });

    return marker;
}


function showVisible() {



    // FIT ONLY VISIBLE MARKERS

}


google.maps.event.addDomListener(window, 'load', initialize);

最佳答案

我所做的是在创建 Map 实例之后和创建任何 Marker 实例之前创建一个 LatLngBounds实例。然后,当我创建我的标记时,我为创建的每个标记调用 LatLngBounds 实例的扩展方法:

myLatLngBounds.extend( myMarker.getPosition() );

完成所有标记后,我在我的 map 实例上调用 fitBounds,将 LatLngBounds 传递给它:

myMap.fitBounds( myLatLngBounds );

关于javascript - Google Map API v3 - 以最大缩放显示所有可见标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24660992/

相关文章:

javascript - google maps api v3 - javascript - 透视/倾斜(不是卫星!)?

javascript - 在单个 WebForm 中拥有多个 Google Maps API

javascript - 如何使用 highland.js fork 流?

javascript - AngularJS 使用 ng-resource 和多种服务器轮询方法

javascript - 使用用户鼠标缩放时更改 map 类型

javascript - Google Map JS API 3 需要 API key 吗?

google-maps - Google map 未使用 Ionic 2 原生 google-maps 插件显示

javascript - 从map.data类获取标记使用geojson谷歌地图

javascript - Videojs requestPictureInPicture() 仅在滚动到顶部和底部时有效一次

javascript - JQuery 键盘事件代码属性始终未定义