30
30
* collection.add(new ymaps.Placemark([40, 25])); // Fire the 'boundschange' event on the collection instance.
31
31
*/
32
32
33
+ /**
34
+ * Encapsulates bounds calculating methods.
35
+ * @class
36
+ * @name GeoBounds
37
+ * @param {Array } [lowerCorner] Bottom left corner of the area.
38
+ * @param {Array } [upperCorner] Top right corner of the area.
39
+ */
40
+ function GeoBounds ( lowerCorner , upperCorner ) {
41
+ this . setBounds ( [ lowerCorner , upperCorner ] ) ;
42
+ }
43
+
44
+ GeoBounds . prototype = {
45
+ constructor : GeoBounds ,
46
+ /**
47
+ * Bounds getter.
48
+ * @function
49
+ * @name GeoBounds.getBounds
50
+ * @returns {Array } Forms bounds and return it.
51
+ */
52
+ getBounds : function ( ) {
53
+ return Array . isArray ( this . _lower ) && Array . isArray ( this . _upper ) ? [ this . _lower , this . _upper ] : null ;
54
+ } ,
55
+ /**
56
+ * Bounds setter.
57
+ * @function
58
+ * @name GeoBounds.setBounds
59
+ * @param {Array } bounds
60
+ */
61
+ setBounds : function ( bounds ) {
62
+ if ( Array . isArray ( bounds ) ) {
63
+ this . _lower = Array . isArray ( bounds [ 0 ] ) && bounds [ 0 ] . slice ( 0 ) ;
64
+ this . _upper = Array . isArray ( bounds [ 1 ] ) && bounds [ 1 ] . slice ( 0 ) ;
65
+ }
66
+ } ,
67
+ /**
68
+ * Checks whether a point is contained in the bounds area.
69
+ * @function
70
+ * @name GeoBounds.contains
71
+ * @param {Array } point GoePoint GeoJSON geometry representation.
72
+ * @returns {Boolean }
73
+ */
74
+ contains : function ( point ) {
75
+ var bounds = this . getBounds ( ) ;
76
+
77
+ return ! ! bounds &&
78
+ bounds [ 0 ] [ 0 ] <= point [ 0 ] &&
79
+ bounds [ 0 ] [ 1 ] <= point [ 1 ] &&
80
+ bounds [ 1 ] [ 0 ] >= point [ 0 ] &&
81
+ bounds [ 1 ] [ 1 ] >= point [ 1 ] ;
82
+ } ,
83
+ /**
84
+ * Update bounds with other bounds.
85
+ * @function
86
+ * @name GeoBounds.update
87
+ * @param {Array } bounds
88
+ */
89
+ update : function ( bounds ) {
90
+ if ( ! this . getBounds ( ) ) {
91
+ this . setBounds ( bounds ) ;
92
+ return ;
93
+ }
94
+
95
+ var lowerCorner = this . _lower ;
96
+ upperCorner = this . _upper ;
97
+
98
+ if ( Array . isArray ( lowerCorner ) && Array . isArray ( upperCorner ) ) {
99
+ lowerCorner . splice ( 0 , 2 ,
100
+ Math . min ( lowerCorner [ 0 ] , bounds [ 0 ] [ 0 ] ) ,
101
+ Math . min ( lowerCorner [ 1 ] , bounds [ 0 ] [ 1 ] )
102
+ ) ,
103
+ upperCorner . splice ( 0 , 2 ,
104
+ Math . max ( upperCorner [ 0 ] , bounds [ 1 ] [ 0 ] ) ,
105
+ Math . max ( upperCorner [ 1 ] , bounds [ 1 ] [ 1 ] )
106
+ )
107
+ }
108
+ } ,
109
+ } ;
110
+
33
111
/**
34
112
* GeoObjectCollection with bounds calculating.
35
113
* @class
39
117
*/
40
118
function GeoCollectionBounds ( ) {
41
119
GeoCollectionBounds . superclass . constructor . apply ( this , arguments ) ;
42
- this . _bounds = null ;
120
+ this . _bounds = new GeoBounds ( ) ;
43
121
44
122
this . events . add ( 'add' , function ( e ) {
45
- var childBounds = e . get ( 'child' ) . geometry . getBounds ( ) ,
46
- boundsRect = this . getBoundsRect ( ) ;
123
+ var childBounds = e . get ( 'child' ) . geometry . getBounds ( ) ;
47
124
48
- boundsRect && boundsRect . geometry . contains ( childBounds [ 0 ] ) && boundsRect . geometry . contains ( childBounds [ 1 ] ) ||
49
- ( this . _updateBounds ( childBounds ) , this . events . fire ( 'boundschange' , new ymaps . Event ( {
125
+ this . _bounds . contains ( childBounds [ 0 ] ) && this . _bounds . contains ( childBounds [ 1 ] ) ||
126
+ ( this . _bounds . update ( childBounds ) , this . events . fire ( 'boundschange' , {
50
127
target : this ,
51
- bounds : this . _bounds
52
- } , true ) ) ) ;
128
+ bounds : this . _bounds . getBounds ( )
129
+ } , true ) ) ;
53
130
} , this )
54
131
. add ( 'remove' , function ( e ) {
55
132
var childBounds = e . get ( 'child' ) . geometry . getBounds ( ) ;
56
133
57
- this . getLength ( ) || ( this . _bounds = null ) ;
134
+ this . getLength ( ) || ( this . _bounds = new GeoBounds ( ) ) ;
135
+
136
+ var bounds = this . _bounds . getBounds ( ) ;
58
137
59
- this . _bounds &&
60
- ( this . _bounds [ 0 ] [ 0 ] <= childBounds [ 0 ] [ 0 ] ||
61
- this . _bounds [ 0 ] [ 1 ] <= childBounds [ 0 ] [ 1 ] ||
62
- this . _bounds [ 1 ] [ 0 ] <= childBounds [ 1 ] [ 0 ] ||
63
- this . _bounds [ 1 ] [ 0 ] <= childBounds [ 1 ] [ 0 ] ) &&
64
- ( this . _recalculateBounds ( ) , this . events . fire ( 'boundschange' , new ymaps . Event ( {
138
+ bounds &&
139
+ ( bounds [ 0 ] [ 0 ] <= childBounds [ 0 ] [ 0 ] ||
140
+ bounds [ 0 ] [ 1 ] <= childBounds [ 0 ] [ 1 ] ||
141
+ bounds [ 1 ] [ 0 ] <= childBounds [ 1 ] [ 0 ] ||
142
+ bounds [ 1 ] [ 0 ] <= childBounds [ 1 ] [ 0 ] ) &&
143
+ ( this . _recalculateBounds ( ) , this . events . fire ( 'boundschange' , {
65
144
target : this ,
66
- bounds : this . _bounds
67
- } , true ) ) ) ;
145
+ bounds : bounds
146
+ } , true ) ) ;
68
147
} , this ) ;
69
148
}
70
149
@@ -75,7 +154,9 @@ ymaps.util.augment(GeoCollectionBounds, ymaps.GeoObjectCollection, /** @lends Ge
75
154
* @returns {Array } Represents collection bounds through 2 points array.
76
155
*/
77
156
getBounds : function ( ) {
78
- return this . _bounds ;
157
+ var bounds = this . _bounds . getBounds ( ) ;
158
+
159
+ return bounds && [ bounds [ 0 ] . slice ( 0 ) , bounds [ 1 ] . slice ( 0 ) ] ;
79
160
} ,
80
161
/**
81
162
* Represents collection bounds through ymaps.Polygon instance.
@@ -84,12 +165,14 @@ ymaps.util.augment(GeoCollectionBounds, ymaps.GeoObjectCollection, /** @lends Ge
84
165
* @returns {ymaps.Polygon } Useful in collection bounds visualization on the map.
85
166
*/
86
167
getBoundsRect : function ( ) {
87
- if ( ! this . _bounds ) {
168
+ var bounds = this . _bounds . getBounds ( ) ;
169
+
170
+ if ( ! bounds ) {
88
171
return ;
89
172
}
90
173
91
- var lowerCorner = this . _bounds [ 0 ] ,
92
- upperCorner = this . _bounds [ 1 ] ,
174
+ var lowerCorner = bounds [ 0 ] ,
175
+ upperCorner = bounds [ 1 ] ,
93
176
rectangle = new ymaps . Polygon ( [ [
94
177
lowerCorner ,
95
178
[ lowerCorner [ 0 ] , upperCorner [ 1 ] ] ,
@@ -99,39 +182,15 @@ ymaps.util.augment(GeoCollectionBounds, ymaps.GeoObjectCollection, /** @lends Ge
99
182
100
183
return rectangle ;
101
184
} ,
102
- /**
103
- * Update collection bounds with child item bounds.
104
- * @ignore
105
- * @function
106
- * @param {Array } childBounds
107
- */
108
- _updateBounds : function ( childBounds ) {
109
- if ( ! this . _bounds ) {
110
- this . _bounds = [ [ ] . concat ( childBounds [ 0 ] ) , [ ] . concat ( childBounds [ 1 ] ) ] ;
111
- return ;
112
- }
113
-
114
- var lowerCorner = this . _bounds [ 0 ] ,
115
- upperCorner = this . _bounds [ 1 ] ;
116
-
117
- lowerCorner . splice ( 0 , 2 ,
118
- Math . min ( lowerCorner [ 0 ] , childBounds [ 0 ] [ 0 ] ) ,
119
- Math . min ( lowerCorner [ 1 ] , childBounds [ 0 ] [ 1 ] )
120
- ) ,
121
- upperCorner . splice ( 0 , 2 ,
122
- Math . max ( upperCorner [ 0 ] , childBounds [ 1 ] [ 0 ] ) ,
123
- Math . max ( upperCorner [ 1 ] , childBounds [ 1 ] [ 1 ] )
124
- )
125
- } ,
126
185
/**
127
186
* Complete collection bounds recount.
128
187
* @ignore
129
188
* @function
130
189
*/
131
190
_recalculateBounds : function ( ) {
132
- this . _bounds = null ;
191
+ this . _bounds = new GeoBounds ( ) ;
133
192
this . each ( function ( item ) {
134
- this . _updateBounds ( item . geometry . getBounds ( ) ) ;
193
+ this . _bounds . update ( item . geometry . getBounds ( ) ) ;
135
194
} , this ) ;
136
195
}
137
196
} ) ;
0 commit comments