【问题标题】:Unable to remove a polygon from Bing map无法从 Bing 地图中删除多边形
【发布时间】:2017-07-10 11:30:46
【问题描述】:

我的应用程序中有一个屏幕,我在其中使用 Bing 地图管理多边形和与多边形相关的信息。

我想在有人点击多边形时显示多边形详细信息。所以我在多边形上添加了一个点击事件,它工作正常。

我还希望允许用户在单击事件后查看/修改其他信息时编辑多边形。所以我计划在点击事件后从地图实体中删除多边形并将其添加到绘图管理器以进行进一步编辑。但是我无法从地图实体中删除特定的多边形。我尝试准备相同的多边形并将其传递给地图的实体删除功能,但它不起作用。以下是相同的示例代码,我的应用程序是用 Angular 开发的:

var map = new Microsoft.Maps.Map(document.getElementById('myMap'), {
    credentials: 'Your Bing Maps Key'
});

var polygon1 = new Microsoft.Maps.Polygon([
    new Microsoft.Maps.Location(center.latitude + 0.01, center.longitude - 0.03),
    new Microsoft.Maps.Location(center.latitude + 0.01, center.longitude - 0.11),
    new Microsoft.Maps.Location(center.latitude + 0.05, center.longitude - 0.07)
]);
polygon1.entity = { id: 1 };
map.entities.push(polygon1);

var polygon2 = new Microsoft.Maps.Polygon([
    new Microsoft.Maps.Location(center.latitude + 0.01, center.longitude + 0.03),
    new Microsoft.Maps.Location(center.latitude + 0.01, center.longitude + 0.11),
    new Microsoft.Maps.Location(center.latitude + 0.05, center.longitude + 0.07)
]);
polygon2.entity = { id: 2 };
map.entities.push(polygon2);    

Microsoft.Maps.Events.addHandler(polygon, 'click', function (e) {   
    var geoId = e.target.entity.id;
    //Here I want to find the polygon with provided Id and remove it so that I can add it to drawing manager for further modification
    //Code to remove the polygon
    var polygonToRemove = new Microsoft.Maps.Polygon([
        new Microsoft.Maps.Location(center.latitude + 0.01, center.longitude - 0.03),
        new Microsoft.Maps.Location(center.latitude + 0.01, center.longitude - 0.11),
        new Microsoft.Maps.Location(center.latitude + 0.05, center.longitude - 0.07)
    ]);
    polygonToRemove.entity = { id: 1 };
    map.entities.remove(polygonToRemove);
    //Code to add the polygon to drawing manager
});

【问题讨论】:

    标签: javascript bing-maps


    【解决方案1】:

    在您的单击事件处理程序中,您正在创建一个新多边形,它可能与另一个多边形具有相同的位置,但仍然是一个不同的对象。您必须传入要删除的实际形状对象的实例,而不是它的副本。这是您的代码的修改版本:

    Microsoft.Maps.Events.addHandler(polygon, 'click', function (e) {   
        map.entities.remove(e.target);
    });
    

    【讨论】:

    • 谢谢您。我现在可以删除多边形。也只是想知道,如果我想使用 entity.id 从 entitycollection 中找到实体/多边形,然后将其删除。我该怎么做?
    • 循环遍历集合中的所有形状并检查属性值,直到找到您要查找的形状,然后使用集合的删除功能并跳出循环。不要将实体属性添加到形状,而是使用元数据属性。 Bing Maps API 专门为开发人员保留了元数据属性名称,以便他们可以将自定义元数据(例如 id)添加到形状中,而 Bing 地图团队不会冒使用此名称和破坏应用程序的风险。实体属性名称由 Bing 地图团队使用,将来使用它可能会出现问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-22
    • 2019-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-22
    相关资源
    最近更新 更多