2012-02-08 148 views
0

我在google maps api上有點初學者。我設法讓用戶在地圖上繪製多邊形,然後我想獲取繪製的多邊形上的座標。谷歌地圖api v2獲取多邊形座標

我已經使用下面的代碼段,但它給我的下面的錯誤未捕獲的類型錯誤:對象[對象的對象]無方法「的getPath」

這是我用

function startShape() { 


    initialize(); 
    document.getElementById('lat').disabled = true; 
    document.getElementById('lng').disabled = true; 
    var polygon = new GPolygon([],"ff0000", 2, 0.7,"ff0000",0.2); 
    startDrawing(polygon, "Shape " + (++shapeCounter_), function() { 
    var cell = this; 
    var area = polygon.getArea(); 
    cell.innerHTML = (Math.round(area/10000)/100) + "km<sup>2</sup>"; 
    }); 
    showcoor(polygon); 
} 




function startDrawing(poly, name, onUpdate) { 

map.addOverlay(poly); 
    poly.enableDrawing(options); 
    poly.enableEditing({onEvent: "mouseover"}); 
    poly.disableEditing({onEvent: "mouseout"}); 
    GEvent.addListener(poly, "endline", function() { 
    //var cells = addFeatureEntry(name, color); 
    //GEvent.bind(poly, "lineupdated", cells.desc, onUpdate); 
    GEvent.addListener(poly, "click", function(latlng, index) { 
     if (typeof index == "number") { 
     poly.deleteVertex(index); 
     } 
    }); 
    }); 


} 


function showcoor (poly) { 
GEvent.addListener(poly, "endline", function() { 
    GEvent.addListener(poly, "click", function() { 
    var str; 
       var vertices = this.getPath(); 
       for (var i =0; i < vertices.length; i++) { 
       var xy = vertices.getAt(i); 
       str += xy.lat() +"," + xy.lng()+"<br />"; 
       } 
       alert (str); 
      }); 
      }); 
    } 
代碼
+0

你說你在使用V3,但你的代碼看起來可疑V2ish(GPolygon,GEvent)。你有沒有發佈你的所有代碼,我們可以看看?也許在JSFiddle上? – 2012-02-08 20:59:00

回答

2

GPolygon對象沒有getPath方法。請參閱GPolygon reference。您需要使用getVertexCount()getVertex(i)

for (var i = 0, I = this.getVertexCount(); i < I; ++i) { 
    var xy = this.getVertex(i); 
    str += xy.lat() + ', ' + xy.lng() + '<br />'; 
}