在Bing Map上绘制多边形奇数行为setLocations

时间:2017-06-06 15:51:22

标签: javascript bing-maps

下面是我用来在Bing地图上绘制多边形的所有代码。我的问题是鼠标移动我试图删除多边形中的最后一个点,并将其替换为鼠标当前所在的位置。当发生单击时,会添加一个额外的点以完成所述点,并且它应该在单击时添加1点,并且在鼠标移动时应该是点中性(因为它弹出一次并推动一次)。我的问题是,由于某些原因,当我调用Polygon.setLocations(points)时,它会在内部向我的多边形添加一个额外的点。我在调用setLocations之前通过调试验证了我比调用setLocations后少了一点。控制台输出显示在代码下方以验证这一点。看起来像库中的一个bug,任何帮助都会被理解为如何防止这种情况,似乎只有在点数组大于长度1时才会发生,因为当我只有1点时它不会导致这种奇怪行为。

编辑:好吧想通了,它复制了最后的第一个元素,而不仅仅是内部引用自己,就像人们希望你必须修改倒数第二个索引一样,否则它会有这种行为。

    Microsoft.Maps.Events.addHandler(map, 'mousemove', PolygonDrawMouseMove);

    Microsoft.Maps.Events.addHandler(map, 'click', function (e) {
        HideInfobox();
        HideContextInfobox();
        ClearPinSelection();
        PolygonDrawClick(e);
    });

    Microsoft.Maps.Events.addHandler(map, 'rightclick', function () {
        HideInfobox();
        HideContextInfobox();
        ClearPinSelection();
    });

var Polygon = null;
var isDrawingMode = false;


function PolygonDrawClick(event) {

    if (isDrawingMode && Polygon !== null && Polygon instanceof Microsoft.Maps.Polygon)
    {
        var locations = Polygon.getLocations();
        var location = propertyMap.tryPixelToLocation(new Microsoft.Maps.Point(event.getX(), event.getY()));
        locations.push(location);
        Polygon.setLocations(locations);
    }
}

function PolygonDrawMouseMove(event)
{
    if (isDrawingMode && Polygon !== null && Polygon instanceof Microsoft.Maps.Polygon)
    {
        var points = Polygon.getLocations();

        if (points.length > 1)
        {
            console.log('start');
            console.log(points.length);
        }

        if (points.length > 0)
        {
            points.pop();
        }

        if (points.length > 1)
            console.log(points.length);

        var location = propertyMap.tryPixelToLocation(new Microsoft.Maps.Point(event.getX(), event.getY()));
        points.push(location);

        if (points.length > 1)
            console.log(points.length);

        Polygon.setLocations(points);

        if(points.length > 1)
        {
            console.log(Polygon.getLocations().length);
        }
    }
}

function DeletePolygons() {
    if (!propertyMap)
        return;

    for (var i = propertyMap.entities.getLength() - 1; i >= 0; i--) {
        var polygon = propertyMap.entities.get(i);
        if (polygon instanceof Microsoft.Maps.Polygon)
            propertyMap.entities.removeAt(i);
    }

    Polygon = null;
}

$("#compsMap").keyup(function (event) {

    //escape
    if (event.keyCode === 27 && isDrawingMode && Polygon !== null && Polygon instanceof Microsoft.Maps.Polygon)
    {
        isDrawingMode = false;
        var locations = Polygon.getLocations();
        if (locations.length > 0) locations.pop();

        if (locations.length === 0)
            DeletePolygons();
        else
            Polygon.setLocations(locations);
    }
    else if (event.keyCode === 32 && !isDrawingMode) //space
    {
        DeletePolygons();
        isDrawingMode = true;
        Polygon = new Microsoft.Maps.Polygon([new Microsoft.Maps.Location(0,0)], { fillColor: 'rgba(255,212,42,0.6)', strokeColor: '#000000', strokeThickness: 2 });
        propertyMap.entities.push(Polygon);
    }
});

[![screen shot of console output][1]][1]

1 个答案:

答案 0 :(得分:1)

这是设计的。多边形自动在V8中关闭它们的环以帮助它们有效。简单地引用自身是不正常的,也不会起作用。