0

我需要根据 checkox 的状态添加/删除标记,这是我的添加/删除功能(无法正常工作):

function getPointOnMap(mapObj) { 
    var position = new google.maps.LatLng(mapObj.lattitude, mapObj.longtitude);

    if(markers.length!=0){
        for(var i=0; i<markers.length; i++){
            if(markers[i].getPosition().toString() == position.toString()){
                marker.setMap(null);
                markers.slice(i);
            } else {
                var marker = new google.maps.Marker({ 
                    position: new google.maps.LatLng(mapObj.lattitude, mapObj.longtitude), 
                    map: map });
                markers.push(marker);
            }
        }   
    } else {

        var marker = new google.maps.Marker({ 
            position: new google.maps.LatLng(mapObj.lattitude, mapObj.longtitude), 
            map: map });
        markers.push(marker);
    }
}

这是获取标记的检查框和数据的代码:

$.ajax({
            type : "POST",
            url : "/getElements.json",
            success : function(result) {
            var html = '';
                for ( var i = 0; i < result.length; i++) {
                    (function(n) {
                        var obj = result[i];
                        if (obj.field == 0) {
                            element = $("<input>", {
                            type : "checkbox",
                            id   : obj.id
                        });

                        element.click(function() {
                            getPointOnMap(obj);
                        });
                        $("#orders").append(element,
                            obj.address + "<br />");
                        }
                    })(i)
                }
            }
});

我需要根据复选框状态从地图中添加/删除标记,所以也许有人会指出我有逻辑错误的地方

4

1 回答 1

0

js函数中的循环+拼写错误存在逻辑错误。这是监听复选框动作的完整工作函数:

function getPointOnMap(mapObj) { 
        var position = new google.maps.LatLng(mapObj.lattitude, mapObj.longtitude);
        console.log(markers);
       var count = 0;


        if(markers.length!=0){
            for(var i=0; i<markers.length; i++){

                if(markers[i].getPosition().toString() == position.toString()){
                    count ++;
                    console.log("1");
                    markers[i].setMap(null);
                    markers.splice(i);
                } 
            }
            if(count==0) {
                var marker = new google.maps.Marker({ 
                    position: new google.maps.LatLng(mapObj.lattitude, mapObj.longtitude), 
                    map: map });
                markers.push(marker);
            }
        } else {
            console.log("3");
            var marker = new google.maps.Marker({ 
                position: new google.maps.LatLng(mapObj.lattitude, mapObj.longtitude), 
                map: map });
            markers.push(marker);
        }
        console.log(markers);
}
于 2013-06-09T12:11:54.517 回答