+ _allFeatures: function() {
+ var layer = this;
+ return $.map(layer.olLayer.features, function(feature) {
+ return new $.MapQuery.Feature(layer, {olFeature: feature});
+ });
+ },
+ _addFeature: function(options) {
+ var feature = new $.MapQuery.Feature(this, options);
+ // NOTE vmx 2012-04-19: Not sure if this is a good idea, or if it would
+ // be better to include `options` with the preaddfeature event
+ if (this._triggerReturn('preaddfeature', [feature])===false) {
+ return false;
+ }
+ this.olLayer.addFeatures(feature.olFeature);
+ this.trigger('addfeature', [feature]);
+ return feature;
+ }
+};
+
+/**
+#MapQuery.Feature
+
+The MapQuery.Feature object. It is constructed with a feature options object
+in the layer.`features([options])` function. The Feautre object is refered to
+as _feature_ in the documentation.
+
+TODO vmx 20110905: Support other geometry types than GeoJSON
+options:
+ * geometry: A GeoJSON geometry
+ * properties: Properties for the feature
+*/
+// Not in the pulic API docs: You can pass in as options:
+// * olFeature: This will wrap the olFeature in a MapQuery feature
+$.MapQuery.Feature = function(layer, options) {
+ // The ID is the
+ this._id = layer.map._createId();
+ this.layer = layer;
+
+ // Feature already exists on the layer, it just needs to be wrapped
+ // to an MapQuery feature
+ if (options.olFeature) {
+ this.olFeature = options.olFeature;
+ }
+ else {
+ // XXX vmx 20110905: Different feature types might make sense:
+ // (Geo)JSON, KML, WKT
+ // vmx 2012-04-14: I changed my mind quite some time ago. We should onlu
+ // support GeoJSON and let the user easily transfrom their format
+ // (e.g. KML) to GeoJSON, before they add a feature to the layer
+ var GeoJSON = new OpenLayers.Format.GeoJSON();
+ var geometry = GeoJSON.parseGeometry(options.geometry);
+ geometry.transform(
+ new OpenLayers.Projection(this.layer.map.displaProjection),
+ new OpenLayers.Projection(this.layer.map.projection));
+
+ this.olFeature = new OpenLayers.Feature.Vector(geometry,
+ options.properties);
+ }
+
+ // Modify the features to be more practical
+ // e.g. copy properties that should be easily accessed from the
+ // outside, out of the olLayer and to the feature level
+ this.properties = $.extend(true, {}, this.olFeature.attributes);
+ this.geometry = $.parseJSON(
+ new OpenLayers.Format.GeoJSON().write(this.olFeature.geometry));
+
+ return this;
+};
+
+$.MapQuery.Feature.prototype = {
+/**
+###*feature*.`remove()`
+_version added 0.2.0_
+####**Description**: remove the feature from the layer
+
+>Returns: layer (layer) or false
+
+
+The `.remove()` method allows us to remove a feature from the layer.
+It returns the `layer` object if the feature was removed, or `false` if the
+removal was prevented in the preremovefeature event.
+
+ // add a feature to a layer
+ var feature = layer.features({geometry: {type: "Point", coordinates: [5.3, 7.4]}});
+ // remove the feature again
+ feature.remove();
+*/
+ remove: function() {
+ if (this.layer._triggerReturn('preremovefeature', [this])===false) {
+ return false;
+ }
+ this.layer.olLayer.removeFeatures(this.olFeature);
+ // The `removefeature` event is triggered by an OpenLayes event handler
+ return this.layer;
+ },
+/**
+###*feature*.`select(exclusive)`
+_version added 0.2.0_
+####**Description**: select a feature
+
+**exclusive** (boolean, default: true) True means that all other features get
+deselectd
+
+>Returns: layer (layer)
+
+
+The `.select()` method allows us to select a feature from the layer.
+A `featureselected` will be fired.
+
+ // add a feature to a layer
+ var feature = layer.features({geometry: {type: "Point", coordinates: [5.3, 7.4]}});
+ // select the feature again
+ feature.select();
+*/
+ select: function(exclusive) {
+ if (exclusive===undefined || exclusive===true) {
+ this.layer.map.selectFeatureControl.unselectAll();
+ }
+ this.layer.map.selectFeatureControl.select(this.olFeature);
+ },
+/**
+###*feature*.`unselect()`
+_version added 0.2.0_
+####**Description**: unselect a feature
+
+>Returns: layer (layer)
+
+
+The `.unselect()` method allows us to unselect a feature from the layer.
+A `featureunselected` will be fired.
+
+ // add a feature to a layer
+ var feature = layer.features({geometry: {type: "Point", coordinates: [5.3, 7.4]}});
+ // select the feature
+ feature.select();
+ // unselect the feature again
+ feature.unselect();
+*/
+ unselect: function() {
+ this.layer.map.selectFeatureControl.unselect(this.olFeature);