在Google地图中显示可见标记,许多标记

时间:2016-08-15 09:30:31

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

我正在为荷兰的一家商店建立商店定位器。这是一个很多数据,目前3455标记放在地图上。 我已经实现了markerclusterer来加快地图的加载速度。

但我希望在地图上列出所有当前可见的标记。我有这个例子工作(http://jsfiddle.net/glafarge/mbuLw/)但是在marker数组中有3455项非常慢。

有人知道这方面的解决方案吗?

这是我目前的代码:

var map,markers = [], mc;
function initMap() {
map = new google.maps.Map(document.getElementById('map-canvas'), {
    center: new google.maps.LatLng(-28, 135),
    zoom: 4,
    mapTypeId: google.maps.MapTypeId.ROADMAP,
    mapTypeControl: false,  
}); 

mc = new MarkerClusterer(map, {imagePath: 'images/maps'});
}

function centerMap(newLat, newLong){
map.setCenter({
        lat: newLat,
        lng: newLong
});

map.setZoom(18);
}

function setCurrentPosition(){
if (navigator.geolocation){
    navigator.geolocation.getCurrentPosition(function(position){
        var lat = position.coords.latitude;
        var long = position.coords.longitude;

        var geolocation = new google.maps.Marker({
            position: new google.maps.LatLng(lat, long),
            map: map,
            icon: 'http://labs.google.com/ridefinder/images/mm_20_green.png'
        });

        centerMap(lat, long);
    })
}
}

$(document).ready(function(){
setCurrentPosition();

var marker;

$.ajax({
    url: '*[getshoplocationslink]*',
    cache: false,
    success: function(data){

    for(var i = 0; i< data.length; i++){
         marker = new google.maps.Marker({
            position: new google.maps.LatLng(data[i].loc[1], data[i].loc[0]),
            map: map,
            title: data[i].name,
            icon: 'images/map_pin.png'
        });

        var content = '*[loading icon]*';
        var id = data[i]._id

        bindShopInfo(marker, map, content, data[i]._id);
        markers.push(marker);


        google.maps.event.addListener(map, 'idle', function(){
            showVisibleMarkers();
        });

        $('#shoplist').append('<div class="info info-' + (i+1) +'">'+ data[i].name + '</div>');

        }
        mc.addMarkers(markers);
        $('#loading').addClass('hidden');
    }
//end ajax
});

function bindShopInfo(marker, map, content, shopId){
    marker.addListener('click', function(){
        $('#shopinfo').toggleClass("hidden", false);
        $('#shopinfo').fadeIn("slow");
        $('#shopinfo').html(content);
        loadShop(shopId); //load additional data through another ajax function
    });
}

function showVisibleMarkers(){
    var bounds = map.getBounds(),
        count = 0;

    for(var i = 0; i < markers.length; i++){
        var marker = markers[i], infoPanel = $('.info-' + (i+1));

        if(bounds.contains(marker.getPosition()) == true){
            infoPanel.show();
            count ++;
        }else{
            infoPanel.hide();
        }
    }
}

0 个答案:

没有答案