首页 / 浏览问题 / 云GIS / 问题详情
mapbox removeLayer不能再添加
1,255EXP 2019年07月22日
<!--********************************************************************
* Copyright© 2000 - 2019 SuperMap Software Co.Ltd. All rights reserved.
*********************************************************************-->
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title data-i18n="resources.title_editFeature"></title>
    <style>
        .editPane {
            position: absolute;
            right: 10px;
            top: 10px;
            text-align: center;
            background: #FFF;
            z-index: 1000;
        }
    </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>
<div>
    <div class="panel panel-primary editPane" id="editPane">
        <div class='panel-heading'>
            <h5 class='panel-title text-center' data-i18n="resources.text_editSingle"></h5></div>
        <div class='panel-body content'>
            <input type='button' class='btn btn-default' data-i18n="[value]resources.btn_addMarker" onclick='addMarker()'/>&nbsp;
            <input type='button' class='btn btn-default' data-i18n="[value]resources.btn_undoAdd" onclick='revocationMarker()'/>
            <input type='button' class='btn btn-default' data-i18n="[value]resources.text_input_value_submit" onclick='commit()'/>&nbsp;
            <input type='button' class='btn btn-default' data-i18n="[value]resources.text_input_value_clear" onclick='clearLayer()'/>
        </div>
    </div>
</div>
<script type="text/javascript" include="bootstrap,widgets.alert" src="../js/include-web.js"></script>
<script type="text/javascript" src="../../dist/mapboxgl/include-mapboxgl.js"></script>
<script>
    var map, id = [], pointFeature, alertDiv, featureService,
        baseUrl = (window.isLocal ? window.server : "http://support.supermap.com.cn:8090") + "/iserver/services/map-world/rest/maps/World",
        mapUrl = baseUrl + "/zxyTileImage.png?z={z}&x={x}&y={y}",
        dataUrl = (window.isLocal ? window.server : "http://support.supermap.com.cn:8090") + "/iserver/services/data-world/rest/data";
    var attribution = "<a href='https://www.mapbox.com/about/maps/' target='_blank'>© Mapbox </a>" +
        " with <span>© <a href='http://iclient.supermap.io' target='_blank'>SuperMap iClient</a> | </span>" +
       " Map Data <span>© <a href='http://support.supermap.com.cn/product/iServer.aspx' target='_blank'>SuperMap iServer</a></span> ";

    map = new mapboxgl.Map({
        container: 'map',
        style: {
            "version": 8,
            "sources": {
                "raster-tiles": {
                    "attribution": attribution,
                    "type": "raster",
                    "tiles": [mapUrl],
                    "tileSize": 256
                }
            },
            "layers": [{
                "id": "simple-tiles",
                "type": "raster",
                "source": "raster-tiles",
            }]
        },
        center: [120.143, 30.236],
        zoom: 2
    });
    map.addControl(new mapboxgl.NavigationControl(), 'top-left');
    map.addControl(new mapboxgl.supermap.LogoControl(), 'bottom-right');
    featureService = new mapboxgl.supermap.FeatureService(dataUrl);

    var sourceFeatures = {},
        addPointFeaturesData = {
            "type": "FeatureCollection",
            "features": []
        };

    map.loadImage('../img/marker-icon.png', function (error, image) {
        if (error) throw error;
        map.addImage('positionPoint', image);
    });
    
    function addMarker(){
        map.addLayer({
            "id": "route",
            "type": "line",
            "source": {
                "type": "geojson",
                "data": {
                    "type": "Feature",
                    "properties": {},
                    "geometry": {
                        "type": "LineString",
                        "coordinates": [
                            [120.143, 30.236],
                            [120.343, 31.236]
                        ]
                    }
                }
            },
            "layout": {
                "line-join": "round",
                "line-cap": "round"
            },
            "paint": {
                "line-color": "#888",
                "line-width": 8
            }
        });
    }

    function clearLayer(){
        map.removeLayer('route')
    }

</script>
</body>
</html>

通过id删除图层后,想再次添加回来,会报异常,说id源已存在,怎么才能删除干净重复使用呢。

3 个回答

在clearlayer里面加一行代码就可以

function clearLayer(){
        map.removeLayer('route');
        map.removeSource("route");
    }

5,668EXP 2019年07月23日

你好,

removeSource

这个方法没有呀?

您好,这个问题解决了吗?
13EXP 2020年12月17日

用addSource定义source

map.addSource("routeData", {
						"type": "geojson",
						"data": "json数据"
				});

map.addLayer里 "source":"routeData"

在clearLayer() 里 填一句 map.removeLayer("routeData"); 

32EXP 2021年12月23日
...