1if (typeof console == "undefined") { 2 this.console = { log: function (msg) { /* do nothing since it would otherwise break IE */} }; 3} 4 5 6L.Control.OSMGeocoder = L.Control.extend({ 7 options: { 8 collapsed: true, 9 position: 'topright', 10 text: 'Locate', 11 bounds: null, // L.LatLngBounds 12 email: null, // String 13 accept_language : 'fr', 14 callback: function (results) { 15 if (results.length == 0) { 16 console.log("ERROR: didn't find a result"); 17 return; 18 } 19 var bbox = results[0].boundingbox, 20 first = new L.LatLng(bbox[0], bbox[2]), 21 second = new L.LatLng(bbox[1], bbox[3]), 22 bounds = new L.LatLngBounds([first, second]); 23 this._map.fitBounds(bounds); 24 } 25 }, 26 27 _callbackId: 0, 28 29 initialize: function (options) { 30 L.Util.setOptions(this, options); 31 }, 32 33 onAdd: function (map) { 34 this._map = map; 35 36 var className = 'leaflet-control-geocoder', 37 container = this._container = L.DomUtil.create('div', className); 38 39 L.DomEvent.disableClickPropagation(container); 40 41 var form = this._form = L.DomUtil.create('form', className + '-form'); 42 43 var input = this._input = document.createElement('input'); 44 input.type = "text"; 45 46 var submit = document.createElement('button'); 47 submit.type = "submit"; 48 submit.value = this.options.text; 49 submit.innerHTML = '<span class="glyphicon glyphicon-search" ></span>'; 50 51 form.appendChild(input); 52 form.appendChild(submit); 53 54 L.DomEvent.addListener(form, 'submit', this._geocode, this); 55 56 if (this.options.collapsed) { 57 L.DomEvent.addListener(container, 'mouseover', this._expand, this); 58 L.DomEvent.addListener(container, 'mouseout', this._collapse, this); 59 60 var link = this._layersLink = L.DomUtil.create('a', className + '-toggle', container); 61 link.href = '#'; 62 link.title = 'Nominatim Geocoder'; 63 64 L.DomEvent.addListener(link, L.Browser.touch ? 'click' : 'focus', this._expand, this); 65 66 this._map.on('movestart', this._collapse, this); 67 } else { 68 this._expand(); 69 } 70 71 container.appendChild(form); 72 73 return container; 74 }, 75 76 /* helper functions for cordinate extraction */ 77 _createSearchResult : function(lat, lon) { 78 //creates an position description similar to the result of a Nominatim search 79 var diff = 0.005; 80 var result = []; 81 result[0] = {}; 82 result[0]["boundingbox"] = [parseFloat(lat)-diff,parseFloat(lat)+diff,parseFloat(lon)-diff,parseFloat(lon)+diff]; 83 result[0]["class"]="boundary"; 84 result[0]["display_name"]="Position: "+lat+" "+lon; 85 result[0]["lat"] = lat; 86 result[0]["lon"] = lon; 87 return result; 88 }, 89 _isLatLon : function (q) { 90 //"lon lat" => xx.xxx x.xxxxx 91 var re = /(-?\d+\.\d+)\s(-?\d+\.\d+)/; 92 var m = re.exec(q); 93 if (m != undefined) return m; 94 95 //lat...xx.xxx...lon...x.xxxxx 96 re = /lat\D*(-?\d+\.\d+)\D*lon\D*(-?\d+\.\d+)/; 97 m = re.exec(q); 98 //showRegExpResult(m); 99 if (m != undefined) return m; 100 else return null; 101 }, 102 _isLatLon_decMin : function (q) { 103 //console.log("is LatLon?: "+q); 104 //N 53° 13.785' E 010° 23.887' 105 //re = /[NS]\s*(\d+)\D*(\d+\.\d+).?\s*[EW]\s*(\d+)\D*(\d+\.\d+)\D*/; 106 re = /([ns])\s*(\d+)\D*(\d+\.\d+).?\s*([ew])\s*(\d+)\D*(\d+\.\d+)/i; 107 m = re.exec(q.toLowerCase()); 108 //showRegExpResult(m); 109 if ((m != undefined)) return m; 110 else return null; 111 // +- dec min +- dec min 112 }, 113 114 _geocode : function (event) { 115 L.DomEvent.preventDefault(event); 116 var q = this._input.value; 117 //console.log(q); 118 //try to find corrdinates 119 if (this._isLatLon(q) != null) 120 { 121 var m = this._isLatLon(q); 122 console.log("LatLon: "+m[1]+" "+m[2]); 123 //m = {lon, lat} 124 this.options.callback.call(this, this._createSearchResult(m[1],m[2])); 125 return; 126 } 127 else if (this._isLatLon_decMin(q) != null) 128 { 129 var m = this._isLatLon_decMin(q); 130 //m: [ns, lat dec, lat min, ew, lon dec, lon min] 131 var temp = new Array(); 132 temp['n'] = 1; 133 temp['s'] = -1; 134 temp['e'] = 1; 135 temp['w'] = -1; 136 this.options.callback.call(this,this._createSearchResult( 137 temp[m[1]]*(Number(m[2]) + m[3]/60), 138 temp[m[4]]*(Number(m[5]) + m[6]/60) 139 )); 140 return; 141 } 142 143 //and now Nominatim 144 //http://wiki.openstreetmap.org/wiki/Nominatim 145 //console.log(this._callbackId); 146 window[("_l_osmgeocoder_"+this._callbackId)] = L.Util.bind(this.options.callback, this); 147 148 149 /* Set up params to send to Nominatim */ 150 var params = { 151 // Defaults 152 q: this._input.value, 153 json_callback : ("_l_osmgeocoder_"+this._callbackId++), 154 format: 'json', 155 addressdetails : 1, 156 limit : 1, 157 'accept-language' : this.options.accept_language 158 }; 159 160 if (this.options.bounds && this.options.bounds != null) { 161 if( this.options.bounds instanceof L.LatLngBounds ) { 162 params.viewboxlbrt = this.options.bounds.toBBoxString(); 163 params.bounded = 1; 164 } 165 else { 166 console.log('bounds must be of type L.LatLngBounds'); 167 return; 168 } 169 } 170 171 if (this.options.email && this.options.email != null) { 172 if (typeof this.options.email == 'string') { 173 params.email = this.options.email; 174 } 175 else{ 176 console.log('email must be a string'); 177 } 178 } 179 180 var url = " http://nominatim.openstreetmap.org/search" + L.Util.getParamString(params), 181 script = document.createElement("script"); 182 //console.log(url); 183 184 185 186 script.type = "text/javascript"; 187 script.src = url; 188 script.id = this._callbackId; 189 document.getElementsByTagName("head")[0].appendChild(script); 190 }, 191 192 _expand: function () { 193 L.DomUtil.addClass(this._container, 'leaflet-control-geocoder-expanded'); 194 }, 195 196 _collapse: function () { 197 this._container.className = this._container.className.replace(' leaflet-control-geocoder-expanded', ''); 198 } 199});
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.