删除gmappanel标记-EXTJS 4

时间:2013-02-22 22:55:55

标签: javascript google-maps extjs extjs4 google-maps-markers

function addDoctorLocation(options) 
{
    var gm = Ext.getCmp('mygooglemap');
    var mpoint = new google.maps.LatLng(options.lat,options.lng);
    var marker = gm.addMarker(mpoint,options.marker,false,false, options.listeners);
    infoBubble = new InfoBubble({maxWidth: 300});
}

tree.on('checkchange', function(node){
    var data = node.data;

    if (data.checked == true){
        var options = {
            lat:3.951941,
            lng:102.052002,
            marker: {title:"Hello World!"},
            listeners: {
                click: function(e){

                }
            }     
        }     
        addDoctorLocation(options);           
    }

    Ext.MessageBox.show({
        title: 'Changed checkbox status',
        msg: 'MainID: ' + data.MainID + ' <br /> Checkbox status: ' + data.checked,
        icon: Ext.MessageBox.INFO
    });
})

问题

此代码在选中复选框时成功添加标记,但是,如果取消选中该复选框,如何删除标记?
我是否需要为标记分配ID?取消选中后,我应该通过ID删除标记吗? 附加:地图有多个标记,而不仅仅是1个标记

1 个答案:

答案 0 :(得分:2)

创建标记结构作为全局变量。添加标记时,也可以将它们添加到结构中,根据复选框键入唯一的标记。因此,您可以稍后使用它来识别要隐藏的标记。如果data.checked!= true,那么使用marker.setMap(null);来摆脱它。

类似的东西:

var markers = {};

function addDoctorLocation(options) 
{
    var gm = Ext.getCmp('mygooglemap');
    var mpoint = new google.maps.LatLng(options.lat,options.lng);
    var marker = gm.addMarker(mpoint,options.marker,false,false, options.listeners);
    infoBubble = new InfoBubble({maxWidth: 300});

    // assign it into the structure
    markers[options.MainID] = marker;
}

tree.on('checkchange', function(node){
    var data = node.data;

    if (data.checked == true){
        var options = {
            lat:3.951941,
            lng:102.052002,
            marker: {title:"Hello World!"},
            listeners: {
                click: function(e){

                }
            },
            MainID: data.MainID
        };     
        addDoctorLocation(options);           
    } else {
        markers[data.MainID].setMap(null);
    }

    Ext.MessageBox.show({
        title: 'Changed checkbox status',
        msg: 'MainID: ' + data.MainID + ' <br /> Checkbox status: ' + data.checked,
        icon: Ext.MessageBox.INFO
    });
});