底图切换功能

var createMapToggle = function() {
        var map = Application.map;
        if(map)
        {            
            var toggleButton = '<div id="toggleButtonBarContainer"><a href="#"><img id="baseMapTianditu" src="img/toogleMap/tdtVictor.png" title="天地图" /></a>'+
                '<a href="#"><img id="baseMapVector" src="img/toogleMap/victor_on.png" title="矢量图" /></a>'+
                '<a href="#"><img id="baseMapImage" src="img/toogleMap/image.png" title="影像图" /></a></div>';
            //
            var toogleBottonBar = '<div id="toggleButtonBar" style="position:fixed; top:10px; left:5px; overflow:hidden; padding:0px; margin:0px; vertical-align:middle; z-index:99910; ">'+toggleButton+'</div>'
            //
            $(toogleBottonBar).appendTo("#controllerDiv");
            //
            $("#toggleButtonBar img").each(function() {
                console.log((this).id);
                $(this).click(function() {
                    var id = (this).id;
                    //changeBaseMap(id);                    
                    PubSub.publish("changeBaseMapEvent", id);
                    //                    
                });
            });
        }
    };
    //
    var registerEventHandle = function()
    {
        PubSub.subscribe('changeBaseMapEvent', function(msg, data) {
            //
            var id = data;
            changeBaseMap(id);            
        });
    };
    //
    var changeBaseMap = function(id)
    {
        var map = Application.map;
        //
        var oldObj = $('#'+id);
        var oldSrc = oldObj.attr("src");   //oldObj.src;
        if(oldSrc.indexOf("_on.png") < 0)
        {
            var newSrc = oldSrc.replace(".png", "_on.png");
            oldObj.attr("src", newSrc);
            var layer = map.getLayer(id);
            console.log(id);
            if(layer)
            {
                console.log(id);
                layer.show();
                if("baseMapTianditu" === id)
                {
                    var layerAnno = map.getLayer(id+"Anno");
                    layerAnno.show();                            
                }
            }
            //
            $('#toggleButtonBar img').each(function() {                        
                var otherId = this.id;
                if(otherId.indexOf(id) < 0 )
                {
                    var otherLayer = map.getLayer(otherId);
                    if(otherLayer)
                    {
                        otherLayer.hide();
                        if("baseMapTianditu" === otherId)
                        {
                            var layerAnno = map.getLayer(otherId+"Anno");
                            layerAnno.hide();                                
                        }
                    }
                    var oldSrc = (this).src;
                    var newSrc = oldSrc.replace("_on.png", ".png");
                    $(this).attr("src", newSrc);
                }
                //
            });        
        }        
    };
posted @ 2016-08-07 10:17  ParamousGIS  阅读(371)  评论(0编辑  收藏  举报