Vector.js
34.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
/* Copyright (c) 2006-2011 by OpenLayers Contributors (see authors.txt for
* full list of contributors). Published under the Clear BSD license.
* See http://svn.openlayers.org/trunk/openlayers/license.txt for the
* full text of the license. */
/**
* @requires OpenLayers/Layer.js
* @requires OpenLayers/Renderer.js
* @requires OpenLayers/StyleMap.js
* @requires OpenLayers/Feature/Vector.js
* @requires OpenLayers/Console.js
* @requires OpenLayers/Lang.js
*/
/**
* Class: OpenLayers.Layer.Vector
* Instances of OpenLayers.Layer.Vector are used to render vector data from
* a variety of sources. Create a new vector layer with the
* <OpenLayers.Layer.Vector> constructor.
*
* Inherits from:
* - <OpenLayers.Layer>
*/
OpenLayers.Layer.Vector = OpenLayers.Class(OpenLayers.Layer, {
/**
* Constant: EVENT_TYPES
* {Array(String)} Supported application event types. Register a listener
* for a particular event with the following syntax:
* (code)
* layer.events.register(type, obj, listener);
* (end)
*
* Listeners will be called with a reference to an event object. The
* properties of this event depends on exactly what happened.
*
* All event objects have at least the following properties:
* object - {Object} A reference to layer.events.object.
* element - {DOMElement} A reference to layer.events.element.
*
* Supported map event types (in addition to those from <OpenLayers.Layer>):
* beforefeatureadded - Triggered before a feature is added. Listeners
* will receive an object with a *feature* property referencing the
* feature to be added. To stop the feature from being added, a
* listener should return false.
* beforefeaturesadded - Triggered before an array of features is added.
* Listeners will receive an object with a *features* property
* referencing the feature to be added. To stop the features from
* being added, a listener should return false.
* featureadded - Triggered after a feature is added. The event
* object passed to listeners will have a *feature* property with a
* reference to the added feature.
* featuresadded - Triggered after features are added. The event
* object passed to listeners will have a *features* property with a
* reference to an array of added features.
* beforefeatureremoved - Triggered before a feature is removed. Listeners
* will receive an object with a *feature* property referencing the
* feature to be removed.
* beforefeaturesremoved - Triggered before multiple features are removed.
* Listeners will receive an object with a *features* property
* referencing the features to be removed.
* featureremoved - Triggerd after a feature is removed. The event
* object passed to listeners will have a *feature* property with a
* reference to the removed feature.
* featuresremoved - Triggered after features are removed. The event
* object passed to listeners will have a *features* property with a
* reference to an array of removed features.
* beforefeatureselected - Triggered after a feature is selected. Listeners
* will receive an object with a *feature* property referencing the
* feature to be selected. To stop the feature from being selectd, a
* listener should return false.
* featureselected - Triggered after a feature is selected. Listeners
* will receive an object with a *feature* property referencing the
* selected feature.
* featureunselected - Triggered after a feature is unselected.
* Listeners will receive an object with a *feature* property
* referencing the unselected feature.
* beforefeaturemodified - Triggered when a feature is selected to
* be modified. Listeners will receive an object with a *feature*
* property referencing the selected feature.
* featuremodified - Triggered when a feature has been modified.
* Listeners will receive an object with a *feature* property referencing
* the modified feature.
* afterfeaturemodified - Triggered when a feature is finished being modified.
* Listeners will receive an object with a *feature* property referencing
* the modified feature.
* vertexmodified - Triggered when a vertex within any feature geometry
* has been modified. Listeners will receive an object with a
* *feature* property referencing the modified feature, a *vertex*
* property referencing the vertex modified (always a point geometry),
* and a *pixel* property referencing the pixel location of the
* modification.
* vertexremoved - Triggered when a vertex within any feature geometry
* has been deleted. Listeners will receive an object with a
* *feature* property referencing the modified feature, a *vertex*
* property referencing the vertex modified (always a point geometry),
* and a *pixel* property referencing the pixel location of the
* removal.
* sketchstarted - Triggered when a feature sketch bound for this layer
* is started. Listeners will receive an object with a *feature*
* property referencing the new sketch feature and a *vertex* property
* referencing the creation point.
* sketchmodified - Triggered when a feature sketch bound for this layer
* is modified. Listeners will receive an object with a *vertex*
* property referencing the modified vertex and a *feature* property
* referencing the sketch feature.
* sketchcomplete - Triggered when a feature sketch bound for this layer
* is complete. Listeners will receive an object with a *feature*
* property referencing the sketch feature. By returning false, a
* listener can stop the sketch feature from being added to the layer.
* refresh - Triggered when something wants a strategy to ask the protocol
* for a new set of features.
*/
EVENT_TYPES: ["beforefeatureadded", "beforefeaturesadded",
"featureadded", "featuresadded", "beforefeatureremoved",
"beforefeaturesremoved", "featureremoved", "featuresremoved",
"beforefeatureselected", "featureselected", "featureunselected",
"beforefeaturemodified", "featuremodified", "afterfeaturemodified",
"vertexmodified", "vertexremoved", "sketchstarted",
"sketchmodified", "sketchcomplete", "refresh"],
/**
* APIProperty: isBaseLayer
* {Boolean} The layer is a base layer. Default is false. Set this property
* in the layer options.
*/
isBaseLayer: false,
/**
* APIProperty: isFixed
* {Boolean} Whether the layer remains in one place while dragging the
* map.
*/
isFixed: false,
/**
* APIProperty: features
* {Array(<OpenLayers.Feature.Vector>)}
*/
features: null,
/**
* Property: filter
* {<OpenLayers.Filter>} The filter set in this layer,
* a strategy launching read requests can combined
* this filter with its own filter.
*/
filter: null,
/**
* Property: selectedFeatures
* {Array(<OpenLayers.Feature.Vector>)}
*/
selectedFeatures: null,
/**
* Property: unrenderedFeatures
* {Object} hash of features, keyed by feature.id, that the renderer
* failed to draw
*/
unrenderedFeatures: null,
/**
* APIProperty: reportError
* {Boolean} report friendly error message when loading of renderer
* fails.
*/
reportError: true,
/**
* APIProperty: style
* {Object} Default style for the layer
*/
style: null,
/**
* Property: styleMap
* {<OpenLayers.StyleMap>}
*/
styleMap: null,
/**
* Property: strategies
* {Array(<OpenLayers.Strategy>})} Optional list of strategies for the layer.
*/
strategies: null,
/**
* Property: protocol
* {<OpenLayers.Protocol>} Optional protocol for the layer.
*/
protocol: null,
/**
* Property: renderers
* {Array(String)} List of supported Renderer classes. Add to this list to
* add support for additional renderers. This list is ordered:
* the first renderer which returns true for the 'supported()'
* method will be used, if not defined in the 'renderer' option.
*/
renderers: ['SVG', 'VML', 'Canvas'],
/**
* Property: renderer
* {<OpenLayers.Renderer>}
*/
renderer: null,
/**
* APIProperty: rendererOptions
* {Object} Options for the renderer. See {<OpenLayers.Renderer>} for
* supported options.
*/
rendererOptions: null,
/**
* APIProperty: geometryType
* {String} geometryType allows you to limit the types of geometries this
* layer supports. This should be set to something like
* "OpenLayers.Geometry.Point" to limit types.
*/
geometryType: null,
/**
* Property: drawn
* {Boolean} Whether the Vector Layer features have been drawn yet.
*/
drawn: false,
/**
* Constructor: OpenLayers.Layer.Vector
* Create a new vector layer
*
* Parameters:
* name - {String} A name for the layer
* options - {Object} Optional object with non-default properties to set on
* the layer.
*
* Returns:
* {<OpenLayers.Layer.Vector>} A new vector layer
*/
initialize: function(name, options) {
// concatenate events specific to vector with those from the base
this.EVENT_TYPES =
OpenLayers.Layer.Vector.prototype.EVENT_TYPES.concat(
OpenLayers.Layer.prototype.EVENT_TYPES
);
OpenLayers.Layer.prototype.initialize.apply(this, arguments);
// allow user-set renderer, otherwise assign one
if (!this.renderer || !this.renderer.supported()) {
this.assignRenderer();
}
// if no valid renderer found, display error
if (!this.renderer || !this.renderer.supported()) {
this.renderer = null;
this.displayError();
}
if (!this.styleMap) {
this.styleMap = new OpenLayers.StyleMap();
}
this.features = [];
this.selectedFeatures = [];
this.unrenderedFeatures = {};
// Allow for custom layer behavior
if(this.strategies){
for(var i=0, len=this.strategies.length; i<len; i++) {
this.strategies[i].setLayer(this);
}
}
},
/**
* APIMethod: destroy
* Destroy this layer
*/
destroy: function() {
if (this.strategies) {
var strategy, i, len;
for(i=0, len=this.strategies.length; i<len; i++) {
strategy = this.strategies[i];
if(strategy.autoDestroy) {
strategy.destroy();
}
}
this.strategies = null;
}
if (this.protocol) {
if(this.protocol.autoDestroy) {
this.protocol.destroy();
}
this.protocol = null;
}
this.destroyFeatures();
this.features = null;
this.selectedFeatures = null;
this.unrenderedFeatures = null;
if (this.renderer) {
this.renderer.destroy();
}
this.renderer = null;
this.geometryType = null;
this.drawn = null;
OpenLayers.Layer.prototype.destroy.apply(this, arguments);
},
/**
* Method: clone
* Create a clone of this layer.
*
* Note: Features of the layer are also cloned.
*
* Returns:
* {<OpenLayers.Layer.Vector>} An exact clone of this layer
*/
clone: function (obj) {
if (obj == null) {
obj = new OpenLayers.Layer.Vector(this.name, this.getOptions());
}
//get all additions from superclasses
obj = OpenLayers.Layer.prototype.clone.apply(this, [obj]);
// copy/set any non-init, non-simple values here
var features = this.features;
var len = features.length;
var clonedFeatures = new Array(len);
for(var i=0; i<len; ++i) {
clonedFeatures[i] = features[i].clone();
}
obj.features = clonedFeatures;
return obj;
},
/**
* Method: refresh
* Ask the layer to request features again and redraw them. Triggers
* the refresh event if the layer is in range and visible.
*
* Parameters:
* obj - {Object} Optional object with properties for any listener of
* the refresh event.
*/
refresh: function(obj) {
if(this.calculateInRange() && this.visibility) {
this.events.triggerEvent("refresh", obj);
}
},
/**
* Method: assignRenderer
* Iterates through the available renderer implementations and selects
* and assigns the first one whose "supported()" function returns true.
*/
assignRenderer: function() {
for (var i=0, len=this.renderers.length; i<len; i++) {
var rendererClass = this.renderers[i];
var renderer = (typeof rendererClass == "function") ?
rendererClass :
OpenLayers.Renderer[rendererClass];
if (renderer && renderer.prototype.supported()) {
this.renderer = new renderer(this.div, this.rendererOptions);
break;
}
}
},
/**
* Method: displayError
* Let the user know their browser isn't supported.
*/
displayError: function() {
if (this.reportError) {
OpenLayers.Console.userError(OpenLayers.i18n("browserNotSupported",
{'renderers':this.renderers.join("\n")}));
}
},
/**
* Method: setMap
* The layer has been added to the map.
*
* If there is no renderer set, the layer can't be used. Remove it.
* Otherwise, give the renderer a reference to the map and set its size.
*
* Parameters:
* map - {<OpenLayers.Map>}
*/
setMap: function(map) {
OpenLayers.Layer.prototype.setMap.apply(this, arguments);
if (!this.renderer) {
this.map.removeLayer(this);
} else {
this.renderer.map = this.map;
this.renderer.setSize(this.map.getSize());
}
},
/**
* Method: afterAdd
* Called at the end of the map.addLayer sequence. At this point, the map
* will have a base layer. Any autoActivate strategies will be
* activated here.
*/
afterAdd: function() {
if(this.strategies) {
var strategy, i, len;
for(i=0, len=this.strategies.length; i<len; i++) {
strategy = this.strategies[i];
if(strategy.autoActivate) {
strategy.activate();
}
}
}
},
/**
* Method: removeMap
* The layer has been removed from the map.
*
* Parameters:
* map - {<OpenLayers.Map>}
*/
removeMap: function(map) {
this.drawn = false;
if(this.strategies) {
var strategy, i, len;
for(i=0, len=this.strategies.length; i<len; i++) {
strategy = this.strategies[i];
if(strategy.autoActivate) {
strategy.deactivate();
}
}
}
},
/**
* Method: onMapResize
* Notify the renderer of the change in size.
*
*/
onMapResize: function() {
OpenLayers.Layer.prototype.onMapResize.apply(this, arguments);
this.renderer.setSize(this.map.getSize());
},
/**
* Method: moveTo
* Reset the vector layer's div so that it once again is lined up with
* the map. Notify the renderer of the change of extent, and in the
* case of a change of zoom level (resolution), have the
* renderer redraw features.
*
* If the layer has not yet been drawn, cycle through the layer's
* features and draw each one.
*
* Parameters:
* bounds - {<OpenLayers.Bounds>}
* zoomChanged - {Boolean}
* dragging - {Boolean}
*/
moveTo: function(bounds, zoomChanged, dragging) {
OpenLayers.Layer.prototype.moveTo.apply(this, arguments);
var ng = (OpenLayers.Renderer.NG && this.renderer instanceof OpenLayers.Renderer.NG);
if (ng) {
dragging || this.renderer.updateDimensions(zoomChanged);
} else {
var coordSysUnchanged = true;
if (!dragging) {
this.renderer.root.style.visibility = "hidden";
this.div.style.left = -parseInt(this.map.layerContainerDiv.style.left) + "px";
this.div.style.top = -parseInt(this.map.layerContainerDiv.style.top) + "px";
var extent = this.map.getExtent();
coordSysUnchanged = this.renderer.setExtent(extent, zoomChanged);
this.renderer.root.style.visibility = "visible";
// Force a reflow on gecko based browsers to prevent jump/flicker.
// This seems to happen on only certain configurations; it was originally
// noticed in FF 2.0 and Linux.
if (OpenLayers.IS_GECKO === true) {
this.div.scrollLeft = this.div.scrollLeft;
}
if(!zoomChanged && coordSysUnchanged) {
for(var i in this.unrenderedFeatures) {
var feature = this.unrenderedFeatures[i];
this.drawFeature(feature);
}
}
}
}
if (!this.drawn || (!ng && (zoomChanged || !coordSysUnchanged))) {
this.drawn = true;
var feature;
for(var i=0, len=this.features.length; i<len; i++) {
this.renderer.locked = (i !== (len - 1));
feature = this.features[i];
this.drawFeature(feature);
}
}
},
/**
* APIMethod: redraw
* Redraws the layer. Returns true if the layer was redrawn, false if not.
*
* Returns:
* {Boolean} The layer was redrawn.
*/
redraw: function() {
if (OpenLayers.Renderer.NG && this.renderer instanceof OpenLayers.Renderer.NG) {
this.drawn = false;
}
return OpenLayers.Layer.prototype.redraw.apply(this, arguments);
},
/**
* APIMethod: display
* Hide or show the Layer
*
* Parameters:
* display - {Boolean}
*/
display: function(display) {
OpenLayers.Layer.prototype.display.apply(this, arguments);
// we need to set the display style of the root in case it is attached
// to a foreign layer
var currentDisplay = this.div.style.display;
if(currentDisplay != this.renderer.root.style.display) {
this.renderer.root.style.display = currentDisplay;
}
},
/**
* APIMethod: addFeatures
* Add Features to the layer.
*
* Parameters:
* features - {Array(<OpenLayers.Feature.Vector>)}
* options - {Object}
*/
addFeatures: function(features, options) {
if (!(OpenLayers.Util.isArray(features))) {
features = [features];
}
var notify = !options || !options.silent;
if(notify) {
var event = {features: features};
var ret = this.events.triggerEvent("beforefeaturesadded", event);
if(ret === false) {
return;
}
features = event.features;
}
// Track successfully added features for featuresadded event, since
// beforefeatureadded can veto single features.
var featuresAdded = [];
for (var i=0, len=features.length; i<len; i++) {
if (i != (features.length - 1)) {
this.renderer.locked = true;
} else {
this.renderer.locked = false;
}
var feature = features[i];
if (this.geometryType &&
!(feature.geometry instanceof this.geometryType)) {
var throwStr = OpenLayers.i18n('componentShouldBe',
{'geomType':this.geometryType.prototype.CLASS_NAME});
throw throwStr;
}
//give feature reference to its layer
feature.layer = this;
if (!feature.style && this.style) {
feature.style = OpenLayers.Util.extend({}, this.style);
}
if (notify) {
if(this.events.triggerEvent("beforefeatureadded",
{feature: feature}) === false) {
continue;
}
this.preFeatureInsert(feature);
}
featuresAdded.push(feature);
this.features.push(feature);
this.drawFeature(feature);
if (notify) {
this.events.triggerEvent("featureadded", {
feature: feature
});
this.onFeatureInsert(feature);
}
}
if(notify) {
this.events.triggerEvent("featuresadded", {features: featuresAdded});
}
},
/**
* APIMethod: removeFeatures 移除图层中所有要素,可指定移除要素
* Remove features from the layer. This erases any drawn features and
* removes them from the layer's control. The beforefeatureremoved
* and featureremoved events will be triggered for each feature. The
* featuresremoved event will be triggered after all features have
* been removed. To supress event triggering, use the silent option.
*
* Parameters:
* features - {Array(<OpenLayers.Feature.Vector>)} List of features to be
* removed.
* options - {Object} Optional properties for changing behavior of the
* removal.
*
* Valid options:
* silent - {Boolean} Supress event triggering. Default is false.
*/
removeFeatures: function(features, options) {
if(!features || features.length === 0) {
return;
}
if (features === this.features) {
return this.removeAllFeatures(options);
}
if (!(OpenLayers.Util.isArray(features))) {
features = [features];
}
if (features === this.selectedFeatures) {
features = features.slice();
}
var notify = !options || !options.silent;
if (notify) {
this.events.triggerEvent(
"beforefeaturesremoved", {features: features}
);
}
for (var i = features.length - 1; i >= 0; i--) {
// We remain locked so long as we're not at 0
// and the 'next' feature has a geometry. We do the geometry check
// because if all the features after the current one are 'null', we
// won't call eraseGeometry, so we break the 'renderer functions
// will always be called with locked=false *last*' rule. The end result
// is a possible gratiutious unlocking to save a loop through the rest
// of the list checking the remaining features every time. So long as
// null geoms are rare, this is probably okay.
if (i != 0 && features[i-1].geometry) {
this.renderer.locked = true;
} else {
this.renderer.locked = false;
}
var feature = features[i];
delete this.unrenderedFeatures[feature.id];
if (notify) {
this.events.triggerEvent("beforefeatureremoved", {
feature: feature
});
}
this.features = OpenLayers.Util.removeItem(this.features, feature);
// feature has no layer at this point
feature.layer = null;
if (feature.geometry) {
this.renderer.eraseFeatures(feature);
}
//in the case that this feature is one of the selected features,
// remove it from that array as well.
if (OpenLayers.Util.indexOf(this.selectedFeatures, feature) != -1){
OpenLayers.Util.removeItem(this.selectedFeatures, feature);
}
if (notify) {
this.events.triggerEvent("featureremoved", {
feature: feature
});
}
}
if (notify) {
this.events.triggerEvent("featuresremoved", {features: features});
}
},
/**
* APIMethod: removeAllFeatures 移除图层所有要素
* Remove all features from the layer.
*
* Parameters:
* options - {Object} Optional properties for changing behavior of the
* removal.
*
* Valid options:
* silent - {Boolean} Supress event triggering. Default is false.
*/
removeAllFeatures: function(options) {
var notify = !options || !options.silent;
var features = this.features;
if (notify) {
this.events.triggerEvent(
"beforefeaturesremoved", {features: features}
);
}
var feature;
for (var i = features.length-1; i >= 0; i--) {
feature = features[i];
if (notify) {
this.events.triggerEvent("beforefeatureremoved", {
feature: feature
});
}
feature.layer = null;
if (notify) {
this.events.triggerEvent("featureremoved", {
feature: feature
});
}
}
this.renderer.clear();
this.features = [];
this.unrenderedFeatures = {};
this.selectedFeatures = [];
if (notify) {
this.events.triggerEvent("featuresremoved", {features: features});
}
},
/**
* APIMethod: destroyFeatures 注销图层中指定要素
* Erase and destroy features on the layer.
*
* Parameters:
* features - {Array(<OpenLayers.Feature.Vector>)} An optional array of
* features to destroy. If not supplied, all features on the layer
* will be destroyed.
* options - {Object}
*/
destroyFeatures: function(features, options) {
var all = (features == undefined); // evaluates to true if
// features is null
if(all) {
features = this.features;
}
if(features) {
this.removeFeatures(features, options);
for(var i=features.length-1; i>=0; i--) {
features[i].destroy();
}
}
},
/**
* APIMethod: drawFeature
* Draw (or redraw) a feature on the layer. If the optional style argument
* is included, this style will be used. If no style is included, the
* feature's style will be used. If the feature doesn't have a style,
* the layer's style will be used.
*
* This function is not designed to be used when adding features to
* the layer (use addFeatures instead). It is meant to be used when
* the style of a feature has changed, or in some other way needs to
* visually updated *after* it has already been added to a layer. You
* must add the feature to the layer for most layer-related events to
* happen.
*
* Parameters:
* feature - {<OpenLayers.Feature.Vector>}
* style - {String | Object} Named render intent or full symbolizer object.
*/
drawFeature: function(feature, style) {
// don't try to draw the feature with the renderer if the layer is not
// drawn itself
if (!this.drawn) {
return;
}
if (typeof style != "object") {
if(!style && feature.state === OpenLayers.State.DELETE) {
style = "delete";
}
var renderIntent = style || feature.renderIntent;
style = feature.style || this.style;
if (!style) {
style = this.styleMap.createSymbolizer(feature, renderIntent);
}
}
var drawn = this.renderer.drawFeature(feature, style);
//TODO remove the check for null when we get rid of Renderer.SVG
if (drawn === false || drawn === null) {
this.unrenderedFeatures[feature.id] = feature;
} else {
delete this.unrenderedFeatures[feature.id];
}
},
/**
* Method: eraseFeatures 擦除要素
* Erase features from the layer.
*
* Parameters:
* features - {Array(<OpenLayers.Feature.Vector>)}
*/
eraseFeatures: function(features) {
this.renderer.eraseFeatures(features);
},
/**
* Method: getFeatureFromEvent
* Given an event, return a feature if the event occurred over one.
* Otherwise, return null.
*
* Parameters:
* evt - {Event}
*
* Returns:
* {<OpenLayers.Feature.Vector>} A feature if one was under the event.
*/
getFeatureFromEvent: function(evt) {
if (!this.renderer) {
OpenLayers.Console.error(OpenLayers.i18n("getFeatureError"));
return null;
}
var feature = null;
var featureId = this.renderer.getFeatureIdFromEvent(evt);
if (featureId) {
if (typeof featureId === "string") {
feature = this.getFeatureById(featureId);
} else {
feature = featureId;
}
}
return feature;
},
/**
* APIMethod: getFeatureBy 根据指定属性删除要素
* Given a property value, return the feature if it exists in the features array
*
* Parameters:
* property - {String}
* value - {String}
*
* Returns:
* {<OpenLayers.Feature.Vector>} A feature corresponding to the given
* property value or null if there is no such feature.
*/
getFeatureBy: function(property, value) {
//TBD - would it be more efficient to use a hash for this.features?
var feature = null;
for(var i=0, len=this.features.length; i<len; ++i) {
if(this.features[i][property] == value) {
feature = this.features[i];
break;
}
}
return feature;
},
/**
* APIMethod: getFeatureById
* Given a feature id, return the feature if it exists in the features array
*
* Parameters:
* featureId - {String}
*
* Returns:
* {<OpenLayers.Feature.Vector>} A feature corresponding to the given
* featureId or null if there is no such feature.
*/
getFeatureById: function(featureId) {
return this.getFeatureBy('id', featureId);
},
/**
* APIMethod: getFeatureByFid
* Given a feature fid, return the feature if it exists in the features array
*
* Parameters:
* featureFid - {String}
*
* Returns:
* {<OpenLayers.Feature.Vector>} A feature corresponding to the given
* featureFid or null if there is no such feature.
*/
getFeatureByFid: function(featureFid) {
return this.getFeatureBy('fid', featureFid);
},
/**
* APIMethod: getFeaturesByAttribute
* Returns an array of features that have the given attribute key set to the
* given value. Comparison of attribute values takes care of datatypes, e.g.
* the string '1234' is not equal to the number 1234.
*
* Parameters:
* attrName - {String}
* attrValue - {Mixed}
*
* Returns:
* Array(<OpenLayers.Feature.Vector>) An array of features that have the
* passed named attribute set to the given value.
*/
getFeaturesByAttribute: function(attrName, attrValue) {
var i,
feature,
len = this.features.length,
foundFeatures = [];
for(i = 0; i < len; i++) {
feature = this.features[i];
if(feature && feature.attributes) {
if (feature.attributes[attrName] === attrValue) {
foundFeatures.push(feature);
}
}
}
return foundFeatures;
},
/**
* Unselect the selected features
* i.e. clears the featureSelection array
* change the style back
clearSelection: function() {
var vectorLayer = this.map.vectorLayer;
for (var i = 0; i < this.map.featureSelection.length; i++) {
var featureSelection = this.map.featureSelection[i];
vectorLayer.drawFeature(featureSelection, vectorLayer.style);
}
this.map.featureSelection = [];
},
*/
/**
* APIMethod: onFeatureInsert
* method called after a feature is inserted.
* Does nothing by default. Override this if you
* need to do something on feature updates.
*
* Paarameters:
* feature - {<OpenLayers.Feature.Vector>}
*/
onFeatureInsert: function(feature) {
},
/**
* APIMethod: preFeatureInsert
* method called before a feature is inserted.
* Does nothing by default. Override this if you
* need to do something when features are first added to the
* layer, but before they are drawn, such as adjust the style.
*
* Parameters:
* feature - {<OpenLayers.Feature.Vector>}
*/
preFeatureInsert: function(feature) {
},
/**
* APIMethod: getDataExtent
* Calculates the max extent which includes all of the features.
*
* Returns:
* {<OpenLayers.Bounds>} or null if the layer has no features with
* geometries.
*/
getDataExtent: function () {
var maxExtent = null;
var features = this.features;
if(features && (features.length > 0)) {
var geometry = null;
for(var i=0, len=features.length; i<len; i++) {
geometry = features[i].geometry;
if (geometry) {
if (maxExtent === null) {
maxExtent = new OpenLayers.Bounds();
}
maxExtent.extend(geometry.getBounds());
}
}
}
return maxExtent;
},
CLASS_NAME: "OpenLayers.Layer.Vector"
});