我使用OpenLayer3创建了一个带矢量图层的地图。 我现在要做的是遍历矢量图层,获取坐标并将它们存储在数组中。
我试过这样的事情:
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],
});
答案 0 :(得分: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]);
}
答案 1 :(得分:1)
它不会改变最终结果,但可以使用forEachFeature
完成:
var source = shops_layer.getSource();
source.forEachFeature(function(feature){
var coord = feature.getGeometry().getCoordinates();
// ...
});