From 805402b23044d0e98c48b68c196d8431f79e48f0 Mon Sep 17 00:00:00 2001 From: DennisSchiefer Date: Tue, 13 Mar 2012 17:32:18 +0100 Subject: [PATCH] starting to reverse geocoding --- WebContent/OSRM.JSONP.js | 4 +-- WebContent/geocoder.js | 61 ++++++++++++++++++++++++++++++++++++++++ WebContent/main.html | 2 +- WebContent/routing.js | 9 ++++++ 4 files changed, 73 insertions(+), 3 deletions(-) diff --git a/WebContent/OSRM.JSONP.js b/WebContent/OSRM.JSONP.js index 363873f3e..38af68549 100644 --- a/WebContent/OSRM.JSONP.js +++ b/WebContent/OSRM.JSONP.js @@ -9,8 +9,8 @@ OSRM.JSONP = { TIMEOUT: OSRM.DEFAULTS.JSONP_TIMEOUT, - late: function() { console.log("reply too late");}, - empty: function() { console.log("empty callback");}, + late: function() { },//console.log("reply too late");}, + empty: function() { },//console.log("empty callback");}, call: function(source, callback_function, timeout_function, timeout, id) { // only one active JSONP call per id diff --git a/WebContent/geocoder.js b/WebContent/geocoder.js index 005726eae..9ad9e4c83 100644 --- a/WebContent/geocoder.js +++ b/WebContent/geocoder.js @@ -1,9 +1,70 @@ // some constants OSRM.GEOCODE_POST = 'http://nominatim.openstreetmap.org/search?format=json'; +OSRM.REVERSE_GEOCODE_POST = 'http://nominatim.openstreetmap.org/reverse?format=json'; OSRM.SOURCE_MARKER_LABEL = "source"; OSRM.TARGET_MARKER_LABEL = "target"; +// update locations in input boxes +function updateLocations() { + if( my_markers.route[0] && my_markers.route[0].label == OSRM.SOURCE_MARKER_LABEL) { + document.getElementById("input-source-name").value = my_markers.route[0].getPosition().lat.toFixed(6) + ", " + my_markers.route[0].getPosition().lng.toFixed(6); + callReverseGeocoder("source", my_markers.route[0].getPosition().lat, my_markers.route[0].getPosition().lng); + OSRM.debug.log("[call1] reverse geocoder"); + } + +// if( my_markers.route[my_markers.route.length-1] && my_markers.route[ my_markers.route.length-1 ].label == OSRM.TARGET_MARKER_LABEL) { +// document.getElementById("input-target-name").value = my_markers.route[my_markers.route.length-1].getPosition().lat.toFixed(6) + ", " + my_markers.route[my_markers.route.length-1].getPosition().lng.toFixed(6); +// callReverseGeocoder("target", my_markers.route[my_markers.route.length-1].getPosition().lat, my_markers.route[my_markers.route.length-1].getPosition().lng); +// } +} + + +function timeout_ReverseGeocoder() { + OSRM.debug.log("[timeout] reverse geocoder"); +} + +//prepare request and call reverse geocoder +function callReverseGeocoder(marker_id, lat, lon) { + //build request + if (marker_id == OSRM.SOURCE_MARKER_LABEL) { + var src= OSRM.REVERSE_GEOCODE_POST + "&lat=" + lat + "&lon=" + lon; + OSRM.JSONP.call( src, showReverseGeocoderResults_Source, timeout_ReverseGeocoder, OSRM.JSONP.TIMEOUT, "reverse_geocoder_source" ); + OSRM.debug.log("[call2] reverse geocoder"); + } else if (marker_id == OSRM.TARGET_MARKER_LABEL) { + var src = OSRM.REVERSE_GEOCODE_POST + "&lat=" + lat + "&lon=" + lon; + OSRM.JSONP.call( src, showReverseGeocoderResults_Target, timeout_ReverseGeocoder, OSRM.JSONP.TIMEOUT, "reverse_geocoder_target" ); + } +} +//processing JSONP response of reverse geocoder +//(with wrapper functions for source/target jsonp) +function showReverseGeocoderResults_Source(response) { showReverseGeocoderResults(OSRM.SOURCE_MARKER_LABEL, response); } +function showReverseGeocoderResults_Target(response) { showReverseGeocoderResults(OSRM.TARGET_MARKER_LABEL, response); } +function showReverseGeocoderResults(marker_id, response) { + OSRM.debug.log("[inner] reverse geocoder"); + if(response){ + if(response.address == undefined) + return; + + var address = ""; + if( response.address.road) + address += response.address.road; + if( response.address.city) { + if( response.address.road) + address += ", "; + address += response.address.city; + } + if( address == "" ) + return; + + if(marker_id == OSRM.SOURCE_MARKER_LABEL) + document.getElementById("input-source-name").value = address; + else if(marker_id == OSRM.TARGET_MARKER_LABEL) + document.getElementById("input-target-name").value = address; + } +} + + // prepare request and call geocoder function callGeocoder(marker_id, query) { //build request diff --git a/WebContent/main.html b/WebContent/main.html index 60b9cfd5c..bfa031774 100644 --- a/WebContent/main.html +++ b/WebContent/main.html @@ -23,7 +23,7 @@ - + diff --git a/WebContent/routing.js b/WebContent/routing.js index 4d4d86ea3..509f13feb 100644 --- a/WebContent/routing.js +++ b/WebContent/routing.js @@ -85,6 +85,7 @@ function showRoute(response) { snapRoute(); } updateHints(response); + updateLocations(); } function showNoRouteGeometry() { @@ -346,6 +347,12 @@ function positionsToInput() { } function reverseRouting() { + // invert input boxes + var tmp = document.getElementById("input-source-name").value; + document.getElementById("input-source-name").value = document.getElementById("input-target-name").value; + document.getElementById("input-target-name").value = tmp; + + // invert route my_markers.route.reverse(); if(my_markers.route.length == 1) { if(my_markers.route[0].label == OSRM.TARGET_MARKER_LABEL) { @@ -362,6 +369,8 @@ function reverseRouting() { my_markers.route[my_markers.route.length-1].label = OSRM.TARGET_MARKER_LABEL; my_markers.route[my_markers.route.length-1].marker.setIcon( new L.Icon('images/marker-target.png') ); } + + // recompute route getRoute(OSRM.FULL_DESCRIPTION); my_markers.highlight.hide(); }