[OpenLayers-Users] Specifying an ID when creating a vector feature

Eric Lemoine eric.lemoine at camptocamp.com
Wed May 30 13:28:22 PDT 2012


On Wed, May 30, 2012 at 9:37 PM, Spencer Renosis <renosis at gmail.com> wrote:
> Hi,
>
> I am trying to figure out how to give features I create a custom ID. For
> example:
>
> var feature_point = new OpenLayers.Feature.Vector(new
> OpenLayers.Geometry.Point(-81, 26));
>
> map.layers[1].addFeatures([feature_point]);
>
> When I do this, I end up with an id of "OpenLayers.Feature.Vector_92" or
> whatever random number it chooses. What I want to know is, how do I give the
> feature, its' own custom id during creation?

Why do you want to do that?


You can do:

var f = new OpenLayers.Feature.Vector();
f.id = your_id;

but make sure you don't assign the same id to different features, or
weird things will happen, if you use a select feature control in
particular.

And if you want to assign a new id to the feature you'll have to
redraw it after assignment:

f.id = your_new_id;
layer.drawFeature(f);

But again, you have to have good reasons for assigning custom ids to
features. Hence my initial question.


-- 
Eric Lemoine

Camptocamp France SAS
Savoie Technolac, BP 352
73377 Le Bourget du Lac, Cedex

Tel : 00 33 4 79 44 44 96
Mail : eric.lemoine at camptocamp.com
http://www.camptocamp.com


More information about the Users mailing list