From 783bf451cbff53830c28b6ea15e2fe7403b6f2dc Mon Sep 17 00:00:00 2001
From: Nikolaus Krismer <niko@krismer.de>
Date: Wed, 25 Dec 2013 23:42:56 +0100
Subject: [PATCH] improved geocode-reverse-lookup in geosearch component

---
 src/main/webapp/index_new.html                |  1 -
 src/main/webapp/js_new/geosearch.js           | 33 ++++++++-----
 .../webapp/js_new/geosearch_provider_osm.js   | 47 +++++++++++++++++--
 .../js_new/geosearch_reverseProvider_osm.js   | 37 ---------------
 src/main/webapp/js_new/map.js                 |  1 -
 5 files changed, 66 insertions(+), 53 deletions(-)
 delete mode 100644 src/main/webapp/js_new/geosearch_reverseProvider_osm.js

diff --git a/src/main/webapp/index_new.html b/src/main/webapp/index_new.html
index 2bb3375e..d21942ff 100644
--- a/src/main/webapp/index_new.html
+++ b/src/main/webapp/index_new.html
@@ -18,7 +18,6 @@
 	<script type="text/javascript" src="js_new/console.js"></script>
 	<script type="text/javascript" src="js_new/geosearch.js"></script>
 	<script type="text/javascript" src="js_new/geosearch_provider_osm.js"></script>
-	<script type="text/javascript" src="js_new/geosearch_reverseProvider_osm.js"></script>
 	<script type="text/javascript" src="js_new/controls.js"></script>
 	<script type="text/javascript" src="js_new/bing.js"></script>
 	<script type="text/javascript" src="js_new/google.js"></script>
diff --git a/src/main/webapp/js_new/geosearch.js b/src/main/webapp/js_new/geosearch.js
index 819eded2..caed0885 100644
--- a/src/main/webapp/js_new/geosearch.js
+++ b/src/main/webapp/js_new/geosearch.js
@@ -7,14 +7,16 @@ L.GeoSearch = {};
 L.GeoSearch.Provider = {};
 L.GeoSearch.ReverseProvider = {};
 
-L.GeoSearch.ReverseResult = function (address, label) {
+L.GeoSearch.ReverseResult = function (lon, lat, address, label) {
+	this.lon = lon;
+    this.lat = lat;
     this.address = address;
     this.Label = label;
 };
 
-L.GeoSearch.Result = function (x, y, label) {
-    this.X = x;
-    this.Y = y;
+L.GeoSearch.Result = function (lon, lat, label) {
+	this.lon = lon;
+    this.lat = lat;
     this.Label = label;
 };
 
@@ -22,7 +24,6 @@ L.Control.GeoSearch = L.Control.extend({
     options: {
         position: 'topleft',
         provider: null,
-        reverseProvider: null,
         showMarker: true
     },
 
@@ -76,7 +77,7 @@ L.Control.GeoSearch = L.Control.extend({
           .addListener(this._searchbtn, 'click', this._onSearchClick, this)
           .addListener(this._searchbox, 'keypress', this._onKeyUp, this);
 
-        if (this._config.reverseProvider) {
+        if (this._config.provider.options.reverseable) {
         	L.DomEvent.addListener(this._map, 'click', this._onMapClick, this);
         }
 
@@ -190,19 +191,29 @@ L.Control.GeoSearch = L.Control.extend({
         }
 
     	var address = result.address;
-        this._searchbox.value = address.road + ', ' + address.postcode + ', ' + address.country;
+    	var location = {lat: result.lat, lon: result.lon};
+    	var addressStr = '';
+    	if (address.road) addressStr += address.road + ', ';
+    	if (address.postcode) addressStr += address.postcode + ', ';
+    	if (address.country) addressStr += address.country;
+    	if (/, $/.test(addressStr)) addressStr = addressStr.substring(0, addressStr.length - 2);
+
+        this._searchbox.value = addressStr;
+
+        this._map.fireEvent('geosearch_clicklocation', {Location: location});
+        this._showLocation(location);
     },
 
     _showLocation: function (location) {
         if (this.options.showMarker == true) {
             if (typeof this._positionMarker === 'undefined') {
-                this._positionMarker = L.marker([location.Y, location.X]).addTo(this._map);
+                this._positionMarker = L.marker([location.lat, location.lon]).addTo(this._map);
             } else {
-                this._positionMarker.setLatLng([location.Y, location.X]);
+                this._positionMarker.setLatLng([location.lat, location.lon]);
             }
         }
 
-        this._map.setView([location.Y, location.X], this._config.zoomLevel, false);
+        this._map.setView([location.lat, location.lon], this._config.zoomLevel, false);
         this._map.fireEvent('geosearch_showlocation', {Location: location});
     },
 
@@ -231,7 +242,7 @@ L.Control.GeoSearch = L.Control.extend({
 
 	_onMapClick: function (e) {
 		var location = e.latlng,
-			provider = this._config.reverseProvider;
+			provider = this._config.provider;
 
 		if (!location || !provider) {
 			return;
diff --git a/src/main/webapp/js_new/geosearch_provider_osm.js b/src/main/webapp/js_new/geosearch_provider_osm.js
index ed4effb3..9fd8ea38 100644
--- a/src/main/webapp/js_new/geosearch_provider_osm.js
+++ b/src/main/webapp/js_new/geosearch_provider_osm.js
@@ -6,14 +6,43 @@
 
 L.GeoSearch.Provider.OpenStreetMap = L.Class.extend({
 	options : {
-
+		reverseable: true
 	},
 
 	initialize : function(options) {
 		options = L.Util.setOptions(this, options);
 	},
 
-	GetServiceUrl : function(qry) {
+	GetServiceUrl : function(qry, lon) {
+		if (!lon) {
+			// only one parameter given (query).. perform a forward lookup
+			return this._getForwardServiceUrl(qry);
+		}
+
+		// second parameter set... perform a reverse lookup
+		return this._getReverseServiceUrl(qry, lon);
+	},
+
+	ParseJSON : function(data) {
+		if (data instanceof Array) {
+			return this._parseForwardJSON(data);
+		}
+
+		return this._parseReverseJSON(data);
+	},
+
+	_getReverseServiceUrl : function(lat, lon) {
+		var parameters = L.Util.extend({
+			lat: lat,
+			lon: lon,
+			format : 'json'
+		}, this.options);
+
+		return 'http://nominatim.openstreetmap.org/reverse'
+				+ L.Util.getParamString(parameters);
+	},
+
+	_getForwardServiceUrl: function(qry) {
 		var parameters = L.Util.extend({
 			q : qry,
 			format : 'json'
@@ -23,7 +52,7 @@ L.GeoSearch.Provider.OpenStreetMap = L.Class.extend({
 				+ L.Util.getParamString(parameters);
 	},
 
-	ParseJSON : function(data) {
+	_parseForwardJSON : function(data) {
 		if (data.length == 0)
 			return [];
 
@@ -36,5 +65,17 @@ L.GeoSearch.Provider.OpenStreetMap = L.Class.extend({
 			));
 
 		return results;
+	},
+
+	_parseReverseJSON : function(data) {
+		if (data.length == 0)
+			return {};
+
+		return new L.GeoSearch.ReverseResult(
+			data.lon,
+			data.lat,
+			data.address,
+			data.display_name
+		);
 	}
 });
diff --git a/src/main/webapp/js_new/geosearch_reverseProvider_osm.js b/src/main/webapp/js_new/geosearch_reverseProvider_osm.js
deleted file mode 100644
index 5d3d2f8a..00000000
--- a/src/main/webapp/js_new/geosearch_reverseProvider_osm.js
+++ /dev/null
@@ -1,37 +0,0 @@
-/**
- * L.Control.GeoSearch - search for an address and zoom to it's location
- * L.GeoSearch.Provider.OpenStreetMap uses openstreetmap geocoding service
- * https://github.com/smeijer/leaflet.control.geosearch
- */
-
-L.GeoSearch.ReverseProvider.OpenStreetMap = L.Class.extend({
-	options : {
-
-	},
-
-	initialize : function(options) {
-		options = L.Util.setOptions(this, options);
-	},
-
-	GetServiceUrl : function(lat, lon) {
-		var parameters = L.Util.extend({
-			lat: lat,
-			lon: lon,
-			format : 'json'
-		}, this.options);
-
-		return 'http://nominatim.openstreetmap.org/reverse'
-				+ L.Util.getParamString(parameters);
-	},
-
-	ParseJSON : function(data) {
-		if (data.length == 0)
-			return [];
-
-		return new L.GeoSearch.ReverseResult(
-			data.address,
-			data.display_name
-		);
-	}
-
-});
diff --git a/src/main/webapp/js_new/map.js b/src/main/webapp/js_new/map.js
index dba23981..b26d04e6 100644
--- a/src/main/webapp/js_new/map.js
+++ b/src/main/webapp/js_new/map.js
@@ -50,7 +50,6 @@ function drawMap(mapOptions) {
 	map.addControl(new L.Control.GeoSearch({
 		position: 'topleft',
 		provider: new L.GeoSearch.Provider.OpenStreetMap(),
-		reverseProvider: new L.GeoSearch.ReverseProvider.OpenStreetMap(),
 		showMarker: true
 	}));
 }
-- 
GitLab