user1831985
user1831985

Reputation: 11

Open layers features

Why cant I add feature to Vector? This code is not working:

var features = [new OpenLayers.Feature.Vector(new OpenLayers.Geometry.Point(-70.702451, 42.374473), {className: "latarnia"})]

vectors = new OpenLayers.Layer.Vector("warstwa", {
strategies: [new OpenLayers.Strategy.Fixed()],
 protocol: new OpenLayers.Protocol.HTTP({
format: new OpenLayers.Format.OSM()
 }),
 features : features,
projection: new OpenLayers.Projection("EPSG:4326")});   

map.addLayers([vectors]);

I mean vectors has no features at all. I tried

layer.addFeatures([feature]);

but it fails as well.

Upvotes: 1

Views: 212

Answers (2)

Hasan Ramezani
Hasan Ramezani

Reputation: 5194

it seems that the projection of your map and point is not the same. map peojection is EPSG:4326 , but it seems that point projection is EPSG:3857.

it may help you

conv_latlon = new OpenLayers.LonLat(-70.702451, 42.374473).transform('EPSG:3857', 'EPSG:4326')//transform point 
var features = [new OpenLayers.Feature.Vector(new OpenLayers.Geometry.Point(conv_latlon.lon, conv_latlon.lat), {className: "latarnia"})]

Upvotes: 1

George
George

Reputation: 201

For some reason initializing "features" property on OpenLayers.Layer.Vector constructor does not work.

But you should be able to add them afterwards:

vectors.addFeatures(features);

You can then test in browser console:

vectors.features.length; //this should be 1 now

Otherwise the code seems ok. You should also be able to see the feature on map as an orange circle (default style), but only if the point's coordinates are inside the extent of your base layer. Tested with OpenLayers version 2.14.

Upvotes: 0

Related Questions