简体   繁体   English

Bing Maps Api清除图层

[英]Bing Maps Api clear a layer

I have a page that works well it loads a Bing Map and creates a layer which is then filled with Polygons. 我有一个工作得很好的页面,它可以加载Bing地图并创建一个图层,然后将其填充为多边形。 I then need to reload the JSON data that makes the polygons and this again works, however the data is then added to another layer so appears over the top. 然后,我需要重新加载构成多边形的JSON数据,这再次起作用,但是该数据随后被添加到另一层,因此显示在顶部。 I have tried to delete the layer, clear the layer etc etc but nothing seems to work. 我试图删除该层,清除该层等,但似乎没有任何作用。

Any idea please. 任何想法,请。

This is the function that does it all... 这是完成所有任务的功能...

        function AddData() {

        dataLayer = new Microsoft.Maps.Layer();

        Microsoft.Maps.loadModule('Microsoft.Maps.GeoJson', function () {
            var featureCollection = Microsoft.Maps.GeoJson.read(json, {
                polygonOptions: {
                    strokeColor: 'LightSkyBlue',
                    strokeThickness: 2
                }
            });

            for (var i = 0; i < featureCollection.length; i++) {
                var fillColour = featureCollection[i].metadata.FillColor;
                featureCollection[i].setOptions({ fillColor: fillColour });

                Microsoft.Maps.Events.addHandler(featureCollection[i], 'click', displayClickBox);
                Microsoft.Maps.Events.addHandler(featureCollection[i], 'mouseover', displayMouseOverBox);
                Microsoft.Maps.Events.addHandler(featureCollection[i],'mouseout', displayMouseOut);
                dataLayer.add(featureCollection[i], 0);
            }
            map.layers.insert(dataLayer);
        });
    }

        var getJson = function () {
        var onContentComplete = function (response) {
            //Load the JSON data into the local variable for use latter in the project...
            json = response.data;
            //load the map now that we have the polygon data...
            AddData();
        };

        var onError = function (reason) {
            //An error has occured so display a message to the user...
            $scope.error = "Server communication error please try again...";
            //Log the error to the console for admin debug...
            console.log(reason.data);
        };

        //Load the JSON for the map polygons into memory ready for display...
        $http.get("../JSON/MapData.json")
            .then(onContentComplete, onError);
    }

As I have said I have tried to clear the layer first using 如我所说,我尝试先使用

dataLayer.clear();

But that seems to do nothing. 但这似乎无济于事。

Help please as I have been working at this for hours now. 请帮忙,因为我已经工作了几个小时了。

Thanks 谢谢

Cliff. 悬崖。

By the sounds of things you want all data to render in a single layer, but instead it is rendering two or more layers. 通过声音,您希望所有数据都在单个层中呈现,而实际上是在呈现两个或更多层。 This happens because you are creating a new instance of dataLayer every time the AddData function is called. 发生这种情况是因为每次调用AddData函数时都在创建一个dataLayer的新实例。 Additionally, this overwrites the local variable for dataLayer and thus you lose the reference to the original layer that you are trying to clear/delete. 此外,这将覆盖dataLayer的局部变量,因此您将丢失对尝试清除/删除的原始图层的引用。 If you want to clear or delete the layer, do that before initializing the new layer. 如果要清除或删除该层,请在初始化新层之前执行此操作。 Try adding the following to the top of your AddData function: 尝试将以下内容添加到AddData函数的顶部:

if(dataLayer){
   map.layers.remove(dataLayer);
}

Alternatively, reuse the layer by clearing it if it exists of or creating it if it doesn't: 或者,通过清除该层(如果存在)或不创建它来重用该层:

if(dataLayer){
    dataLayer.clear();
}else{
    dataLayer = new Microsoft.Maps.Layer();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM