44license 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 >
0 commit comments