首页 / 浏览问题 / WebGIS / 问题详情
图层切换问题,
1EXP 2023年07月19日

图层切换代码运行时,只显示了图层,没有显示切换按钮

代码如下

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<!--meta http-equiv="X-UA-Compatible" content="IE=edge"-->
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>切换</title>
<script src="dist/leaflet/include-leaflet.js"></script>
</head>
<body>
<div id="map" style="width:1900px;height:950px;"></div>

<script>
    

    window.url="http://localhost:8090/iserver/services/map-SiChuan-2/rest/maps/SiChuan";
        //实例化map对象,坐标crs: L.CRS.EPSG4326,中心点center: { lng: 103.64, lat: 30.07 },缩放级别zoom: 8
        
        //【编写练习】加载天地图服务图层,密钥key(任选一个key): "e30c2ae658b877688726a99957b380b6、b188f8ab88d1f56a68ea672dd8c5d11e、4a00a1dc5387b8ed8adba3374bd87e5e",图层类型layerType: 'vec'
        var tiandituVec=new L.supermap.TiandituTileLayer({
            key:"e30c2ae658b877688726a99957b380b6",
            layerType:"vec",
        });
        var tiandituImg=new L.supermap.TiandituTileLayer({
            key:"e30c2ae658b877688726a99957b380b6",
            layerType:"img",
        });
        //加载iServer服务图层,设置图层透明transparent: true,透明度opacity: 0.9
        var sichuan = new L.supermap.TiledMapLayer(url, {
            transparent: true,
            opacity: 0.9
        });
        window.map = L.map('map', {
            crs: L.CRS.EPSG4326,
            center: { lng: 103.64, lat: 30.07 },
            zoom: 8,
            layers:[sichuan,tiandituVec]
        });
        var baseMaps={
            "四川市":sichuan,
        };
        var overlayMaps={
            "矢量地图":tiandituVec,
            "影像地图":tiandituImg,
        }
        L.control.layers(overlayMaps,baseMaps).addTo(map);
</script>
</body>
</html>

1个回答

您好,

请问下我们这个代码是自己写的吗,我没有找到js逻辑和按钮代码

https://iclient.supermap.io/examples/leaflet/examples.html#iServer

这里有一些案例代码供您参考

希望能帮助到您

950EXP 2023年07月19日

您好,我就是对比了实例网站的代码和我的差不多,所以找不到问题在哪里,这个功能的实例代码是这样的

<!--********************************************************************
* Copyright© 2000 - 2023 SuperMap Software Co.Ltd. All rights reserved.
*********************************************************************-->
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title data-i18n="resources.title_layerSwitchControl"></title>
    <script type="text/javascript" src="../js/include-web.js"></script>
    <script type="text/javascript" src="../../dist/leaflet/include-leaflet.js"></script>
    <style>
        .leaflet-control-layers.leaflet-control {
            margin-right: 60px;
        }
    </style>
</head>
<body style=" margin: 0;overflow: hidden;background: #fff;width: 100%;height:100%;position: absolute;top: 0;">
<div id="map" style="margin:0 auto;width: 100%;height: 100%"></div>
<script type="text/javascript">
    var host = window.isLocal ? window.server : "https://iserver.supermap.io";
    var BJ = L.marker([39.830660058696104, 116.92866163503169]).bindPopup(resources ? resources.text_beijingCity : '北京市'),
        CD = L.marker([30.40, 104.04]).bindPopup(resources ? resources.text_chengdu : '成都市');
    var cities = L.layerGroup([BJ, CD]);
    var China = new L.supermap.TiledMapLayer(host + '/iserver/services/map-china400/rest/maps/China', {noWrap: true});
    var ChinaDark = new L.supermap.TiledMapLayer(host + '/iserver/services/map-china400/rest/maps/ChinaDark', {noWrap: true});
    var map = L.map('map', {
        center: {lon: 0, lat: 0},
        maxZoom: 18,
        zoom: 2,
        zoomControl: false,
        layers: [China, cities]
    });
    var baseMaps = {
        "China": China,
        "ChinaDark": ChinaDark
    };
    var overlayMaps = {
        "Cities": cities
    };
    L.control.layers(baseMaps, overlayMaps).addTo(map);
</script>
</body>
</html>

实现图层切换的是这一部分

//添加图层切换控件
var baseMaps = {
"矢量图层":tiandituvec,
"影像图层":tiandituimg,
};
var overlayMaps = {
"成都市":chengdu
};
L.control.layers(baseMaps, overlayMaps).addTo(map);

这个切换控件应该是不全的,我私下了你qq你加我沟通下
您好,切换功能中的按钮需要触发js通过js来实现
...