2015-11-26 55 views
1

我已經使用OpenLayer3創建了一個具有矢量圖的地圖。 我現在想要做的是迭代遍歷矢量圖層,獲取座標並將它們存儲在數組中。如何從openlayers3中的矢量圖層獲取點座標?

我想是這樣的:

var store = vectorLayer.getGeometry().getExtent(); 

,但我得到一個未定義功能的警告。

我也試着這樣做:

var source = layer.getSource(); 
var features = source.getFeatures(); 

但我也得到了未定義功能警告:

TypeError: Cannot read property 'getExtent' of undefined 
我的代碼

那一部分:

var url="http://localhost:8080/geoserver/wfs?&service=wfs&version=1.1.0&request=GetFeature&typeNames=dSpatialAnalysis:categoriesdata"; 
var shops_layer=new ol.layer.Vector({ 
    title: 'Shops', 
    source: new ol.source.Vector({ 
     url: '/cgi-bin/proxy.cgi?url='+ encodeURIComponent(url), 
     format: new ol.format.WFS({ 
     }) 
    }), 
    style: iconStyle 

}); 


map = new ol.Map({ 
    target:'map', 
    renderer:'canvas', 
    view: view, 
    layers: [newLayer, shops_layer], 
}); 

回答

1

它不會改變最終的結果,但可以用forEachFeature做到:

var source = shops_layer.getSource(); 
source.forEachFeature(function(feature){ 
    var coord = feature.getGeometry().getCoordinates(); 
    // ... 
}); 
+0

感謝。比我的更優雅。公認。 – user1919

1

這是怎麼了我畢竟解決了它:

// First access the source of the vectore layer 
    var source = shops_layer.getSource(); 
    // Get the features of the layer 
    var features = source.getFeatures(); 
    var feature; 
    // iterate through the array 
    for (var i = 0, ii = features.length; i < ii; ++i) { 
     feature = features[i]; 
     // get the geometry for each feature point 
     geometry = feature.getGeometry(); 
     // get the first coordinate 
     geometry_coords = geometry.getFirstCoordinate() 
     // assign them to two variables 
     geometry_coord_x = console.log(geometry_coords[0]); 
     geometry_coord_y = console.log(geometry_coords[1]); 

    } 
相關問題