Files
MYSOPHAL/lib/leaflet/plugins/leaflet-control-osm-geocoder/Control.OSMGeocoder.min.js
2025-08-07 13:15:31 +01:00

1 line
3.5 KiB
JavaScript

if(typeof console=="undefined"){this.console={log:function(e){}}}L.Control.OSMGeocoder=L.Control.extend({options:{collapsed:true,position:"topright",text:"Locate",placeholder:"",bounds:null,email:null,callback:function(e){if(e.length==0){console.log("ERROR: didn't find a result");return}var t=e[0].boundingbox,o=new L.LatLng(t[0],t[2]),n=new L.LatLng(t[1],t[3]),i=new L.LatLngBounds([o,n]);this._map.fitBounds(i)}},_callbackId:0,initialize:function(e){L.Util.setOptions(this,e)},onAdd:function(e){this._map=e;var t="leaflet-control-geocoder",o=this._container=L.DomUtil.create("div",t);L.DomEvent.disableClickPropagation(o);var n=this._form=L.DomUtil.create("form",t+"-form");var i=this._input=document.createElement("input");i.type="text";i.placeholder=this.options.placeholder||"";var s=document.createElement("input");s.type="submit";s.value=this.options.text;n.appendChild(i);n.appendChild(s);L.DomEvent.addListener(n,"submit",this._geocode,this);if(this.options.collapsed){L.DomEvent.addListener(o,"mouseover",this._expand,this);L.DomEvent.addListener(o,"mouseout",this._collapse,this);var a=this._layersLink=L.DomUtil.create("a",t+"-toggle",o);a.href="#";a.title="Nominatim Geocoder";L.DomEvent.addListener(a,L.Browser.touch?"click":"focus",this._expand,this);this._map.on("movestart",this._collapse,this)}else{this._expand()}o.appendChild(n);return o},_createSearchResult:function(e,t){var o=.005;var n=[];n[0]={};n[0]["boundingbox"]=[parseFloat(e)-o,parseFloat(e)+o,parseFloat(t)-o,parseFloat(t)+o];n[0]["class"]="boundary";n[0]["display_name"]="Position: "+e+" "+t;n[0]["lat"]=e;n[0]["lon"]=t;return n},_isLatLon:function(e){var t=/(-?\d+\.\d+)\s(-?\d+\.\d+)/;var o=t.exec(e);if(o!=undefined)return o;t=/lat\D*(-?\d+\.\d+)\D*lon\D*(-?\d+\.\d+)/;o=t.exec(e);if(o!=undefined)return o;else return null},_isLatLon_decMin:function(e){console.log("is LatLon?: "+e);re=/([ns])\s*(\d+)\D*(\d+\.\d+).?\s*([ew])\s*(\d+)\D*(\d+\.\d+)/i;m=re.exec(e.toLowerCase());if(m!=undefined)return m;else return null},_geocode:function(e){L.DomEvent.preventDefault(e);var t=this._input.value;if(this._isLatLon(t)!=null){var o=this._isLatLon(t);console.log("LatLon: "+o[1]+" "+o[2]);this.options.callback.call(this,this._createSearchResult(o[1],o[2]));return}else if(this._isLatLon_decMin(t)!=null){var o=this._isLatLon_decMin(t);var n=new Array;n["n"]=1;n["s"]=-1;n["e"]=1;n["w"]=-1;this.options.callback.call(this,this._createSearchResult(n[o[1]]*(Number(o[2])+o[3]/60),n[o[4]]*(Number(o[5])+o[6]/60)));return}console.log(this._callbackId);window["_l_osmgeocoder_"+this._callbackId]=L.Util.bind(this.options.callback,this);var i={q:this._input.value,json_callback:"_l_osmgeocoder_"+this._callbackId++,format:"json"};if(this.options.bounds&&this.options.bounds!=null){if(this.options.bounds instanceof L.LatLngBounds){i.viewbox=this.options.bounds.toBBoxString();i.bounded=1}else{console.log("bounds must be of type L.LatLngBounds");return}}if(this.options.email&&this.options.email!=null){if(typeof this.options.email=="string"){i.email=this.options.email}else{console.log("email must be a string")}}var s=location.protocol;if(s=="file:")s="https:";var a=s+"//nominatim.openstreetmap.org/search"+L.Util.getParamString(i),l=document.createElement("script");l.type="text/javascript";l.src=a;l.id=this._callbackId;document.getElementsByTagName("head")[0].appendChild(l)},_expand:function(){L.DomUtil.addClass(this._container,"leaflet-control-geocoder-expanded")},_collapse:function(){this._container.className=this._container.className.replace(" leaflet-control-geocoder-expanded","")}});