简体   繁体   中英

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.

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"})]

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.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM