@react-google-maps/marker-clusterer 2.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,84 @@
1
+ import { Clusterer } from '../Clusterer'
2
+
3
+ describe('Clusterer', () => {
4
+ describe('CALCULATOR', () => {
5
+ const elementMock = document.createElement('div')
6
+ const mapMock = new window.google.maps.Map(elementMock)
7
+ const markerMock = new google.maps.Marker()
8
+
9
+ const clusterer = new Clusterer(mapMock)
10
+ const { calculator } = clusterer
11
+
12
+ it('should return index 1 for no markers', () => {
13
+ const markers: google.maps.Marker[] = []
14
+ const numStyles = 10
15
+ const actual = calculator(markers, numStyles)
16
+
17
+ expect(actual).toEqual({
18
+ text: '0',
19
+ index: 1,
20
+ title: '',
21
+ })
22
+ })
23
+
24
+ it('should return index 1 for single marker', () => {
25
+ const markers = [markerMock]
26
+ const numStyles = 10
27
+ const actual = calculator(markers, numStyles)
28
+
29
+ expect(actual).toEqual({
30
+ text: '1',
31
+ index: 1,
32
+ title: '',
33
+ })
34
+ })
35
+
36
+ it('should return index 1 for 1-digit number of markers', () => {
37
+ const markers = Array.from({ length: 9 }, () => markerMock)
38
+ const numStyles = 10
39
+ const actual = calculator(markers, numStyles)
40
+
41
+ expect(actual).toEqual({
42
+ text: '9',
43
+ index: 1,
44
+ title: '',
45
+ })
46
+ })
47
+
48
+ it('should return index 2 for 2-digit number of markers', () => {
49
+ const markers = Array.from({ length: 99 }, () => markerMock)
50
+ const numStyles = 50
51
+ const actual = calculator(markers, numStyles)
52
+
53
+ expect(actual).toEqual({
54
+ text: '99',
55
+ index: 2,
56
+ title: '',
57
+ })
58
+ })
59
+
60
+ it('should return index 3 for 3-digit number of markers', () => {
61
+ const markers = Array.from({ length: 125 }, () => markerMock)
62
+ const numStyles = 10
63
+ const actual = calculator(markers, numStyles)
64
+
65
+ expect(actual).toEqual({
66
+ text: '125',
67
+ index: 3,
68
+ title: '',
69
+ })
70
+ })
71
+
72
+ it('should return index = numStyles if it is smaller than calculated index', () => {
73
+ const markers = Array.from({ length: 10_000 }, () => markerMock)
74
+ const numStyles = 3
75
+ const actual = calculator(markers, numStyles)
76
+
77
+ expect(actual).toEqual({
78
+ text: '10000',
79
+ index: 3,
80
+ title: '',
81
+ })
82
+ })
83
+ })
84
+ })
package/src/index.ts ADDED
@@ -0,0 +1,51 @@
1
+ /* eslint-disable filenames/match-exported */
2
+ /**
3
+ * @name MarkerClusterer for Google Maps V3
4
+ * @version 1.0.0 [March 2019]
5
+ * @author Alexey Lyakhov
6
+ * @fileoverview
7
+ * The library creates and manages per-zoom-level clusters for large amounts of markers.
8
+ * <p>
9
+ * This is an enhanced V3 implementation of the
10
+ * <a href="http://gmaps-utility-library-dev.googlecode.com/svn/tags/markerclusterer/"
11
+ * >V2 MarkerClusterer</a> by Xiaoxi Wu. It is based on the
12
+ * <a href="http://google-maps-utility-library-v3.googlecode.com/svn/tags/markerclusterer/"
13
+ * >V3 MarkerClusterer</a> port by Luke Mahe. MarkerClustererPlus was created by Gary Little.
14
+ * <p>
15
+ * v2.0 release: MarkerClustererPlus v2.0 is backward compatible with MarkerClusterer v1.0. It
16
+ * adds support for the <code>ignoreHidden</code>, <code>title</code>, <code>batchSizeIE</code>,
17
+ * and <code>calculator</code> properties as well as support for four more events. It also allows
18
+ * greater control over the styling of the text that appears on the cluster marker. The
19
+ * documentation has been significantly improved and the overall code has been simplified and
20
+ * polished. Very large numbers of markers can now be managed without causing Javascript timeout
21
+ * errors on Internet Explorer. Note that the name of the <code>clusterclick</code> event has been
22
+ * deprecated. The new name is <code>click</code>, so please change your application code now.
23
+ */
24
+
25
+ /**
26
+ * Licensed under the Apache License, Version 2.0 (the "License");
27
+ * you may not use this file except in compliance with the License.
28
+ * You may obtain a copy of the License at
29
+ *
30
+ * http://www.apache.org/licenses/LICENSE-2.0
31
+ *
32
+ * Unless required by applicable law or agreed to in writing, software
33
+ * distributed under the License is distributed on an "AS IS" BASIS,
34
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
35
+ * See the License for the specific language governing permissions and
36
+ * limitations under the License.
37
+ */
38
+
39
+ export { Clusterer } from './Clusterer'
40
+
41
+ export { Cluster } from './Cluster'
42
+
43
+ export { ClusterIcon } from './ClusterIcon'
44
+
45
+ export {
46
+ ClusterIconInfo,
47
+ ClusterIconStyle,
48
+ MarkerExtended,
49
+ TCalculator,
50
+ ClustererOptions,
51
+ } from './types'
@@ -0,0 +1,356 @@
1
+ const createMockFuncsFromArray = (instance, names = []) => {
2
+ names.forEach(name => {
3
+ instance[name] = jest.fn().mockName(name)
4
+ })
5
+ }
6
+
7
+ const createGoogleMapsMock = (libraries = []) => {
8
+ const createMVCObject = instance => {
9
+ const listeners = {}
10
+ instance.listeners = listeners
11
+
12
+ instance.addListener = jest
13
+ .fn((event, fn) => {
14
+ listeners[event] = listeners[event] || []
15
+ listeners[event].push(fn)
16
+ return {
17
+ remove: () => {
18
+ const index = listeners[event].indexOf(fn)
19
+
20
+ if (index !== -1) {
21
+ listeners[event].splice(index, 1)
22
+ }
23
+ },
24
+ }
25
+ })
26
+ .mockName('addListener')
27
+
28
+ createMockFuncsFromArray(instance, [
29
+ 'bindTo',
30
+ 'get',
31
+ 'notify',
32
+ 'set',
33
+ 'setValues',
34
+ 'unbind',
35
+ 'unbindAll',
36
+ ])
37
+ }
38
+
39
+ const OverlayViewMock = function() {}
40
+ OverlayViewMock.prototype.setMap = jest.fn()
41
+
42
+ const maps = {
43
+ Animation: {
44
+ BOUNCE: 1,
45
+ DROP: 2,
46
+ Lo: 3,
47
+ Go: 4,
48
+ },
49
+ BicyclingLayer: jest.fn().mockImplementation(function() {
50
+ createMVCObject(this)
51
+ createMockFuncsFromArray(this, ['setMap'])
52
+ }),
53
+ TransitLayer: jest.fn().mockImplementation(function() {
54
+ createMVCObject(this)
55
+ createMockFuncsFromArray(this, ['setMap'])
56
+ }),
57
+ Circle: jest.fn().mockImplementation(function(opts) {
58
+ this.opts = opts
59
+ createMVCObject(this)
60
+ createMockFuncsFromArray(this, [
61
+ 'setCenter',
62
+ 'setDraggable',
63
+ 'setEditable',
64
+ 'setMap',
65
+ 'setOptions',
66
+ 'setRadius',
67
+ 'setVisible',
68
+ ])
69
+ }),
70
+ ControlPosition: {
71
+ TOP_LEFT: 1,
72
+ TOP_CENTER: 2,
73
+ TOP: 2,
74
+ TOP_RIGHT: 3,
75
+ LEFT_CENTER: 4,
76
+ LEFT: 5,
77
+ LEFT_TOP: 5,
78
+ LEFT_BOTTOM: 6,
79
+ RIGHT: 7,
80
+ RIGHT_CENTER: 8,
81
+ RIGHT_BOTTOM: 9,
82
+ BOTTOM_LEFT: 10,
83
+ BOTTOM: 11,
84
+ BOTTOM_CENTER: 11,
85
+ BOTTOM_RIGHT: 12,
86
+ CENTER: 13,
87
+ },
88
+ Data: jest.fn().mockImplementation(function(options) {
89
+ this.options = options
90
+ createMVCObject(this)
91
+ createMockFuncsFromArray(this, [
92
+ 'setControlPosition',
93
+ 'setControls',
94
+ 'setDrawingMode',
95
+ 'setMap',
96
+ 'setStyle',
97
+ ])
98
+ }),
99
+ DirectionsRenderer: jest.fn().mockImplementation(function(opts) {
100
+ this.opts = opts
101
+ createMVCObject(this)
102
+ createMockFuncsFromArray(this, [
103
+ 'setDirections',
104
+ 'setMap',
105
+ 'setOptions',
106
+ 'setPanel',
107
+ 'setRouteIndex',
108
+ ])
109
+ }),
110
+ DirectionsService: function() {},
111
+ DirectionsStatus: {
112
+ INVALID_REQUEST: 'INVALID_REQUEST',
113
+ MAX_WAYPOINTS_EXCEEDED: 'MAX_WAYPOINTS_EXCEEDED',
114
+ NOT_FOUND: 'NOT_FOUND',
115
+ OK: 'OK',
116
+ OVER_QUERY_LIMIT: 'OVER_QUERY_LIMIT',
117
+ REQUEST_DENIED: 'REQUEST_DENIED',
118
+ UNKNOWN_ERROR: 'UNKNOWN_ERROR',
119
+ ZERO_RESULTS: 'ZERO_RESULTS',
120
+ },
121
+ DirectionsTravelMode: {
122
+ BICYCLING: 'BICYCLING',
123
+ DRIVING: 'DRIVING',
124
+ TRANSIT: 'TRANSIT',
125
+ WALKING: 'WALKING',
126
+ },
127
+ DirectionsUnitSystem: {
128
+ IMPERIAL: 1,
129
+ METRIC: 0,
130
+ },
131
+ DistanceMatrixElementStatus: {
132
+ NOT_FOUND: 'NOT_FOUND',
133
+ OK: 'OK',
134
+ ZERO_RESULTS: 'ZERO_RESULTS',
135
+ },
136
+ DistanceMatrixService: function() {},
137
+ DistanceMatrixStatus: {
138
+ INVALID_REQUEST: 'INVALID_REQUEST',
139
+ MAX_DIMENSIONS_EXCEEDED: 'MAX_DIMENSIONS_EXCEEDED',
140
+ MAX_ELEMENTS_EXCEEDED: 'MAX_ELEMENTS_EXCEEDED',
141
+ OK: 'OK',
142
+ OVER_QUERY_LIMIT: 'OVER_QUERY_LIMIT',
143
+ REQUEST_DENIED: 'REQUEST_DENIED',
144
+ UNKNOWN_ERROR: 'UNKNOWN_ERROR',
145
+ },
146
+ ElevationService: function() {},
147
+ ElevationStatus: {
148
+ Co: 'DATA_NOT_AVAILABLE',
149
+ INVALID_REQUEST: 'INVALID_REQUEST',
150
+ OK: 'OK',
151
+ OVER_QUERY_LIMIT: 'OVER_QUERY_LIMIT',
152
+ REQUEST_DENIED: 'REQUEST_DENIED',
153
+ UNKNOWN_ERROR: 'UNKNOWN_ERROR',
154
+ },
155
+ FusionTablesLayer: jest.fn().mockImplementation(function(options) {
156
+ this.options = options
157
+ createMVCObject(this)
158
+ createMockFuncsFromArray(this, ['setMap', 'setOptions'])
159
+ }),
160
+ Geocoder: function() {},
161
+ GeocoderLocationType: {
162
+ APPROXIMATE: 'APPROXIMATE',
163
+ GEOMETRIC_CENTER: 'GEOMETRIC_CENTER',
164
+ RANGE_INTERPOLATED: 'RANGE_INTERPOLATED',
165
+ ROOFTOP: 'ROOFTOP',
166
+ },
167
+ GeocoderStatus: {
168
+ ERROR: 'ERROR',
169
+ INVALID_REQUEST: 'INVALID_REQUEST',
170
+ OK: 'OK',
171
+ OVER_QUERY_LIMIT: 'OVER_QUERY_LIMIT',
172
+ REQUEST_DENIED: 'REQUEST_DENIED',
173
+ UNKNOWN_ERROR: 'UNKNOWN_ERROR',
174
+ ZERO_RESULTS: 'ZERO_RESULTS',
175
+ },
176
+ GroundOverlay: function() {},
177
+ ImageMapType: function() {},
178
+ InfoWindow: function() {},
179
+ KmlLayer: function() {},
180
+ KmlLayerStatus: {
181
+ DOCUMENT_NOT_FOUND: 'DOCUMENT_NOT_FOUND',
182
+ DOCUMENT_TOO_LARGE: 'DOCUMENT_TOO_LARGE',
183
+ FETCH_ERROR: 'FETCH_ERROR',
184
+ INVALID_DOCUMENT: 'INVALID_DOCUMENT',
185
+ INVALID_REQUEST: 'INVALID_REQUEST',
186
+ LIMITS_EXCEEDED: 'LIMITS_EXCEEDED',
187
+ OK: 'OK',
188
+ TIMED_OUT: 'TIMED_OUT',
189
+ UNKNOWN: 'UNKNOWN',
190
+ },
191
+ LatLng: function() {},
192
+ LatLngBounds: function() {},
193
+ MVCArray: function() {},
194
+ MVCObject: jest.fn().mockImplementation(function() {
195
+ createMVCObject(this)
196
+ }),
197
+ Map: jest.fn().mockImplementation(function(mapDiv, opts) {
198
+ this.mapDiv = mapDiv
199
+ this.opts = opts
200
+ createMVCObject(this)
201
+ createMockFuncsFromArray(this, [
202
+ 'setCenter',
203
+ 'setClickableIcons',
204
+ 'setHeading',
205
+ 'setMapTypeId',
206
+ 'setOptions',
207
+ 'setStreetView',
208
+ 'setTilt',
209
+ 'setZoom',
210
+ 'fitBounds',
211
+ 'getBounds',
212
+ 'panToBounds',
213
+ ])
214
+ }),
215
+ MapTypeControlStyle: {
216
+ DEFAULT: 0,
217
+ DROPDOWN_MENU: 2,
218
+ HORIZONTAL_BAR: 1,
219
+ INSET: 3,
220
+ INSET_LARGE: 4,
221
+ },
222
+ MapTypeId: {
223
+ HYBRID: 'hybrid',
224
+ ROADMAP: 'roadmap',
225
+ SATELLITE: 'satellite',
226
+ TERRAIN: 'terrain',
227
+ },
228
+ MapTypeRegistry: function() {},
229
+ Marker: jest.fn().mockImplementation(function(opts) {
230
+ this.opts = opts
231
+ createMVCObject(this)
232
+ createMockFuncsFromArray(this, [
233
+ 'setMap',
234
+ 'setOpacity',
235
+ 'setOptions',
236
+ 'setPosition',
237
+ 'setShape',
238
+ 'setTitle',
239
+ 'setVisible',
240
+ 'setZIndex',
241
+ ])
242
+ }),
243
+ MarkerImage: function() {},
244
+ MaxZoomService: function() {
245
+ return {
246
+ getMaxZoomAtLatLng: function() {},
247
+ }
248
+ },
249
+ MaxZoomStatus: {
250
+ ERROR: 'ERROR',
251
+ OK: 'OK',
252
+ },
253
+ NavigationControlStyle: {
254
+ ANDROID: 2,
255
+ DEFAULT: 0,
256
+ Mo: 4,
257
+ SMALL: 1,
258
+ ZOOM_PAN: 3,
259
+ ik: 5,
260
+ },
261
+ OverlayView: OverlayViewMock,
262
+ Point: function() {},
263
+ Polygon: function() {},
264
+ Polyline: function() {},
265
+ Rectangle: function() {},
266
+ SaveWidget: function() {},
267
+ ScaleControlStyle: {
268
+ DEFAULT: 0,
269
+ },
270
+ Size: function() {},
271
+ StreetViewCoverageLayer: function() {},
272
+ StreetViewPanorama: function() {},
273
+ StreetViewPreference: {
274
+ BEST: 'best',
275
+ NEAREST: 'nearest',
276
+ },
277
+ StreetViewService: function() {},
278
+ StreetViewSource: {
279
+ DEFAULT: 'default',
280
+ OUTDOOR: 'outdoor',
281
+ },
282
+ StreetViewStatus: {
283
+ OK: 'OK',
284
+ UNKNOWN_ERROR: 'UNKNOWN_ERROR',
285
+ ZERO_RESULTS: 'ZERO_RESULTS',
286
+ },
287
+ StrokePosition: {
288
+ CENTER: 0,
289
+ INSIDE: 1,
290
+ OUTSIDE: 2,
291
+ },
292
+ StyledMapType: function() {},
293
+ SymbolPath: {
294
+ BACKWARD_CLOSED_ARROW: 3,
295
+ BACKWARD_OPEN_ARROW: 4,
296
+ CIRCLE: 0,
297
+ FORWARD_CLOSED_ARROW: 1,
298
+ FORWARD_OPEN_ARROW: 2,
299
+ },
300
+ TrafficLayer: jest.fn().mockImplementation(function(opts) {
301
+ this.opts = opts
302
+ createMVCObject(this)
303
+ createMockFuncsFromArray(this, ['setMap', 'setOptions'])
304
+ }),
305
+ TrafficModel: {
306
+ BEST_GUESS: 'bestguess',
307
+ OPTIMISTIC: 'optimistic',
308
+ PESSIMISTIC: 'pessimistic',
309
+ },
310
+ TransitMode: {
311
+ BUS: 'BUS',
312
+ RAIL: 'RAIL',
313
+ SUBWAY: 'SUBWAY',
314
+ TRAIN: 'TRAIN',
315
+ TRAM: 'TRAM',
316
+ },
317
+ TransitRoutePreference: {
318
+ FEWER_TRANSFERS: 'FEWER_TRANSFERS',
319
+ LESS_WALKING: 'LESS_WALKING',
320
+ },
321
+ TravelMode: {
322
+ BICYCLING: 'BICYCLING',
323
+ DRIVING: 'DRIVING',
324
+ TRANSIT: 'TRANSIT',
325
+ WALKING: 'WALKING',
326
+ },
327
+ UnitSystem: {
328
+ IMPERIAL: 1,
329
+ METRIC: 0,
330
+ },
331
+ ZoomControlStyle: {
332
+ DEFAULT: 0,
333
+ LARGE: 2,
334
+ SMALL: 1,
335
+ ik: 3,
336
+ },
337
+ __gjsload__: function() {},
338
+ event: {
339
+ clearInstanceListeners: jest.fn().mockName('clearInstanceListeners'),
340
+ },
341
+ }
342
+
343
+ if (libraries.includes('places')) {
344
+ maps.places = {
345
+ AutocompleteService: jest.fn(() => ({
346
+ getPlacePredictions: jest.fn(),
347
+ })),
348
+ }
349
+ }
350
+
351
+ return maps
352
+ }
353
+
354
+ window.google = {
355
+ maps: createGoogleMapsMock(),
356
+ }
package/src/types.tsx ADDED
@@ -0,0 +1,47 @@
1
+ /* globals google */
2
+ export interface ClusterIconInfo {
3
+ text: string
4
+ index: number
5
+ title: string
6
+ }
7
+
8
+ export type MarkerExtended = google.maps.Marker & {
9
+ isAdded?: boolean
10
+ }
11
+
12
+ export type TCalculator = (markers: MarkerExtended[], num: number) => ClusterIconInfo
13
+
14
+ export interface ClustererOptions {
15
+ gridSize?: number
16
+ maxZoom?: number
17
+ zoomOnClick?: boolean
18
+ averageCenter?: boolean
19
+ minimumClusterSize?: number
20
+ ignoreHidden?: boolean
21
+ title?: string
22
+ calculator?: TCalculator
23
+ clusterClass?: string
24
+ styles?: ClusterIconStyle[]
25
+ enableRetinaIcons?: boolean
26
+ batchSize?: number
27
+ batchSizeIE?: number
28
+ imagePath?: string
29
+ imageExtension?: string
30
+ imageSizes?: number[]
31
+ }
32
+
33
+ export interface ClusterIconStyle {
34
+ url: string
35
+ className?: string
36
+ height: number
37
+ width: number
38
+ anchorText?: number[]
39
+ anchorIcon?: number[]
40
+ textColor?: string
41
+ textSize?: number
42
+ textDecoration?: string
43
+ fontWeight?: string
44
+ fontStyle?: string
45
+ fontFamily?: string
46
+ backgroundPosition?: string
47
+ }