forked from JohnRiv/google-map
-
Notifications
You must be signed in to change notification settings - Fork 2
/
google-map-poly.js
565 lines (489 loc) · 14.1 KB
/
google-map-poly.js
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
import { Polymer } from '@polymer/polymer/lib/legacy/polymer-fn.js';
import { html } from '@polymer/polymer/lib/utils/html-tag.js';
import './google-map-point.js';
Polymer({
_template: html`
<style>
:host {
display: none;
}
</style>
<slot id="points"></slot>
`,
is: 'google-map-poly',
/**
* Fired when the `path` property is built based on child `google-map-point` elements, either
* initially or when they are changed.
*
* @event google-map-poly-path-built
* @param {google.maps.MVCArray.<LatLng>} path The poly path.
*/
/**
* Fired when the user finishes adding vertices to the poly. The host component can use the
* provided path to rebuild its list of points.
*
* @event google-map-poly-path-updated
* @param {google.maps.MVCArray.<LatLng>} path The poly path.
*/
/**
* Fired when the DOM `click` event is fired on the poly. Requires the clickEvents and clickable attribute to
* be true.
*
* @event google-map-poly-click
* @param {google.maps.PolyMouseEvent} event The poly event.
*/
/**
* Fired when the DOM `dblclick` event is fired on the poly. Requires the clickEvents and clickable attribute
* to be true.
*
* @event google-map-poly-dblclick
* @param {google.maps.PolyMouseEvent} event The poly event.
*/
/**
* Fired repeatedly while the user drags the poly. Requires the dragEvents attribute to be true.
*
* @event google-map-poly-drag
* @param {google.maps.MouseEvent} event The mouse event.
*/
/**
* Fired when the user stops dragging the poly. Requires the dragEvents attribute to be true.
*
* @event google-map-poly-dragend
* @param {google.maps.MouseEvent} event The mouse event.
*/
/**
* Fired when the user starts dragging the poly. Requires the dragEvents attribute to be true.
*
* @event google-map-poly-dragstart
* @param {google.maps.MouseEvent} event The mouse event.
*/
/**
* Fired when the DOM `mousedown` event is fired on the poly. Requires the mouseEvents attribute
* to be true.
*
* @event google-map-poly-mousedown
* @param {google.maps.PolyMouseEvent} event The poly event.
*/
/**
* Fired when the DOM `mousemove` event is fired on the poly. Requires the mouseEvents attribute
* to be true.
*
* @event google-map-poly-mousemove
* @param {google.maps.PolyMouseEvent} event The poly event.
*/
/**
* Fired on poly mouseout. Requires the mouseEvents attribute to be true.
*
* @event google-map-poly-mouseout
* @param {google.maps.PolyMouseEvent} event The poly event.
*/
/**
* Fired on poly mouseover. Requires the mouseEvents attribute to be true.
*
* @event google-map-poly-mouseover
* @param {google.maps.PolyMouseEvent} event The poly event.
*/
/**
* Fired when the DOM `mouseup` event is fired on the poly. Requires the mouseEvents attribute
* to be true.
*
* @event google-map-poly-mouseup
* @param {google.maps.PolyMouseEvent} event The poly event.
*/
/**
* Fired when the poly is right-clicked on. Requires the clickEvents attribute to be true.
*
* @event google-map-poly-rightclick
* @param {google.maps.PolyMouseEvent} event The poly event.
*/
/**
* Polymer properties for the google-map-poly element.
*/
properties: {
/**
* A Google Maps polyline or polygon object (depending on value of "closed" attribute).
*
* @type {google.maps.Polyline|google.maps.Polygon}
*/
poly: {
type: Object,
readOnly: true,
},
/**
* An array of the Google Maps LatLng objects that define the poly shape.
*
* @type google.maps.MVCArray.<LatLng>
*/
path: {
type: Object,
readOnly: true,
},
/**
* The Google map object.
*
* @type google.maps.Map
*/
map: {
type: Object,
observer: '_mapChanged',
},
/**
* When true, the poly will generate mouse events.
*/
clickable: {
type: Boolean,
value: false,
observer: '_clickableChanged',
},
/**
* When true, the google-map-poly-*click events will be automatically registered.
*/
clickEvents: {
type: Boolean,
value: false,
observer: '_clickEventsChanged',
},
/**
* When true, the path will be closed by connecting the last point to the first one and
* treating the poly as a polygon.
*/
closed: {
type: Boolean,
value: false,
observer: '_closedChanged',
},
/**
* When true, the google-map-poly-drag* events will be automatically registered.
*/
dragEvents: {
type: Boolean,
value: false,
observer: '_dragEventsChanged',
},
/**
* When true, the poly's vertices may be individually moved or new ones added.
*/
editable: {
type: Boolean,
value: false,
observer: '_editableChanged',
},
/**
* When true, indicates that the user has begun editing the poly path (adding vertices).
*/
editing: {
type: Boolean,
value: false,
notify: true,
readOnly: true,
},
/**
* If the path is closed, the polygon fill color. All CSS3 colors are supported except for
* extended named colors.
*/
fillColor: {
type: String,
value: '',
observer: '_fillColorChanged',
},
/**
* If the path is closed, the polygon fill opacity (between 0.0 and 1.0).
*/
fillOpacity: {
type: Number,
value: 0,
observer: '_fillOpacityChanged',
},
/**
* When true, the poly's edges are interpreted as geodesic and will follow the curvature of
* the Earth. When not set, the poly's edges are rendered as straight lines in screen space.
* Note that the poly of a geodesic poly may appear to change when dragged, as the dimensions
* are maintained relative to the surface of the earth.
*/
geodesic: {
type: Boolean,
value: false,
observer: '_geodesicChanged',
},
/**
* If the path is not closed, the icons to be rendered along the polyline.
*/
icons: {
type: Array,
value: null,
observer: '_iconsChanged',
},
/**
* When true, the google-map-poly-mouse* events will be automatically registered.
*/
mouseEvents: {
type: Boolean,
value: false,
observer: '_mouseEventsChanged',
},
/**
* The color to draw the poly's stroke with. All CSS3 colors are supported except for extended
* named colors.
*/
strokeColor: {
type: String,
value: 'black',
observer: '_strokeColorChanged',
},
/**
* The stroke opacity (between 0.0 and 1.0).
*/
strokeOpacity: {
type: Number,
value: 1,
observer: '_strokeOpacityChanged',
},
/**
* The stroke position (center, inside, or outside).
*/
strokePosition: {
type: String,
value: 'center',
observer: '_strokePositionChanged',
},
/**
* The stroke width in pixels.
*/
strokeWeight: {
type: Number,
value: 3,
observer: '_strokeWeightChanged',
},
/**
* The Z-index relative to other objects on the map.
*/
zIndex: {
type: Number,
value: 0,
observer: '_zIndexChanged',
},
},
// Lifecycle event handlers.
detached() {
if(this.poly) {
this._listeners = {};
this.poly.setMap(null);
}
if (this._pointsObserver) {
this._pointsObserver.disconnect();
this._pointsObserver = null;
}
},
attached() {
// If element is added back to DOM, put it back on the map.
this.poly && this.poly.setMap(this.map);
},
// Attribute/property change watchers.
attributeChanged(attrName) {
if (!this.poly) {
return;
}
// Cannot use *Changed watchers for native properties.
switch (attrName) {
case 'hidden':
this.poly.setVisible(!this.hidden);
break;
case 'draggable':
this.poly.setDraggable(this.draggable);
break;
}
},
_clickableChanged() {
this.poly && this.poly.set('clickable', this.clickable);
},
_clickEventsChanged() {
if (this.poly) {
if (this.clickEvents) {
this._forwardEvent('click');
this._forwardEvent('dblclick');
this._forwardEvent('rightclick');
} else {
this._clearListener('click');
this._clearListener('dblclick');
this._clearListener('rightclick');
}
}
},
_closedChanged() {
this._mapChanged();
},
_dragEventsChanged() {
if (this.poly) {
if (this.clickEvents) {
this._forwardEvent('drag');
this._forwardEvent('dragend');
this._forwardEvent('dragstart');
} else {
this._clearListener('drag');
this._clearListener('dragend');
this._clearListener('dragstart');
}
}
},
_editableChanged() {
this.poly && this.poly.setEditable(this.editable);
},
_fillColorChanged() {
this.poly && this.poly.set('fillColor', this.fillColor);
},
_fillOpacityChanged() {
this.poly && this.poly.set('fillOpacity', this.fillOpacity);
},
_geodesicChanged() {
this.poly && this.poly.set('geodesic', this.geodesic);
},
_iconsChanged() {
this.poly && this.poly.set('icons', this.icons);
},
_mapChanged() {
// Poly will be rebuilt, so disconnect existing one from old map and listeners.
if (this.poly) {
this.poly.setMap(null);
google.maps.event.clearInstanceListeners(this.poly);
}
if (this.map && this.map instanceof google.maps.Map) {
this._createPoly();
}
},
_mouseEventsChanged() {
if (this.poly) {
if (this.mouseEvents) {
this._forwardEvent('mousedown');
this._forwardEvent('mousemove');
this._forwardEvent('mouseout');
this._forwardEvent('mouseover');
this._forwardEvent('mouseup');
} else {
this._clearListener('mousedown');
this._clearListener('mousemove');
this._clearListener('mouseout');
this._clearListener('mouseover');
this._clearListener('mouseup');
}
}
},
_strokeColorChanged() {
this.poly && this.poly.set('strokeColor', this.strokeColor);
},
_strokeOpacityChanged() {
this.poly && this.poly.set('strokeOpacity', this.strokeOpacity);
},
_strokePositionChanged() {
this.poly && this.poly.set('strokePosition', this._convertStrokePosition());
},
_strokeWeightChanged() {
this.poly && this.poly.set('strokeWeight', this.strokeWeight);
},
_zIndexChanged() {
this.poly && this.poly.set('zIndex', this.zIndex);
},
// Helper logic.
_buildPathFromPoints() {
this._points = Array.prototype.slice.call(this.$.points.assignedNodes({ flatten: true }))
.filter(n => n.nodeName !== '#text');
// Build path from current points (ignoring vertex insertions while doing so).
this._building = true;
this.path.clear();
for (var i = 0, point; point = this._points[i]; ++i) {
let tagName = point.tagName;
if (tagName) {
tagName = tagName.toLowerCase();
if (tagName == 'google-map-point') {
this.path.push(point.getPosition());
}
}
}
this._building = false;
this.fire('google-map-poly-path-built', this.path);
// Watch for future updates.
if (this._pointsObserver) {
return;
}
this._pointsObserver = new MutationObserver(this._buildPathFromPoints.bind(this));
this._pointsObserver.observe(this, {
subtree: true,
attributes: true,
});
},
_clearListener(name) {
if (this._listeners[name]) {
google.maps.event.removeListener(this._listeners[name]);
this._listeners[name] = null;
}
},
_convertStrokePosition() {
return google.maps.StrokePosition && this.strokePosition ?
google.maps.StrokePosition[this.strokePosition.toUpperCase()] : 0;
},
_createPoly() {
// Build poly's path and register mutation listeners on first creation.
if (!this.path) {
this._setPath(new google.maps.MVCArray());
google.maps.event.addListener(this.path, 'insert_at', this._startEditing.bind(this));
google.maps.event.addListener(this.path, 'set_at', this._updatePoint.bind(this));
this._buildPathFromPoints();
}
const options = {
clickable: this.clickable || this.draggable, // draggable must be clickable to work.
draggable: this.draggable,
editable: this.editable,
geodesic: this.geodesic,
map: this.map,
path: this.path,
strokeColor: this.strokeColor,
strokeOpacity: this.strokeOpacity,
strokePosition: this._convertStrokePosition(),
strokeWeight: this.strokeWeight,
visible: !this.hidden,
zIndex: this.zIndex,
};
if (this.closed) {
options.fillColor = this.fillColor;
options.fillOpacity = this.fillOpacity;
this._setPoly(new google.maps.Polygon(options));
} else {
options.icons = this.icons;
this._setPoly(new google.maps.Polyline(options));
}
this._listeners = {};
this._clickEventsChanged();
this._mouseEventsChanged();
this._dragEventsChanged();
},
_forwardEvent(name) {
this._listeners[name] = google.maps.event.addListener(this.poly, name, (event) => {
this.fire(`google-map-poly-${name}`, event);
});
},
_startEditing(index) {
if (this._building) {
// Ignore changes while building path.
return;
}
// Signal start of editing when first vertex inserted, end when map clicked.
if (!this.editing) {
this._setEditing(true);
// The poly path and google-map-point elements lose sync once the user starts adding points,
// so invalidate the _points array.
this._points = null;
google.maps.event.addListenerOnce(this.map, 'click', () => {
this._setEditing(false);
this.fire('google-map-poly-path-updated', this.path);
});
}
},
_updatePoint(index, vertex) {
// Ignore changes if path is out of sync with google-map-point elements.
if (!this._points) {
return;
}
// Update existing point so bound properties are updated. too.
this._points[index].latitude = vertex.lat();
this._points[index].longitude = vertex.lng();
},
});