Skip to content
Snippets Groups Projects
Commit 783bf451 authored by Nikolaus Krismer's avatar Nikolaus Krismer
Browse files

improved geocode-reverse-lookup in geosearch component

parent 21049fe9
No related branches found
No related tags found
No related merge requests found
......@@ -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>
......
......@@ -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;
......
......@@ -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
);
}
});
/**
* 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
);
}
});
......@@ -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
}));
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment