vogomatix
vogomatix

Reputation: 5071

Openlayers 2, specifying DOM Id for a Geometry/Feature?

Is there any way I can specify/set the DOM Id of an OpenLayers Geometry Point object i.e. call it "myID" instead of "OpenLayers_Geometry_Point_nnn"?

As far as I can tell, in Openlayers 2.13, creating a OpenLayers.Geometry.Point object does not accept any form of DOM id attribute, and creates its own unique ID through `OpenLayers.Util.createUniqueID. The OpenLayers.Geometry.Point.initialize 'constructor' only accepts X and Y values, not additional info.

I'd really like to use Selenium to verify/manipulate certain objects are on a map, and having predictable DOM Ids seems the be best way to do this.

Upvotes: 2

Views: 184

Answers (1)

vogomatix
vogomatix

Reputation: 5071

One "solution" I have implemented is to replace OpenLayers.Util.createUniqueID whilst I am creating the objects I wish to test.

// replace OpenLayer Dom ID generation for this layer
var olPrefix = "MyPrefix";
var olCount = 0;
var old = OpenLayers.Util.createUniqueID;
OpenLayers.Util.createUniqueID = function(prefix) {
  if (prefix.search( '^OpenLayers.Geometry') >= 0) {
    // special Dom IDs for Geometry nodes only...
    olCount++;
    return olPrefix + "_" + olCount;
  }
  // default to using the previous ID generator...
  return( old( prefix));
}

var geoJSON = {
   "type": "FeatureCollection",
   "features": [
        { 
          "type":"Feature",
           "geometry": { "type":"Point","coordinates":[ 1.0, 52.0]},
           "properties":{ "myProperty":"myValue" }
        },
        // more features here
   ]
};

var geoformat = new OpenLayers.Format.GeoJSON();
var features = geoformat.read( geoJSON);

// finished creating your features, so put the "old" ID routine back...
OpenLayers.Util.createUniqueID = old;

Another possibility is to replace OpenLayers.Geometry.initialize in some way

Upvotes: 1

Related Questions