Javascript 在Bing地图上绘制多边形位置
下面是我用来在Bing地图上绘制多边形的所有代码。我的问题是鼠标移动,我试图删除多边形中的最后一点,并将其替换为鼠标当前所在的位置。当点击发生时,会添加一个附加点以最终确定所述点,点击时只应添加1个点,并且在鼠标移动时应为中性点,因为它会弹出一次并按下一次。我的问题是,出于某种原因,当我调用Polygon.setLocationspoints时,它会在多边形内部附加一个点。在调用setLocations之前,我已经通过调试验证了我比调用setLocations之后少了1分。控制台输出显示在验证这一点的代码下面。看起来像是库中的一个bug,如果有人能帮我解决这个问题,我们将不胜感激。似乎只有当点数组的长度大于1时才会发生这种情况,因为当我只有1个点时,它不会导致这种奇怪的行为 编辑:好吧,我想出来了,它在结尾复制了第一个元素,而不是像人们期望的那样在内部引用它自己,你必须修改从第二个到最后一个的索引,否则它就会有这种行为Javascript 在Bing地图上绘制多边形位置,javascript,bing-maps,Javascript,Bing Maps,下面是我用来在Bing地图上绘制多边形的所有代码。我的问题是鼠标移动,我试图删除多边形中的最后一点,并将其替换为鼠标当前所在的位置。当点击发生时,会添加一个附加点以最终确定所述点,点击时只应添加1个点,并且在鼠标移动时应为中性点,因为它会弹出一次并按下一次。我的问题是,出于某种原因,当我调用Polygon.setLocationspoints时,它会在多边形内部附加一个点。在调用setLocations之前,我已经通过调试验证了我比调用setLocations之后少了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]
这是故意的。多边形在V8中自动闭合其环,以帮助使其有效。简单地引用自己是不正常的,它也不起作用