forked from GoogleWebComponents/google-map
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgoogle-map.html
547 lines (457 loc) · 13.4 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
<!--
Copyright (c) 2014 The Polymer Project Authors. All rights reserved.
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
Code distributed by Google as part of the polymer project is also
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
-->
<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 github.io
-->
<polymer-element name="google-map-marker" attributes="icon">
<template>
<style>
:host {
display: none;
}
</style>
</template>
<script>
(function() {
Polymer('google-map-marker', {
/**
* 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: this.latitude, lng: 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>
@element google-map
@homepage github.io
@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">
<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}}"></google-maps-api>
<div id="map"></div>
<content id="markers" select="google-map-marker"></content>
</template>
<script>
Polymer('google-map', {
/**
* 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,
/**
* 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,
observe: {
latitude: 'updateCenter',
longitude: 'updateCenter'
},
created: function() {
this.markers = [];
},
attached: function() {
this.resize();
},
mapApiLoaded: function() {
var mapOptions = {
zoom: this.zoom,
mapTypeId: this.mapType,
disableDefaultUI: this.disableDefaultUI,
disableDoubleClickZoom: !this.zoomable,
scrollwheel: this.zoomable
};
// 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
}
this.map = new google.maps.Map(this.$.map, mapOptions);
this.updateCenter();
this.updateMarkers();
this.fire('google-map-ready');
},
updateMarkers: function() {
this.markers = Array.prototype.slice.call(
this.$.markers.getDistributedNodes());
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;
}
this.map.panTo(
new google.maps.LatLng(this.latitude, this.longitude));
this.showCenterMarkerChanged();
},
zoomChanged: function() {
if (this.map) {
this.map.setZoom(Number(this.zoom));
}
},
mapTypeChanged: function() {
if (this.map) {
this.map.setMapTypeId(this.mapType);
}
},
showCenterMarkerChanged: function() {
if (!this.map) {
return;
}
if (!this.centerMarker && this.showCenterMarker) {
this.centerMarker = new google.maps.Marker({
map: this.map
});
}
if (this.centerMarker) {
this.centerMarker.setPosition(this.map.getCenter());
this.centerMarker.setMap(this.showCenterMarker ? this.map : null);
}
},
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());
}
}
});
</script>
</polymer-element>