I have created using OpenLayer3 a map with a vector layer. What I want to do now is to iterate through the vector layer, get the coordinates and store them in an array.
I tried something like this:
var store = vectorLayer.getGeometry().getExtent();
But I get an undefined function warning.
I also tried doing this:
var source = layer.getSource();
var features = source.getFeatures();
But I also get the undefined function warning:
TypeError: Cannot read property 'getExtent' of undefined
Thats part of my code:
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],
});
This is how I solved it after all:
// 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]);
}
It doesn't change the final result but can be done with forEachFeature
:
var source = shops_layer.getSource();
source.forEachFeature(function(feature){
var coord = feature.getGeometry().getCoordinates();
// ...
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.