From 447ac954e75bc64532f036ff7b6e645f813e4797 Mon Sep 17 00:00:00 2001 From: Francis Chabouis Date: Wed, 11 Oct 2017 14:36:46 +0200 Subject: [PATCH] initial commit --- demo/index.html | 41 ++++++++ images/geocoder.png | Bin 0 -> 490 bytes src/leaflet.BAN.geocoder.css | 66 ++++++++++++ src/leaflet.BAN.geocoder.js | 196 +++++++++++++++++++++++++++++++++++ 4 files changed, 303 insertions(+) create mode 100644 demo/index.html create mode 100644 images/geocoder.png create mode 100644 src/leaflet.BAN.geocoder.css create mode 100644 src/leaflet.BAN.geocoder.js diff --git a/demo/index.html b/demo/index.html new file mode 100644 index 0000000..459542e --- /dev/null +++ b/demo/index.html @@ -0,0 +1,41 @@ + + + + + + + Leaflet.BAN.geocoder demo + + + + + + + + + +
+ + + + diff --git a/images/geocoder.png b/images/geocoder.png new file mode 100644 index 0000000000000000000000000000000000000000..d82a0170e62ccacac814cef91bbefdacc62071ca GIT binary patch literal 490 zcmeAS@N?(olHy`uVBq!ia0vp^QXtI13?%1G+4BcToe%H{aRt&UDk^GVpsK10WCKOC zw6q|C#>U1FL7+U40VFjvG$4xA)zyIvpkfFYC@U!`2~h(S0W#ohU0q#>2#^Hol97>t zYf(~Cg0LY9fR+MvDJv`M>FL4MfK5_`SOjDP9RLwQQvehMifrinc@wDAr6kBNn8Eei zhkJ(1Y;ONpSNo=%-YWU=81EIv1wTG<9iA^@+w^CPVAhJsp`1Mhkz$XY)V_{Nx8znl znebo^)4DUpKX-f$`*o|*yFi5ZJJ7r~PZ!4!3CXhuU74B<7+4OtGMAPE!Oj2sSAOw2 zG*`04Z*AVqfRkafpRD1JR5=`$ZdZDI!jYQ{28QCyKU6O>%@Z~Kp<49g@{X4HEjQYp zA2$7y^oHwz(LFO2R=K|C()%Yobyti3-u|!U)Uy`uUfnC}tjd|Ps*5@pY 0 ? currentSelection.previousElementSibling : currentSelection.nextElementSibling; + } + if (el) { + L.DomUtil.addClass(el, 'leaflet-control-geocoder-ban-selected'); + } + }, + _keyup: function(e) { + switch (e.keyCode) { + case 27: + // escape + this._collapse(); + break; + case 38: + // down + this._moveSelection(1); + L.DomEvent.preventDefault(e); + break; + case 40: + // up + this._moveSelection(-1); + L.DomEvent.preventDefault(e); + break; + case 13: + // enter + var s = document.getElementsByClassName('leaflet-control-geocoder-ban-selected') + if (s.length) { + this._geocodeResult(s[0]._geocodedFeatures); + } + break; + default: + if (this._input.value) { + var params = {q: this._input.value, limit: this.options.resultsNumber}; + var t = this; + if (this._setTimeout) { + clearTimeout(this._setTimeout); + } + //avoid responses collision if typing quickly + this._setTimeout = setTimeout(function () { + getJSON(t.options.serviceUrl, params, t._displayResults(t)); + }, this.options.minIntervalBetweenRequests); + } else { + this._clearResults(); + } + } + }, + _clearResults () { + while (this._alts.firstChild) { + this._alts.removeChild(this._alts.firstChild); + } + }, + _displayResults: function (t) { + t._clearResults(); + return function (res) { + var features = res.features; + L.DomUtil.removeClass(t._alts, 'leaflet-control-geocoder-ban-alternatives-minimized'); + for (var i = 0; i < Math.min(features.length, t.options.resultsNumber); i++) { + t._alts.appendChild(t._createAlt(features[i], i)); + } + } + }, + _createAlt: function (feature, index) { + var li = L.DomUtil.create('li', ''), + a = L.DomUtil.create('a', '', li); + li.setAttribute('data-result-index', index); + a.innerHTML = feature.properties.label; + li._geocodedFeatures = feature + clickHandler = function (e) { + this._collapse(); + this._geocodeResult(feature); + } + mouseOverHandler = function (e) { + var s = document.getElementsByClassName('leaflet-control-geocoder-ban-selected'); + if (s.length) { + L.DomUtil.removeClass(s[0], 'leaflet-control-geocoder-ban-selected'); + } + L.DomUtil.addClass(li, 'leaflet-control-geocoder-ban-selected'); + } + mouseOutHandler = function (e) { + L.DomUtil.removeClass(li, 'leaflet-control-geocoder-ban-selected'); + } + L.DomEvent.on(li, 'click', clickHandler, this); + L.DomEvent.on(li, 'mouseover', mouseOverHandler, this); + L.DomEvent.on(li, 'mouseout', mouseOutHandler, this); + return li; + }, + _geocodeResult: function (feature) { + this._collapse(); + this.fire('markgeocode', {geocode: feature}); + }, + markGeocode: function (result) { + var f = result.geocode; + var latlng = [f.geometry.coordinates[1], f.geometry.coordinates[0]] + this._map.setView(latlng, 14) + this._geocodeMarker = new L.Marker(latlng) + .bindPopup(f.properties.label) + .addTo(this._map) + .openPopup(); + } +}); + +L.geocoderBAN = function (options) { + return new L.GeocoderBAN(options); +}; + +function getJSON (url, params, callback) { + var xmlHttp = new XMLHttpRequest(); + xmlHttp.onreadystatechange = function () { + if (xmlHttp.readyState !== 4){ + return; + } + if (xmlHttp.status !== 200 && xmlHttp.status !== 304){ + callback(''); + return; + } + callback(JSON.parse(xmlHttp.response)); + }; + xmlHttp.open('GET', url + L.Util.getParamString(params), true); + xmlHttp.setRequestHeader('Accept', 'application/json'); + xmlHttp.send(null); +}