Skip to content
This repository was archived by the owner on Mar 13, 2018. It is now read-only.

Commit ccc10bb

Browse files
committed
google-map: load map api dynamically; add metadata
1 parent bd1c349 commit ccc10bb

File tree

4 files changed

+110
-50
lines changed

4 files changed

+110
-50
lines changed

google-map/google-map.html

Lines changed: 101 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -4,58 +4,110 @@
44
license that can be found in the LICENSE file.
55
-->
66
<polymer-element name="google-map" attributes="latitude longitude zoom showCenterMarker">
7+
<template>
8+
<style>
9+
@host {
10+
* {
11+
position: relative;
12+
}
13+
}
14+
15+
#map {
16+
position: absolute;
17+
top: 0;
18+
right: 0;
19+
bottom: 0;
20+
left: 0;
21+
}
22+
</style>
23+
24+
<div id="map"></div>
25+
</template>
726
<script>
8-
Polymer('google-map', {
9-
latitude: '-34.397',
10-
longitude: '150.644',
11-
zoom: 10,
12-
showCenterMarker: false,
13-
created: function() {
14-
var options = {
15-
zoom: this.zoom,
16-
center: new google.maps.LatLng(this.latitude, this.longitude)
17-
};
18-
this.map = new google.maps.Map(this, options);
19-
},
20-
enteredDocument: function() {
21-
this.resize();
22-
},
23-
resize: function() {
24-
google.maps.event.trigger(this.map, 'resize');
25-
this.updateCenter();
26-
},
27-
latitudeChanged: function() {
28-
this.asyncUpdateCenter();
29-
},
30-
longitudeChanged: function() {
31-
this.asyncUpdateCenter();
32-
},
33-
zoomChanged: function() {
34-
this.map.setZoom(this.zoom);
35-
},
36-
showCenterMarkerChanged: function() {
37-
if (this.centerMarker) {
38-
this.centerMarker.setMap(this.showCenterMarker ? this.map : null);
27+
(function() {
28+
var CALLBACK_NAME = 'polymer_google_map_callback';
29+
var MAP_URL = 'https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false&callback=' + CALLBACK_NAME;
30+
var pendingCallbacks = [];
31+
var loading;
32+
33+
function loadMapApi(cb) {
34+
if (window.google && window.google.maps) {
35+
cb();
36+
return;
3937
}
40-
},
41-
asyncUpdateCenter: function() {
42-
this.updateCenterJob = this.job(
43-
this.updateCenterJob, this.updateCenter, 0);
44-
},
45-
updateCenter: function() {
46-
this.map.setCenter(
47-
new google.maps.LatLng(this.latitude, this.longitude));
48-
this.updateCenterMarker();
49-
},
50-
updateCenterMarker: function() {
51-
if (!this.centerMarker) {
52-
this.centerMarker = new google.maps.Marker({
53-
map: this.map
54-
});
38+
if (!loading) {
39+
loading = true;
40+
window[CALLBACK_NAME] = mapApiLoaded.bind(this);
41+
var s = document.createElement('script');
42+
s.src = MAP_URL;
43+
document.head.appendChild(s);
5544
}
56-
this.centerMarker.setPosition(this.map.getCenter());
57-
this.showCenterMarkerChanged();
45+
pendingCallbacks.push(cb);
5846
}
59-
});
47+
48+
function mapApiLoaded() {
49+
pendingCallbacks.forEach(function(cb) {
50+
cb();
51+
});
52+
}
53+
54+
Polymer('google-map', {
55+
latitude: '-34.397',
56+
longitude: '150.644',
57+
zoom: 10,
58+
showCenterMarker: false,
59+
observe: {
60+
latitude: 'updateCenter',
61+
longitude: 'updateCenter'
62+
},
63+
ready: function() {
64+
loadMapApi(this.mapReady.bind(this));
65+
},
66+
enteredView: function() {
67+
this.resize();
68+
},
69+
mapReady: function() {
70+
this.map = new google.maps.Map(this.$.map, {
71+
zoom: this.zoom,
72+
center: new google.maps.LatLng(this.latitude, this.longitude)
73+
});
74+
this.showCenterMarkerChanged();
75+
this.fire('google-map-ready');
76+
},
77+
resize: function() {
78+
if (this.map) {
79+
google.maps.event.trigger(this.map, 'resize');
80+
this.updateCenter();
81+
}
82+
},
83+
updateCenter: function() {
84+
if (!this.map) {
85+
return;
86+
}
87+
this.map.setCenter(
88+
new google.maps.LatLng(this.latitude, this.longitude));
89+
this.showCenterMarkerChanged();
90+
},
91+
zoomChanged: function() {
92+
if (this.map) {
93+
this.map.setZoom(Number(this.zoom));
94+
}
95+
},
96+
showCenterMarkerChanged: function() {
97+
if (!this.map) {
98+
return;
99+
}
100+
if (!this.centerMarker && this.showCenterMarker) {
101+
this.centerMarker = new google.maps.Marker({
102+
map: this.map
103+
});
104+
}
105+
if (this.centerMarker) {
106+
this.centerMarker.setPosition(this.map.getCenter());
107+
this.centerMarker.setMap(this.showCenterMarker ? this.map : null);
108+
}
109+
}
110+
});
111+
})();
60112
</script>
61113
</polymer-element>

google-map/index.html

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
<head>
99
<title>Google Map</title>
1010
<script src="../../polymer/polymer.js"></script>
11-
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
1211
<link rel="import" href="google-map.html">
1312
<style>
1413
google-map {

google-map/metadata.html

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<x-meta id="google-map" label="Google Map">
2+
<template>
3+
<google-map style="width: 400px; height: 400px;"></google-map>
4+
</template>
5+
<template id="imports">
6+
<link rel="import" href="google-map.html">
7+
</template>
8+
</x-meta>

metadata.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<link rel="import" href="smoothie-chart/metadata.html">
2+
<link rel="import" href="google-map/metadata.html">
23

34
<!--
45
<x-meta id="ace-element" label="Ace Editor"></x-meta>

0 commit comments

Comments
 (0)