作者:柳惠珠
一.监听地图缩放事件来控制地图显示
这里使用的是缩放操作完成后触发的事件
//changelayer为触发事件后执行的方法名
map.events.on({"zoomend":changelayer});
二.用map的removeLayer()来移除已经加载的图层,然后新建要替换的图层
function changelayer(e){
console.log(e.object.zoom)
//判断当前的缩放级别,当小于5时显示china地图,大于则显示world地图
if(e.object.zoom <= 5){
if(map.layers[0]){
if(map.layers[0].name == "World"){
map.removeLayer(layerWorld);
layerchina= new SuperMap.Layer.TiledDynamicRESTLayer("china", url2);
layerchina.events.on({"layerInitialized": addLayer1});
map.events.un({"zoomend":changelayer});
}
}
}else{
if(map.layers[0]){
if(map.layers[0].name == "china"){
map.removeLayer(layerchina);
layerWorld = new SuperMap.Layer.TiledDynamicRESTLayer("World", url);
layerWorld.events.on({"layerInitialized": addLayer});
map.events.un({"zoomend":changelayer});
}
}
}
}
function addLayer(){
map.addLayer(layerWorld);
map.addControl(overviewmap);
map.setCenter(new SuperMap.LonLat(118, 40), 6); setTimeout('map.events.on({"zoomend":changelayer})',1000);
}
function addLayer1(){
map.addLayer(layerchina);
map.addControl(overviewmap);
map.setCenter(new SuperMap.LonLat(118, 40), 6); setTimeout('map.events.on({"zoomend":changelayer})',1000);
}
三.在地图上增加鹰眼,鹰眼在构造函数中没有传layer,则使用主图的baselayer。
var overviewmap = new SuperMap.Control.OverviewMap();
//属性minRectSize:鹰眼范围矩形边框的最小的宽度和高度。默认为8pixels
overviewmap.minRectSize = 20;
map.addControl(overviewmap);
四.也可将两个地图都添加到map(这种方式最好坐标系相同或者比例尺相同)
1.控制地图显隐:
可以通过图层的setVisibility()方法来控制图层的显示与隐藏。
layerWorld.setVisibility(true);
layerchina.setVisibility(false);
2.设置图层顺序控制底图
设置map属性allOverlays为false的时候,map是不会讲多个图层进行叠加显示的,显示的是底图即图层顺序索引为0的图层。
可以用map的setLayerIndex()方法来设置图层顺序。
map.setLayerIndex(layerWorld,0);
3.设置map底图
设置map属性allOverlays为false的时候,map是不会讲多个图层进行叠加显示的,显示的是底图,用map的setBaseLayer()设置map的底图
map.setBaseLayer(layerchina);