2014-04-16 28 views
0

我想代表geoJSON多邊形,我想給path轉換爲座標的array這樣的:獲得的latLng從多邊形的路徑發生故障

createCoordinates:function(path) 
    { 
     var res=[]; 
     var i=0; 
     console.log(path.getLength()); 
     for(i=0;i<path.getLength();i++) 
     { 
      res.push([path.getAt(i).lat(),path.getAt(i).lng()]); 
     } 
     console.log(res); 
     return res; 
    } 

    exportPolygon:function(poly){ 
     var res={}; 
     res.type="Polygon"; 
     console.log(polygon.getPath(poly)); 
     var cone=this.createCoordinates(polygon.getPath(poly)); 
     res.coordinates=cone; 
     return res; 
    } 

path看起來是這樣的:

 Ef {j: Array[181], gm_accessors_: Object, length: 181, gm_bindings_: Object, k: 
    function…} 
    A: function (b,c){Gd(a.A.ab,function(a,e){e(b,c)})} 
    D: function (b,c){Gd(a.D.ab,function(a,e){e(b,c)})} 
    gm_accessors_: Object 
    gm_bindings_: Object 
    j: Array[181] 
    k: function (b){Gd(a.k.ab,function(a,d){d(b)})} 
    length: 181 
    __proto__: c 

但我獲得的長度是0,因此結果是一個空數組。

回答

1

您有一個錯字。 polygon.getPath()應該是poly.getPath()。 (我希望javascript錯誤)。

exportPolygon:function(poly){ 
    var res={}; 
    res.type="Polygon"; 
    console.log(poly.getPath(poly)); 
    var cone=this.createCoordinates(poly.getPath(poly)); 
    res.coordinates=cone; 
    return res; 
}