mirror of
https://github.com/netzmacht/contao-leaflet-libraries.git
synced 2025-11-28 11:04:07 +01:00
1 line
5.3 KiB
JavaScript
1 line
5.3 KiB
JavaScript
L.Control.MinZoomIndicator=L.Control.extend({options:{position:"bottomleft"},_layers:{},initialize:function(t){L.Util.setOptions(this,t),this._layers=new Object},_addLayer:function(t){var e=15;t.options.minzoom&&(e=t.options.minzoom),this._layers[t._leaflet_id]=e,this._updateBox(null)},_removeLayer:function(t){this._layers[t._leaflet_id]=null,this._updateBox(null)},_getMinZoomLevel:function(){var t=-1;for(var e in this._layers)null!=this._layers[e]&&this._layers[e]>t&&(t=this._layers[e]);return t},onAdd:function(t){this._map=t,t.zoomIndicator=this;var e=this.className,o=this._container=L.DomUtil.create("div",e);return t.on("moveend",this._updateBox,this),this._updateBox(null),o},onRemove:function(t){L.Control.prototype.onRemove.call(this,t),t.off({moveend:this._updateBox},this),this._map=null},_updateBox:function(t){null!=t&&L.DomEvent.preventDefault(t);var e=this._getMinZoomLevel();this._container.innerHTML=-1==e?this.options.minZoomMessageNoLayer:this.options.minZoomMessage.replace(/CURRENTZOOM/,this._map.getZoom()).replace(/MINZOOMLEVEL/,e),this._container.style.display=this._map.getZoom()>=e?"none":"block"},className:"leaflet-control-minZoomIndicator"}),L.LatLngBounds.prototype.toOverpassBBoxString=function(){var t=this._southWest,e=this._northEast;return[t.lat,t.lng,e.lat,e.lng].join(",")},L.OverPassLayer=L.FeatureGroup.extend({options:{debug:!1,minzoom:15,endpoint:"http://overpass-api.de/api/",query:"(node(BBOX)[organic];node(BBOX)[second_hand];);out qt;",callback:function(t){for(var e=0;e<t.elements.length;e++){var o=t.elements[e];if(o.id in this.instance._ids)return;this.instance._ids[o.id]=!0;var n;n="node"==o.type?new L.LatLng(o.lat,o.lon):new L.LatLng(o.center.lat,o.center.lon);var i=this.instance._poiInfo(o.tags,o.id),s=L.circle(n,50,{color:"green",fillColor:"#3f0",fillOpacity:.5}).bindPopup(i);this.instance.addLayer(s)}},beforeRequest:function(){this.options.debug&&console.debug("about to query the OverPassAPI")},afterRequest:function(){this.options.debug&&console.debug("all queries have finished!")},minZoomIndicatorOptions:{position:"bottomleft",minZoomMessageNoLayer:"no layer assigned",minZoomMessage:"current Zoom-Level: CURRENTZOOM all data at Level: MINZOOMLEVEL"}},initialize:function(t){L.Util.setOptions(this,t),this._layers={},this._ids={},this._requested={}},_poiInfo:function(t,e){var o=document.createElement("a");o.href="http://www.openstreetmap.org/edit?editor=id&node="+e,o.appendChild(document.createTextNode("Edit this entry in iD"));var n=document.createElement("table");for(var i in t){var s=n.insertRow(0);s.insertCell(0).appendChild(document.createTextNode(i)),s.insertCell(1).appendChild(document.createTextNode(t[i]))}var a=document.createElement("div");return a.appendChild(o),a.appendChild(n),a},long2tile:function(t,e){return Math.floor((t+180)/360*Math.pow(2,e))},lat2tile:function(t,e){return Math.floor((1-Math.log(Math.tan(t*Math.PI/180)+1/Math.cos(t*Math.PI/180))/Math.PI)/2*Math.pow(2,e))},tile2long:function(t,e){return t/Math.pow(2,e)*360-180},tile2lat:function(t,e){var o=Math.PI-2*Math.PI*t/Math.pow(2,e);return 180/Math.PI*Math.atan(.5*(Math.exp(o)-Math.exp(-o)))},_view2BBoxes:function(t,e,o,n){for(var i=14,s=this.long2tile(t,i),a=this.long2tile(o,i),r=this.lat2tile(n,i),l=this.lat2tile(e,i),h=new Array,d=s;a>=d;d++)for(var u=r;l>=u;u++){var p=Math.round(1e6*this.tile2long(d,i))/1e6,c=Math.round(1e6*this.tile2long(d+1,i))/1e6,m=Math.round(1e6*this.tile2lat(u,i))/1e6,_=Math.round(1e6*this.tile2lat(u+1,i))/1e6;h.push(new L.LatLngBounds(new L.LatLng(_,p),new L.LatLng(m,c)))}return h},addBBox:function(t,e,o,n){L.polygon([[n,t],[e,t],[e,o],[n,o]]).addTo(this._map)},onMoveEnd:function(){if(this.options.debug&&console.debug("load Pois"),this._map.getZoom()>=this.options.minzoom)for(var t=this._view2BBoxes(this._map.getBounds()._southWest.lng,this._map.getBounds()._southWest.lat,this._map.getBounds()._northEast.lng,this._map.getBounds()._northEast.lat),e=0,o=t.length,n=!0,i=0;i<t.length;i++){var s=t[i],a=s._southWest.lng,r=s._northEast.lat;if(a in this._requested&&r in this._requested[a]&&1==this._requested[a][r])o--;else{a in this._requested||(this._requested[a]={}),this._requested[a][r]=!0;var l=this.options.query.replace(/(BBOX)/g,s.toOverpassBBoxString()),h=this.options.endpoint+"interpreter?data=[out:json];"+l;n&&(this.options.beforeRequest.call(this),n=!1);var d=this,u=new XMLHttpRequest;u.open("GET",h,!0),u.onload=function(){if(this.status>=200&&this.status<400){var t={instance:d};d.options.callback.call(t,JSON.parse(this.response)),d.options.debug&&console.debug("queryCount: "+o+" - finishedCount: "+e),++e==o&&d.options.afterRequest.call(d)}},u.send()}}},onAdd:function(t){this._map=t,t.zoomIndicator?(this._zoomControl=t.zoomIndicator,this._zoomControl._addLayer(this)):(this._zoomControl=new L.Control.MinZoomIndicator(this.options.minZoomIndicatorOptions),t.addControl(this._zoomControl),this._zoomControl._addLayer(this)),this.onMoveEnd(),-1!=this.options.query.indexOf("(BBOX)")&&t.on("moveend",this.onMoveEnd,this),this.options.debug&&console.debug("add layer")},onRemove:function(t){this.options.debug&&console.debug("remove layer"),L.LayerGroup.prototype.onRemove.call(this,t),this._ids={},this._requested={},this._zoomControl._removeLayer(this),t.off({moveend:this.onMoveEnd},this),this._map=null},getData:function(){return this.options.debug&&console.debug(this._data),this._data}}); |