也许很多时候,selectfeature只需要hover或者click就能达到选择要素的目的,但是当你遇到某些境况,要让你两者兼得,就苦逼了.....因为clickFeature与overFeature互斥
我是新手,在没看源码之前是苦憋了很久,那个痛苦。看完selectfeature源码,顿时有改的冲动,但是又怕影响其他人使用,最后选择了扩展.
以下是clickFeature:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
clickFeature: function(feature) { if(!this.hover) { var selected = (OpenLayers.Util.indexOf( feature.layer.selectedFeatures, feature) > -1); if(selected) { if(this.toggleSelect()) { this.unselect(feature); } else if(!this.multipleSelect()) { this.unselectAll({except: feature}); } } else { if(!this.multipleSelect()) { this.unselectAll({except: feature}); } this.select(feature); } } },
以下是overFeature:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
overFeature: function(feature) { var layer = feature.layer; if(this.hover) { if(this.highlightOnly) { this.highlight(feature); } else if(OpenLayers.Util.indexOf( layer.selectedFeatures, feature) == -1) { this.select(feature); } } },
以下是继承的SelectFeatureExtend
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 /** 2 * Class: OpenLayers.Control.SelectFeatureExtend 3 * 4 * Overwrite the function clickFeature,let it works whether this.hover is false or true 5 * 6 * Inherits from: 7 * - <OpenLayers.Control.SelectFeature> 8 */ 9 OpenLayers.Control.SelectFeatureExtend = OpenLayers.Class(OpenLayers.Control.SelectFeature, { 10 /** 11 * Method: clickFeature 12 * Called on click in a feature 13 * responds whether this.hover is false or true. 14 * 15 * Parameters: 16 * feature - {<OpenLayers.Feature.Vector>} 17 */ 18 clickFeature: function(feature) { 19 var selected = (OpenLayers.Util.indexOf( 20 feature.layer.selectedFeatures, feature) > -1); 21 if(selected) { 22 if(this.toggleSelect()) { 23 this.unselect(feature); 24 } else if(!this.multipleSelect()) { 25 this.unselectAll({except: feature}); 26 } 27 } else { 28 if(!this.multipleSelect()) { 29 this.unselectAll({except: feature}); 30 } 31 this.select(feature); 32 } 33 }, 34 CLASS_NAME: "OpenLayers.Strategy.SelectFeatureExtend" 35 });