2017-06-06 99 views
0

以下是我用於在Bing地圖上繪製多邊形的所有代碼。我的問題是鼠標移動我試圖刪除多邊形中的最後一個點,並將其替換爲鼠標當前的位置。當點擊發生時,添加一個額外的點來完成所述點,並且它應該在點擊時加1點,並且在鼠標移動時應該是點中立(因爲它彈出一次並且推一次)。我的問題是,由於某種原因,當我調用Polygon.setLocations(點)時,它會在內部附加一個點到我的多邊形。在調用setLocations之前,我已經通過調試進行了驗證,在調用setLocations之後,我比我少了1個點。控制檯輸出顯示在代碼下面以驗證這一點。看起來像庫中的一個bug,任何幫助將不勝感激找出如何防止這種情況,它似乎只發生在點的數組大於長度1時,因爲當我只有1點時它不會引起這個怪異行爲。在Bing地圖上繪製多邊形奇數行爲setLocations

編輯:好吧,它重複了第一個元素在最後,而不是像內部引用自己一樣,期望你必須修改倒數第二個索引,否則它有這種行爲。

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

這是設計。多邊形在V8中自動關閉它們的環以幫助使它們有效。簡單地引用它本身並不是正常的,它也不會起作用。