谷歌地图清除了MarkerClusters的新搜索

时间:2013-11-21 18:37:11

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

我无法让我的地图在新搜索中清除群集。有什么想法吗?

function clearLocations() {
    //infoWindow.close();
    for (var i = 0; i < markers.length; i++) {
        markers[i].setMap(null);
    }
    markers.length = 0;

    // clear #side_bar bottom text
    document.getElementById("side_bar").innerHTML = "";
    // clear .alertBox text
    $('.alertBox').html('');
}


function searchLocationsNear(center) {
    clearLocations();

    var searchUrl = 'phpsqlsearch_genxml.php?lat=' + center.lat() + '&lng=' + center.lng();
    downloadUrl(searchUrl, function (data) {
        var xml = parseXml(data);
        var markerNodes = xml.documentElement.getElementsByTagName("marker");
        if (markerNodes.length > 0) {

            var bounds = new google.maps.LatLngBounds();
            for (var i = 0; i < markerNodes.length; i++) {
                var name = markerNodes[i].getAttribute("name");
                var address = markerNodes[i].getAttribute("address");
                var distance = parseFloat(markerNodes[i].getAttribute("distance"));
                var category = markerNodes[i].getAttribute("category");
                var latlng = new google.maps.LatLng(
                    parseFloat(markerNodes[i].getAttribute("lat")),
                    parseFloat(markerNodes[i].getAttribute("lng")));

                // createOption(name, distance, i);
                createMarker(latlng, name, address, category);
                bounds.extend(latlng);
            }
            map.fitBounds(bounds);
            var markerclusterer = new MarkerClusterer(map, markers);
          // markerclusterer.setMap(null);
            makeSidebar(); 

        } else {
            $('.alertBox').html('Sorry, there are no jobs that are close to your location.');
        }
   });
}

1 个答案:

答案 0 :(得分:1)

你应该试试这个:

if (markers) {
    for (i in markers) {
        markers[i].setMap(null);
    }
    markers = [];
    markerclusterer.clearMarkers()
}

根据docs,群集程序可以使用clearMarkers()方法清除。

<强>更新

仅在我们已经创建MarkerClusterer时调用clear。

if(markerclusterer)
{
    clearLocations();
}