more geocoder refactoring

This commit is contained in:
DennisSchiefer 2012-03-18 22:17:59 +01:00
parent 08ce748a37
commit eed22b343a

View File

@ -26,15 +26,7 @@ OSRM.CONSTANTS.VIA_LABEL = "via";
OSRM.C.DO_FALLBACK_TO_LAT_LNG = true; OSRM.C.DO_FALLBACK_TO_LAT_LNG = true;
// update geo coordinates in input boxes //[normal geocoding]
function updateLocation(marker_id) {
if (marker_id == OSRM.C.SOURCE_LABEL && OSRM.G.markers.hasSource()) {
document.getElementById("input-source-name").value = OSRM.G.markers.route[0].getPosition().lat.toFixed(6) + ", " + OSRM.G.markers.route[0].getPosition().lng.toFixed(6);
} else if (marker_id == OSRM.C.TARGET_LABEL && OSRM.G.markers.hasTarget()) {
document.getElementById("input-target-name").value = OSRM.G.markers.route[OSRM.G.markers.route.length-1].getPosition().lat.toFixed(6) + ", " + OSRM.G.markers.route[OSRM.G.markers.route.length-1].getPosition().lng.toFixed(6);
}
}
// process input request and call geocoder if needed // process input request and call geocoder if needed
function callGeocoder(marker_id, query) { function callGeocoder(marker_id, query) {
@ -50,13 +42,8 @@ function callGeocoder(marker_id, query) {
} }
//build request for geocoder //build request for geocoder
if (marker_id == OSRM.C.SOURCE_LABEL) {
var call = OSRM.DEFAULTS.HOST_GEOCODER_URL + "?format=json" + OSRM.DEFAULTS.GEOCODER_BOUNDS + "&q=" + query; var call = OSRM.DEFAULTS.HOST_GEOCODER_URL + "?format=json" + OSRM.DEFAULTS.GEOCODER_BOUNDS + "&q=" + query;
OSRM.JSONP.call( call, showGeocoderResults_Source, showGeocoderResults_Timeout, OSRM.DEFAULTS.JSONP_TIMEOUT, "geocoder_source" ); OSRM.JSONP.call( call, showGeocoderResults, showGeocoderResults_Timeout, OSRM.DEFAULTS.JSONP_TIMEOUT, "geocoder_"+marker_id, marker_id );
} else if (marker_id == OSRM.C.TARGET_LABEL) {
var call = OSRM.DEFAULTS.HOST_GEOCODER_URL + "?format=json" + OSRM.DEFAULTS.GEOCODER_BOUNDS + "&q=" + query;
OSRM.JSONP.call( call, showGeocoderResults_Target, showGeocoderResults_Timeout, OSRM.DEFAULTS.JSONP_TIMEOUT, "geocoder_target" );
}
} }
@ -75,11 +62,9 @@ function onclickGeocoderResult(marker_id, lat, lon) {
getRoute(OSRM.C.FULL_DESCRIPTION); getRoute(OSRM.C.FULL_DESCRIPTION);
} }
// process JSONP response of geocoder
// (with wrapper functions for source/target jsonp) // process geocoder response
function showGeocoderResults_Source(response) { showGeocoderResults(OSRM.C.SOURCE_LABEL, response); } function showGeocoderResults(response, marker_id) {
function showGeocoderResults_Target(response) { showGeocoderResults(OSRM.C.TARGET_LABEL, response); }
function showGeocoderResults(marker_id, response) {
if(!response){ if(!response){
showGeocoderResults_Empty(marker_id); showGeocoderResults_Empty(marker_id);
return; return;
@ -90,6 +75,7 @@ function showGeocoderResults(marker_id, response) {
return; return;
} }
// show possible results for input
var html = ""; var html = "";
html += '<table class="results-table">'; html += '<table class="results-table">';
for(var i=0; i < response.length; i++){ for(var i=0; i < response.length; i++){
@ -130,7 +116,17 @@ function showGeocoderResults_Timeout() {
} }
// - [upcoming feature: reverse geocoding (untested) ] - // [reverse geocoding]
//update geo coordinates in input boxes
function updateLocation(marker_id) {
if (marker_id == OSRM.C.SOURCE_LABEL && OSRM.G.markers.hasSource()) {
document.getElementById("input-source-name").value = OSRM.G.markers.route[0].getPosition().lat.toFixed(6) + ", " + OSRM.G.markers.route[0].getPosition().lng.toFixed(6);
} else if (marker_id == OSRM.C.TARGET_LABEL && OSRM.G.markers.hasTarget()) {
document.getElementById("input-target-name").value = OSRM.G.markers.route[OSRM.G.markers.route.length-1].getPosition().lat.toFixed(6) + ", " + OSRM.G.markers.route[OSRM.G.markers.route.length-1].getPosition().lng.toFixed(6);
}
}
// update address in input boxes // update address in input boxes
function updateAddress(marker_id, do_fallback_to_lat_lng) { function updateAddress(marker_id, do_fallback_to_lat_lng) {
@ -151,13 +147,8 @@ function updateAddress(marker_id, do_fallback_to_lat_lng) {
OSRM.JSONP.call( call, showReverseGeocoderResults, showReverseGeocoderResults_Timeout, OSRM.DEFAULTS.JSONP_TIMEOUT, "reverse_geocoder_"+marker_id, {marker_id:marker_id, do_fallback: do_fallback_to_lat_lng} ); OSRM.JSONP.call( call, showReverseGeocoderResults, showReverseGeocoderResults_Timeout, OSRM.DEFAULTS.JSONP_TIMEOUT, "reverse_geocoder_"+marker_id, {marker_id:marker_id, do_fallback: do_fallback_to_lat_lng} );
} }
// processing JSONP response of reverse geocoder
function showReverseGeocoderResults_Timeout(response, parameters) {
if(!parameters.do_fallback)
return;
updateLocation(parameters.marker_id); // processing JSONP response of reverse geocoder
}
function showReverseGeocoderResults(response, parameters) { function showReverseGeocoderResults(response, parameters) {
if(!response) { if(!response) {
showReverseGeocoderResults_Timeout(response, parameters); showReverseGeocoderResults_Timeout(response, parameters);
@ -204,3 +195,9 @@ function showReverseGeocoderResults(response, parameters) {
else if(parameters.marker_id == OSRM.C.TARGET_LABEL && OSRM.G.markers.hasTarget() ) else if(parameters.marker_id == OSRM.C.TARGET_LABEL && OSRM.G.markers.hasTarget() )
document.getElementById("input-target-name").value = address; document.getElementById("input-target-name").value = address;
} }
function showReverseGeocoderResults_Timeout(response, parameters) {
if(!parameters.do_fallback)
return;
updateLocation(parameters.marker_id);
}