1/* 2 * Google layer using Google Maps API 3 */ 4 5/* global google: true */ 6 7L.Google = L.Class.extend({ 8 includes: L.Mixin.Events, 9 10 options: { 11 minZoom: 0, 12 maxZoom: 18, 13 tileSize: 256, 14 subdomains: 'abc', 15 errorTileUrl: '', 16 attribution: '', 17 opacity: 1, 18 continuousWorld: false, 19 noWrap: false, 20 mapOptions: { 21 backgroundColor: '#dddddd' 22 } 23 }, 24 25 // Possible types: SATELLITE, ROADMAP, HYBRID, TERRAIN 26 initialize: function(type, options) { 27 L.Util.setOptions(this, options); 28 29 this._ready = google.maps.Map !== undefined; 30 if (!this._ready) L.Google.asyncWait.push(this); 31 32 this._type = type || 'SATELLITE'; 33 }, 34 35 onAdd: function(map, insertAtTheBottom) { 36 this._map = map; 37 this._insertAtTheBottom = insertAtTheBottom; 38 39 // create a container div for tiles 40 this._initContainer(); 41 this._initMapObject(); 42 43 // set up events 44 map.on('viewreset', this._resetCallback, this); 45 46 this._limitedUpdate = L.Util.limitExecByInterval(this._update, 150, this); 47 map.on('move', this._update, this); 48 49 map.on('zoomanim', this._handleZoomAnim, this); 50 51 //20px instead of 1em to avoid a slight overlap with google's attribution 52 map._controlCorners.bottomright.style.marginBottom = '20px'; 53 54 this._reset(); 55 this._update(); 56 }, 57 58 onRemove: function(map) { 59 this._map._container.removeChild(this._container); 60 //this._container = null; 61 62 this._map.off('viewreset', this._resetCallback, this); 63 64 this._map.off('move', this._update, this); 65 66 this._map.off('zoomanim', this._handleZoomAnim, this); 67 68 map._controlCorners.bottomright.style.marginBottom = '0em'; 69 //this._map.off('moveend', this._update, this); 70 }, 71 72 getAttribution: function() { 73 return this.options.attribution; 74 }, 75 76 setOpacity: function(opacity) { 77 this.options.opacity = opacity; 78 if (opacity < 1) { 79 L.DomUtil.setOpacity(this._container, opacity); 80 } 81 }, 82 83 setElementSize: function(e, size) { 84 e.style.width = size.x + 'px'; 85 e.style.height = size.y + 'px'; 86 }, 87 88 _initContainer: function() { 89 var tilePane = this._map._container, 90 first = tilePane.firstChild; 91 92 if (!this._container) { 93 this._container = L.DomUtil.create('div', 'leaflet-google-layer leaflet-top leaflet-left'); 94 this._container.id = '_GMapContainer_' + L.Util.stamp(this); 95 this._container.style.zIndex = 'auto'; 96 } 97 98 tilePane.insertBefore(this._container, first); 99 100 this.setOpacity(this.options.opacity); 101 this.setElementSize(this._container, this._map.getSize()); 102 }, 103 104 _initMapObject: function() { 105 if (!this._ready) return; 106 this._google_center = new google.maps.LatLng(0, 0); 107 var map = new google.maps.Map(this._container, { 108 center: this._google_center, 109 zoom: 0, 110 tilt: 0, 111 mapTypeId: google.maps.MapTypeId[this._type], 112 disableDefaultUI: true, 113 keyboardShortcuts: false, 114 draggable: false, 115 disableDoubleClickZoom: true, 116 scrollwheel: false, 117 streetViewControl: false, 118 styles: this.options.mapOptions.styles, 119 backgroundColor: this.options.mapOptions.backgroundColor 120 }); 121 122 var _this = this; 123 this._reposition = google.maps.event.addListenerOnce(map, 'center_changed', 124 function() { _this.onReposition(); }); 125 this._google = map; 126 127 google.maps.event.addListenerOnce(map, 'idle', 128 function() { _this._checkZoomLevels(); }); 129 }, 130 131 _checkZoomLevels: function() { 132 //setting the zoom level on the Google map may result in a different zoom level than the one requested 133 //(it won't go beyond the level for which they have data). 134 // verify and make sure the zoom levels on both Leaflet and Google maps are consistent 135 if (this._google.getZoom() !== this._map.getZoom()) { 136 //zoom levels are out of sync. Set the leaflet zoom level to match the google one 137 this._map.setZoom( this._google.getZoom() ); 138 } 139 }, 140 141 _resetCallback: function(e) { 142 this._reset(e.hard); 143 }, 144 145 _reset: function(clearOldContainer) { 146 this._initContainer(); 147 }, 148 149 _update: function(e) { 150 if (!this._google) return; 151 this._resize(); 152 153 var center = e && e.latlng ? e.latlng : this._map.getCenter(); 154 var _center = new google.maps.LatLng(center.lat, center.lng); 155 156 this._google.setCenter(_center); 157 this._google.setZoom(this._map.getZoom()); 158 159 this._checkZoomLevels(); 160 //this._google.fitBounds(google_bounds); 161 }, 162 163 _resize: function() { 164 var size = this._map.getSize(); 165 if (this._container.style.width === size.x && 166 this._container.style.height === size.y) 167 return; 168 this.setElementSize(this._container, size); 169 this.onReposition(); 170 }, 171 172 173 _handleZoomAnim: function (e) { 174 var center = e.center; 175 var _center = new google.maps.LatLng(center.lat, center.lng); 176 177 this._google.setCenter(_center); 178 this._google.setZoom(e.zoom); 179 }, 180 181 182 onReposition: function() { 183 if (!this._google) return; 184 google.maps.event.trigger(this._google, 'resize'); 185 } 186}); 187 188L.Google.asyncWait = []; 189L.Google.asyncInitialize = function() { 190 var i; 191 for (i = 0; i < L.Google.asyncWait.length; i++) { 192 var o = L.Google.asyncWait[i]; 193 o._ready = true; 194 if (o._container) { 195 o._initMapObject(); 196 o._update(); 197 } 198 } 199 L.Google.asyncWait = []; 200};
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.