如何使用 pure javascript 清除 Microsoft VirtualEarth 6.3 中的整个形状层?

How to Clear entire Shape Layer in Microsoft VirtualEarth 6.3 using pure javascript?

下面显示的代码片段 (HTML5/javascript) 加载 Microsoft Bing Map (VirtualEarth 6.3),然后添加两个形状层和两个示例图钉,每层一个。

问题:要使用什么纯 javascript 函数(或解决方案)来清除第一层的所有图钉,同时保持另一层完好无损? 注意:清除图层功能需要在不删除整个图层的情况下实现。

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Bing VE Map w/layers</title>
    <script type="text/javascript" src="http://ecn.dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=6.3"></script>
    <script type="text/javascript">

        // VE map
        var _map;
        // 1st shape layer
        var _layer1 ;
        // 2nd shape layer
        var _layer2;

        function MapLoad() {

            // load map
            _map = new VEMap('Map');
            _map.LoadMap();

            // center point (Columbus Circle NY)
            var _center = new VELatLong(40.7681, -73.9819);

            // set center point and initial zoom level
            _map.SetCenterAndZoom(_center, 12);

            // set Map style
            _map.SetMapStyle(VEMapStyle.Shaded);

            // add 1st shape layer1 to Map obj
            _layer1 = new VEShapeLayer()
            _map.AddShapeLayer(_layer1);

            // add 2nd shape layer2 to Map obj
            _layer2 = new VEShapeLayer()
            _map.AddShapeLayer(_layer2);

            // add pushpin to layer1
            var _pushpin1 = new VEShape(VEShapeType.Pushpin, _center);
             _layer1.AddShape(_pushpin1);

            // add pushpin (Apple Store on 5th) to layer2
             var _pushpin2 = new VEShape(VEShapeType.Pushpin, new VELatLong(40.7639, -73.9725));
             _layer2.AddShape(_pushpin2);


            // QUESTION: HOW TO CLEAR ALL SHAPES FROM LAYER 1, (BUT NOT DELETING THE ENTIRE LAYER)?
        }
    </script>

</head>
<body onload="MapLoad();">
    <div id="Map" style="position:absolute; top:100px; height:90%; width:100%;"></div>
</body>
</html>

您可以使用 VEShapeLayer class 的专用方法 DeleteAllShapes(),请参阅 MSDN:

https://msdn.microsoft.com/en-us/library/bb412473.aspx

查看示例:

// Delete all shapes within the layer at the selected index.
function DeleteAllShapes()
{
  layer = map.GetShapeLayerByIndex(selIndex);
  layer.DeleteAllShapes();
  IndexChanged(selIndex);
}

此外,我建议使用 AJAX v7.0 而不是 v6.3,因为它现在已被弃用并且将于 2016 年 11 月 30 日停止支持。