forked from GoogleWebComponents/google-map
-
Notifications
You must be signed in to change notification settings - Fork 0
/
google-map.html
649 lines (544 loc) · 15.9 KB
/
google-map.html
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
<!-- Copyright (c) 2014 Google Inc. All rights reserved. -->
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="../google-apis/google-apis.html">
<!--
The `google-map-marker` element represents a map marker. It is used as a
child of `google-map`.
<b>Example</b>:
<google-map latitude="37.77493" longitude="-122.41942">
<google-map-marker latitude="37.779" longitude="-122.3892"
title="Go Giants!"></google-map-marker>
</google-map>
<b>Example</b> - marker with info window (children create the window content):
<google-map-marker latitude="37.77493" longitude="-122.41942">
<img src="image.png">
</google-map-marker>
<b>Example</b> - a draggable marker:
<google-map-marker latitude="37.77493" longitude="-122.41942"
draggable="true"></google-map-marker>
<b>Example</b> - hide a marker:
<google-map-marker latitude="37.77493" longitude="-122.41942"
hidden></google-map-marker>
@element google-map-marker
@status alpha
@homepage http://googlewebcomponents.github.io/google-map
-->
<polymer-element name="google-map-marker" attributes="icon">
<template>
<style>
:host {
display: none;
}
</style>
</template>
<script>
(function() {
Polymer({
/**
* A Google Maps marker object.
*
* @property marker
* @type google.maps.Marker
* @default null
*/
marker: null,
/**
* The Google map object.
*
* @property map
* @type google.maps.Map
* @default null
*/
map: null,
/**
* A Google Map Infowindow object.
*
* @property info
* @type google.map.InfoWindow
* @default null
*/
info: null,
/**
* Image URL for the marker icon.
*
* @attribute icon
* @type string
* @default null
*/
icon: null,
publish: {
/**
* The marker's longitude coordinate.
*
* @attribute longitude
* @type number
* @default null
*/
longitude: {value: null, reflect: true},
/**
* The marker's latitude coordinate.
*
* @attribute latitude
* @type number
* @default null
*/
latitude: {value: null, reflect: true}
},
observe: {
latitude: 'updatePosition',
longitude: 'updatePosition',
},
updatePosition: function() {
if (this.marker && this.latitude != null && this.longitude != null) {
this.marker.setPosition({
lat: parseFloat(this.latitude),
lng: parseFloat(this.longitude)
});
}
},
iconChanged: function() {
if (this.marker) {
this.marker.setIcon(this.icon);
}
},
mapChanged: function() {
if (this.map && this.map instanceof google.maps.Map) {
this.mapReady();
}
},
contentChanged: function() {
this.onMutation(this, this.contentChanged); // Watch for future updates.
var content = this.innerHTML;
if (content) {
if (!this.info) {
// Create a new infowindow
this.info = new google.maps.InfoWindow();
this.infoHandler_ = google.maps.event.addListener(this.marker, 'click', function() {
this.info.open(this.map, this.marker);
}.bind(this));
}
this.info.setContent(content);
} else {
if (this.info) {
// Destroy the existing infowindow. It doesn't make sense to have an empty one.
google.maps.event.removeListener(this.infoHandler_);
this.info = null;
}
}
},
mapReady: function() {
this.marker = new google.maps.Marker({
map: this.map,
position: new google.maps.LatLng(this.latitude, this.longitude),
title: this.title,
draggable: this.draggable,
visible: !this.hidden,
icon: this.icon
});
this.contentChanged();
setupDragHandler_.bind(this)();
},
attributeChanged: function(attrName, oldVal, newVal) {
if (!this.marker) {
return;
}
// Cannot use *Changed watchers for native properties.
switch (attrName) {
case 'hidden':
this.marker.setVisible(!this.hidden);
break;
case 'draggable':
this.marker.setDraggable(this.draggable);
setupDragHandler_.bind(this)();
break;
case 'title':
this.marker.setTitle(this.title);
break;
}
}
});
function onDragEnd_(e, details, sender) {
this.latitude = e.latLng.lat();
this.longitude = e.latLng.lng();
}
function setupDragHandler_() {
if (this.draggable) {
this.dragHandler_ = google.maps.event.addListener(
this.marker, 'dragend', onDragEnd_.bind(this));
} else {
google.maps.event.removeListener(this.dragHandler_);
this.dragHandler_ = null;
}
}
})();
</script>
</polymer-element>
<!--
The `google-map` element renders a Google Map.
<b>Example</b>:
<style>
google-map {
display: block;
height: 600px;
}
</style>
<google-map latitude="37.77493" longitude="-122.41942"></google-map>
<b>Example</b> - add markers to the map and ensure they're in view:
<google-map latitude="37.77493" longitude="-122.41942" fitToMarkers>
<google-map-marker latitude="37.779" longitude="-122.3892"
draggable="true" title="Go Giants!"></google-map-marker>
<google-map-marker latitude="37.777" longitude="-122.38911"></google-map-marker>
</google-map>
<b>Example</b>:
<google-map disableDefaultUI showCenterMarker zoom="15"></google-map>
<script>
var map = document.querySelector('google-map');
map.latitude = 37.77493;
map.longitude = -122.41942;
map.addEventListener('google-map-ready', function(e) {
alert('Map loaded!');
});
</script>
<b>Example</b> - with Google directions, using data-binding inside another Polymer element
<google-map map="{{map}}"></google-map>
<google-map-directions map="{{map}}"
startAddress="San Francisco" endAddress="Mountain View">
</google-map-directions>
<b>Example</b> - not all map options are included as attributes out of the box.
To add custom map options, you can extend this element:
<polymer-element name="my-google-map" extends="google-map">
<script>
Polymer({
getMapOptions: function() {
var mapOptions = this.super();
mapOptions["streetViewControl"] = false;
return mapOptions;
}
});
</script>
</polymer-element>
@element google-map
@homepage http://googlewebcomponents.github.io/google-map
@blurb Element wrapper around Google Maps API.
-->
<!--
Fired when the Maps API has fully loaded.
@event google-map-ready
-->
<!-- TODO(ericbidelman):
- Handle removals and support .innerHTML = ''.
- Use <google-maps-api>.api instead of google.maps namespace.
-->
<polymer-element name="google-map" attributes="apiKey latitude longitude zoom showCenterMarker version map mapType disableDefaultUI fitToMarkers zoomable styles maxZoom minZoom libraries">
<template>
<style>
:host {
position: relative;
}
#map {
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
}
</style>
<google-maps-api apiKey="{{apiKey}}" version="{{version}}" on-api-load="{{mapApiLoaded}}" libraries="{{libraries}}"></google-maps-api>
<div id="map"></div>
<content id="markers" select="google-map-marker"></content>
</template>
<script>
Polymer({
/**
* A Maps API key. To obtain an API key, see developers.google.com/maps/documentation/javascript/tutorial#api_key.
*
* @property apiKey
* @type string
*/
apiKey: null,
/**
* A latitude to center the map on.
*
* @attribute latitude
* @type number
* @default 37.77493
*/
latitude: 37.77493,
/**
* The libraries to load with this map. Defaults to "places". For more information
* https://developers.google.com/maps/documentation/javascript/libraries
*
* @attribute libraries
* @type string
* @default "places"
*/
libraries: "places",
/**
* A longitude to center the map on.
*
* @attribute longitude
* @type number
* @default -122.41942
*/
longitude: -122.41942,
/**
* A zoom level to set the map to.
*
* @attribute zoom
* @type number
* @default 10
*/
zoom: 10,
/**
* When set, displays a map marker at the center point.
*
* @attribute showCenterMarker
* @type boolean
* @default false
*/
showCenterMarker: false,
/**
* Map type to display. One of 'roadmap', 'satellite', 'hybrid', 'terrain'.
*
* @attribute mapType
* @type string
* @default roadmap
*/
mapType: 'roadmap', // roadmap, satellite, hybrid, terrain
/**
* Version of the Google Maps API to use.
*
* @attribute version
* @type string
* @default 3.exp
*/
version: '3.exp',
/**
* If set, removes the map's default UI controls.
*
* @attribute disableDefaultUI
* @type boolean
* @default false
*/
disableDefaultUI: false,
/**
* If set, the zoom level is set such that all markers (google-map-marker children) are brought into view.
*
* @attribute fitToMarkers
* @type boolean
* @default false
*/
fitToMarkers: false,
/**
* If false, prevent the user from zooming the map interactively.
*
* @attribute zoomable
* @type boolean
* @default true
*/
zoomable: true,
/**
* If set, custom styles can be applied to the map.
* For style doucmentation see developers.google.com/maps/documentation/javascript/reference#MapTypeStyle
*
* @attribute styles
* @type JSON encoded array
* @default null
*/
styles: {},
/**
* A maximum zoom level which will be displayed on the map.
*
* @attribute maxZoom
* @type number
* @default null
*/
maxZoom: null,
/**
* A minimum zoom level which will be displayed on the map.
*
* @attribute minZoom
* @type number
* @default null
*/
minZoom: null,
observe: {
latitude: 'updateCenter',
longitude: 'updateCenter'
},
created: function() {
this.markers = [];
},
attached: function() {
this.resize();
},
mapApiLoaded: function() {
this.map = new google.maps.Map(this.$.map, this.getMapOptions());
this.updateCenter();
this.updateMarkers();
this.addMapListeners();
this.fire('google-map-ready');
},
getMapOptions: function() {
var mapOptions = {
zoom: this.zoom,
mapTypeId: this.mapType,
disableDefaultUI: this.disableDefaultUI,
disableDoubleClickZoom: !this.zoomable,
scrollwheel: this.zoomable,
styles: this.styles,
maxZoom: Number(this.maxZoom),
minZoom: Number(this.minZoom)
};
// Only override the default if set.
// We use getAttribute here because the default value of this.draggable = false even when not set.
if (this.getAttribute("draggable") != null) {
mapOptions.draggable = this.draggable
}
return mapOptions;
},
updateMarkers: function() {
this.markers = Array.prototype.slice.call(
this.$.markers.getDistributedNodes());
if (this.centerMarker) {
this.markers.push(this.centerMarker);
}
this.onMutation(this, this.updateMarkers); // Watch for future updates.
// Set the map on each marker and zoom viewport to ensure they're in view.
if (this.markers.length && this.map) {
for (var i = 0, m; m = this.markers[i]; ++i) {
m.map = this.map;
}
if (this.fitToMarkers) {
this.fitToMarkersChanged();
}
}
},
/**
* Clears all markers from the map.
*
* @method clear
*/
clear: function() {
for (var i = 0, m; m = this.markers[i]; ++i) {
m.marker.setMap(null);
}
},
resize: function() {
if (this.map) {
google.maps.event.trigger(this.map, 'resize');
this.updateCenter();
}
},
updateCenter: function() {
if (!this.map || this.latitude == null || this.longitude == null ||
typeof this.latitude === 'object' ||
typeof this.longitude === 'object') {
return;
}
var newCenter = new google.maps.LatLng(this.latitude, this.longitude);
var oldCenter = this.map.getCenter();
if (!oldCenter) {
// If the map does not have a center, set it right away.
this.map.setCenter(newCenter);
} else {
// Using google.maps.LatLng returns corrected lat/lngs.
oldCenter = new google.maps.LatLng(oldCenter.lat(), oldCenter.lng());
// If the map currently has a center, slowly pan to the new one.
if (!oldCenter.equals(newCenter)) {
this.map.panTo(newCenter);
}
}
this.showCenterMarkerChanged();
},
zoomChanged: function() {
if (this.map) {
this.map.setZoom(Number(this.zoom));
}
},
maxZoomChanged: function() {
if (this.map) {
this.map.setOptions({maxZoom: Number(this.maxZoom)});
}
},
minZoomChanged: function() {
if (this.map) {
this.map.setOptions({minZoom: Number(this.minZoom)});
}
},
mapTypeChanged: function() {
if (this.map) {
this.map.setMapTypeId(this.mapType);
}
},
showCenterMarkerChanged: function() {
if (!this.map) {
return;
}
if (this.showCenterMarker) {
if (!this.centerMarker) {
this.centerMarker = document.createElement('google-map-marker');
}
var center = this.map.getCenter();
this.centerMarker.latitude = center.lat();
this.centerMarker.longitude = center.lng();
} else {
if (this.centerMarker) {
//TODO(bamnet): Clean up markers so they can be removed easier from a map.
this.centerMarker.marker.setMap(null);
this.centerMarker = null;
}
}
this.updateMarkers();
},
disableDefaultUIChanged: function() {
if (!this.map) {
return;
}
this.map.setOptions({disableDefaultUI: this.disableDefaultUI});
},
zoomableChanged: function() {
if (!this.map) {
return;
}
this.map.setOptions({
disableDoubleClickZoom: !this.zoomable,
scrollwheel: this.zoomable
});
},
attributeChanged: function(attrName, oldVal, newVal) {
if (!this.map) {
return;
}
// Cannot use *Changed watchers for native properties.
switch (attrName) {
case 'draggable':
this.map.setOptions({draggable: this.draggable});
break;
}
},
fitToMarkersChanged: function() {
// TODO(ericbidelman): respect user's zoom level.
if (this.map && this.fitToMarkers) {
var latLngBounds = new google.maps.LatLngBounds();
for (var i = 0, m; m = this.markers[i]; ++i) {
latLngBounds.extend(
new google.maps.LatLng(m.latitude, m.longitude));
}
// For one marker, don't alter zoom, just center it.
if (this.markers.length > 1) {
this.map.fitBounds(latLngBounds);
}
this.map.setCenter(latLngBounds.getCenter());
}
},
addMapListeners: function() {
google.maps.event.addListener(this.map, 'center_changed', function() {
var center = this.map.getCenter();
this.latitude = center.lat();
this.longitude = center.lng();
}.bind(this));
}
});
</script>
</polymer-element>