|
| 1 | +// source: https://github.com/emikhalev/leaflet-2gis |
| 2 | +// author: Eugene Mikhalev |
| 3 | +// license: MIT |
| 4 | +L.DGis = L.Class.extend({ |
| 5 | + includes: L.Mixin.Events, |
| 6 | + |
| 7 | + options: { |
| 8 | + attribution: '', |
| 9 | + opacity: 0.99 |
| 10 | + }, |
| 11 | + |
| 12 | + // Init object |
| 13 | + initialize: function(options) { |
| 14 | + L.Util.setOptions(this, options); |
| 15 | + }, |
| 16 | + |
| 17 | + // Layer events |
| 18 | + onAdd: function(map, insertAtTheBottom) { |
| 19 | + this._map = map; |
| 20 | + this._insertAtTheBottom = insertAtTheBottom; |
| 21 | + |
| 22 | + // Create map container |
| 23 | + this._initContainer(); |
| 24 | + |
| 25 | + // Deferred loading |
| 26 | + var th = this; |
| 27 | + window.DG.load(function() { |
| 28 | + // Create map object |
| 29 | + th._initMapObject(); |
| 30 | + |
| 31 | + // Setting events handlers |
| 32 | + th._setHandlers(); |
| 33 | + |
| 34 | + map._controlCorners.bottomright.style.marginBottom = "3em"; |
| 35 | + th._reset(); |
| 36 | + }); |
| 37 | + }, |
| 38 | + onRemove: function() { |
| 39 | + // Remove map container |
| 40 | + this._map._container.removeChild(this._container); |
| 41 | + // Unset events handlers |
| 42 | + this._unsetHandlers(); |
| 43 | + this._map._controlCorners.bottomright.style.marginBottom = "0em"; |
| 44 | + }, |
| 45 | + |
| 46 | + // Methods |
| 47 | + getAttribution: function() { |
| 48 | + return this.options.attribution; |
| 49 | + }, |
| 50 | + |
| 51 | + setOpacity: function(opacity) { |
| 52 | + this.options.opacity = opacity; |
| 53 | + if (opacity < 1) { |
| 54 | + L.DomUtil.setOpacity(this._container, opacity); |
| 55 | + } |
| 56 | + }, |
| 57 | + |
| 58 | + setElementSize: function(e, size) { |
| 59 | + e.style.width = size.x + "px"; |
| 60 | + e.style.height = size.y + "px"; |
| 61 | + }, |
| 62 | + |
| 63 | + // Helpers |
| 64 | + _initContainer: function() { |
| 65 | + var tilePane = this._map._container, |
| 66 | + first = tilePane.firstChild; |
| 67 | + |
| 68 | + if (!this._container) { |
| 69 | + this._container = L.DomUtil.create('div', 'leaflet-2gis-layer leaflet-top leaflet-left leaflet-zoom-hide'); |
| 70 | + this._container.id = "_2GisContainer_" + L.Util.stamp(this); |
| 71 | + this._container.style.zIndex = "auto"; |
| 72 | + } |
| 73 | + |
| 74 | + if (this.options.overlay) { |
| 75 | + first = this._map._container.getElementsByClassName('leaflet-map-pane')[0]; |
| 76 | + first = first.nextSibling; |
| 77 | + // XXX: Bug with layer order |
| 78 | + if (L.Browser.opera) |
| 79 | + this._container.className += " leaflet-objects-pane"; |
| 80 | + } |
| 81 | + tilePane.insertBefore(this._container, first); |
| 82 | + |
| 83 | + this.setOpacity(this.options.opacity); |
| 84 | + this.setElementSize(this._container, this._map.getSize()); |
| 85 | + }, |
| 86 | + |
| 87 | + _initMapObject: function() { |
| 88 | + // Init main object |
| 89 | + if (typeof this._map == "undefined") { |
| 90 | + return; |
| 91 | + } |
| 92 | + if (typeof this._dg == "undefined") { |
| 93 | + this._dg = new window.DG.Map(this._container); |
| 94 | + this._dg.fullscreen.disable(); |
| 95 | + this._dg.geoclicker.disable(); |
| 96 | + } |
| 97 | + |
| 98 | + // Set map center and zoom |
| 99 | + this._setCenter(); |
| 100 | + |
| 101 | + var zoom = this._map.getZoom(); |
| 102 | + this._dg.setZoom(zoom); |
| 103 | + }, |
| 104 | + |
| 105 | + _setCenter: function(){ |
| 106 | + var center = this._map.getCenter(); |
| 107 | + this._dg.setCenter(new window.DG.GeoPoint(center.lng, center.lat) ); |
| 108 | + }, |
| 109 | + |
| 110 | + _setHandlers: function(){ |
| 111 | + this._map.on('viewreset', this._resetCallback, this); |
| 112 | + this._limitedUpdate = L.Util.limitExecByInterval(this._move, 150, this); |
| 113 | + this._map.on('move', this._move, this); |
| 114 | + this._map.on('moveend', this._move, this); |
| 115 | + }, |
| 116 | + |
| 117 | + _unsetHandlers: function(){ |
| 118 | + this._map.off('viewreset', this._resetCallback, this); |
| 119 | + this._map.off('move', this._move, this); |
| 120 | + this._map.off('moveend', this._move, this); |
| 121 | + }, |
| 122 | + |
| 123 | + // Event handlers |
| 124 | + _resetCallback: function(e) { |
| 125 | + this._reset(e.hard); |
| 126 | + }, |
| 127 | + |
| 128 | + _reset: function() { |
| 129 | + this._initContainer(); |
| 130 | + }, |
| 131 | + |
| 132 | + _move: function(force) { |
| 133 | + if (typeof this._dg == "undefined") { |
| 134 | + return; |
| 135 | + } |
| 136 | + if (!this._map) return; |
| 137 | + this._resize(force); |
| 138 | + // Zoom |
| 139 | + var zoom = this._map.getZoom(); |
| 140 | + if (force || this._dg.getZoom() != zoom) |
| 141 | + this._dg.setZoom(zoom); |
| 142 | + |
| 143 | + // Position |
| 144 | + var llCenter = this._map.getCenter(); // L.LatLng |
| 145 | + var dgCenter = this._dg.getCenter(); // DG.GeoPoint |
| 146 | + var dgllCenter = [dgCenter.lat, dgCenter.lon]; // L.LatLng <- DG.GeoPoint |
| 147 | + |
| 148 | + var llCenterPx = this._map.project(llCenter); // L.Point |
| 149 | + var dgCenterPx = this._map.project(dgllCenter); // L.Point |
| 150 | + |
| 151 | + var offsetX = dgCenterPx.x - llCenterPx.x; |
| 152 | + var offsetY = dgCenterPx.y - llCenterPx.y; |
| 153 | + |
| 154 | + this._dg.moveE( -offsetX ); // X-offset (East) |
| 155 | + this._dg.moveN( offsetY ); // Y-offset (North) |
| 156 | + |
| 157 | + }, |
| 158 | + |
| 159 | + _resize: function(force) { |
| 160 | + if (typeof this._dg == "undefined") { |
| 161 | + return; |
| 162 | + } |
| 163 | + var size = this._map.getSize(), style = this._container.style; |
| 164 | + if (style.width == size.x + "px" && style.height == size.y + "px" && !force) |
| 165 | + return; |
| 166 | + this.setElementSize(this._container, size); |
| 167 | + //var b = this._map.getBounds(), sw = b.getSouthWest(), ne = b.getNorthEast(); |
| 168 | + this._dg.redraw(); |
| 169 | + } |
| 170 | +}); |
| 171 | + |
| 172 | +(function() { |
| 173 | + var el = document.createElement('script'); |
| 174 | + el.type = 'text/javascript'; |
| 175 | + el.src = 'http://maps.api.2gis.ru/1.0?loadByRequire=1'; |
| 176 | + document.getElementsByTagName('head')[0].appendChild(el); |
| 177 | +})(); |
| 178 | + |
| 179 | +if( !('layerList' in window) ) |
| 180 | + window.layerList = { list: {} }; |
| 181 | +window.layerList.list["2GIS"] = "new L.DGis()"; |
0 commit comments