重新计算markercluster和downloadUrl

时间:2015-06-02 09:03:59

标签: google-maps google-maps-api-3 filter markerclusterer

我正试图解决问题,但没有成功。

正如您在取消选中VDSL或ADSL选项时所看到的那样,markercluster仍具有相同的值。

downloadUrl("php_to_xml.php", function(data) {
        var xml = data.responseXML;
        var markers = xml.documentElement.getElementsByTagName("marker");
        for (var i = 0; i < markers.length; i++) {
            var ThisMarkers = markers[i];
            var lat = markers[i].getAttribute("lat");
            var lng = markers[i].getAttribute("lng");
            var typeofkafao = markers[i].getAttribute("typeofkafao");
            var type = markers[i].getAttribute("type");
            var point = new google.maps.LatLng(
                parseFloat(markers[i].getAttribute("lat")),
                parseFloat(markers[i].getAttribute("lng")));
            var icon = coloredRideshareIcon(typeofkafao);       
            //below is the balloon of the marker.
            var marker = new google.maps.Marker({
                map: map,
                position: point,
                icon: icon
            });
            addMarker(typeofkafao, marker, map, infoWindow, ThisMarkers);
        }
        var mcOptions = {gridSize: 40, maxZoom: 15};
        var markerCluster = new MarkerClusterer(map,gmarkers,mcOptions); // edo trexi
    });

function downloadUrl(url, callback) {
    var request = window.ActiveXObject ?
        new ActiveXObject('Microsoft.XMLHTTP') :
        new XMLHttpRequest;

        request.onreadystatechange = function() {
            if (request.readyState == 4) {
                request.onreadystatechange = doNothing;
                callback(request, request.status);
            }
        };

    request.open('GET', url, true);
    request.send(null);
}

我知道问题出在downloadUrl函数上,因为它是异步的。 我必须在downloadUrl之外调用markerCluster,但我不能因为我无法在downloadUrl函数之外发送标记变量。

我认为问题是用类似的东西解决但不完全像那样。

var markers2;
downloadUrl("php_to_xml.php", function(data) {
        var xml = data.responseXML;
        var markers = xml.documentElement.getElementsByTagName("marker");
        markers2 = markers;
    });

for (var i = 0; i < markers2.length; i++) {
    var ThisMarkers = markers2[i];
    var lat = markers2[i].getAttribute("lat");
    var lng = markers2[i].getAttribute("lng");
    var typeofkafao = markers2[i].getAttribute("typeofkafao");
    var type = markers2[i].getAttribute("type");
    var point = new google.maps.LatLng(
        parseFloat(markers2[i].getAttribute("lat")),
        parseFloat(markers2[i].getAttribute("lng")));
    var icon = coloredRideshareIcon(typeofkafao);       
    //below is the balloon of the marker.
    var marker = new google.maps.Marker({
        map: map,
        position: point,
        icon: icon
    });
    addMarker(typeofkafao, marker, map, infoWindow, ThisMarkers);
}
var mcOptions = {gridSize: 40, maxZoom: 15};
var markerCluster = new MarkerClusterer(map,gmarkers,mcOptions); // edo trexi

function downloadUrl(url, callback) {
    var request = window.ActiveXObject ?
        new ActiveXObject('Microsoft.XMLHTTP') :
        new XMLHttpRequest;

        request.onreadystatechange = function() {
            if (request.readyState == 4) {
                request.onreadystatechange = doNothing;
                callback(request, request.status);
            }
        };

    request.open('GET', url, true);
    request.send(null);
}

任何想法的人?

非常感谢

1 个答案:

答案 0 :(得分:0)

查看控制台会很有用:

ReferenceError:未定义markerCluster

markerCluster - 变量无法在Markers()

中访问

删除var - 关键字:

var markerCluster = new MarkerClusterer(map,gmarkers,mcOptions);