Chase R Lewis
Chase R Lewis

Reputation: 2307

Drawing Polygon On Bing Map Odd Behaviour setLocations

Below is all the code I'm using to draw a polygon on my Bing maps. My issue is on Mouse Move I'm trying to delete the last point in the polygon and replace it with where the mouse currently is. When a click occurs an additional point is added to finalize said point and it should just add 1 point when clicked and should be point neutral on mouse move (since it pops once and pushes once). My issue is that for some reason when I call Polygon.setLocations(points) it internally appends an additional point to my polygon. I've verified via debugging before the call to setLocations I have 1 less point than I do after the call to setLocations. Console output is shown below the code to verify this. Seems like a bug in the library, any help would be appreciated to figure out how to prevent this, it seems to only happen if the array of points is greater than length 1 as when I have only 1 point it doesn't cause this weird behavior.

Edit: Alright figured it out, it duplicates the first element at the end rather than just internally referencing itself like one would expect you have to modify the second to last index or else it has this behaviour.

    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]

Upvotes: 0

Views: 244

Answers (1)

rbrundritt
rbrundritt

Reputation: 17954

This is by design. Polygons automatically close their rings in V8 to help make them valid. It's not normal to simply reference itself, nor would it work.

Upvotes: 1

Related Questions