refactored geocoder code (still need to check dirty flags)

This commit is contained in:
DennisSchiefer 2012-03-18 21:44:14 +01:00
parent 97b9c65c97
commit 69790eb8c7
4 changed files with 85 additions and 68 deletions

View File

@ -23,6 +23,7 @@ or see http://www.gnu.org/licenses/agpl.txt.
OSRM.CONSTANTS.SOURCE_LABEL = "source";
OSRM.CONSTANTS.TARGET_LABEL = "target";
OSRM.CONSTANTS.VIA_LABEL = "via";
OSRM.C.DO_FALLBACK_TO_LAT_LNG = true;
// update geo coordinates in input boxes
@ -37,21 +38,18 @@ function updateLocation(marker_id) {
// process input request and call geocoder if needed
function callGeocoder(marker_id, query) {
if (marker_id == OSRM.C.SOURCE_LABEL && OSRM.G.markers.hasSource() && OSRM.G.markers.route[0].dirty_move == false && OSRM.G.markers.route[0].dirty_type == false)
return;
if (marker_id == OSRM.C.TARGET_LABEL && OSRM.G.markers.hasTarget() && OSRM.G.markers.route[OSRM.G.markers.route.length-1].dirty_move == false && OSRM.G.markers.route[OSRM.G.markers.route.length-1].dirty_type == false)
return;
if(query=="")
return;
//geo coordinates given -> go directly to drawing results
//geo coordinates given -> directly draw results
if(query.match(/^\s*[-+]?[0-9]*\.?[0-9]+\s*[,;]\s*[-+]?[0-9]*\.?[0-9]+\s*$/)){
var coord = query.split(/[,;]/);
onclickGeocoderResult(marker_id, coord[0], coord[1], true);
onclickGeocoderResult(marker_id, coord[0], coord[1]);
updateAddress( marker_id );
return;
}
//build request
//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;
OSRM.JSONP.call( call, showGeocoderResults_Source, showGeocoderResults_Timeout, OSRM.DEFAULTS.JSONP_TIMEOUT, "geocoder_source" );
@ -63,24 +61,17 @@ function callGeocoder(marker_id, query) {
// helper function for clicks on geocoder search results
function onclickGeocoderResult(marker_id, lat, lon, do_reverse_geocode, do_zoom ) {
function onclickGeocoderResult(marker_id, lat, lon) {
var index;
if( marker_id == OSRM.C.SOURCE_LABEL )
index = OSRM.G.markers.setSource( new L.LatLng(lat, lon) );
else if( marker_id == OSRM.C.TARGET_LABEL )
index = OSRM.G.markers.setTarget( new L.LatLng(lat, lon) );
else
index = -1; // via nodes not yet implemented
if( do_reverse_geocode == true )
updateAddress(marker_id);
var zoom = undefined;
if( do_zoom == false )
zoom = OSRM.G.map.getZoom();
return;
OSRM.G.markers.route[index].show();
if( !OSRM.G.markers.route[index].dirty_move || OSRM.G.markers.route[index].dirty_type )
OSRM.G.markers.route[index].centerView(zoom);
OSRM.G.markers.route[index].centerView();
getRoute(OSRM.C.FULL_DESCRIPTION);
OSRM.G.markers.route[index].dirty_move = false;
@ -92,37 +83,40 @@ function onclickGeocoderResult(marker_id, lat, lon, do_reverse_geocode, do_zoom
function showGeocoderResults_Source(response) { showGeocoderResults(OSRM.C.SOURCE_LABEL, response); }
function showGeocoderResults_Target(response) { showGeocoderResults(OSRM.C.TARGET_LABEL, response); }
function showGeocoderResults(marker_id, response) {
if(response){
if(response.length == 0) {
showGeocoderResults_Empty(marker_id);
return;
}
var html = "";
html += '<table class="results-table">';
for(var i=0; i < response.length; i++){
var result = response[i];
//odd or even ?
var rowstyle='results-odd';
if(i%2==0) { rowstyle='results-even'; }
html += '<tr class="'+rowstyle+'">';
html += '<td class="result-counter"><span">'+(i+1)+'.</span></td>';
html += '<td class="result-items">';
if(result.display_name){
html += '<div class="result-item" onclick="onclickGeocoderResult(\''+marker_id+'\', '+result.lat+', '+result.lon+');">'+result.display_name+'</div>';
}
html += "</td></tr>";
}
html += '</table>';
document.getElementById('information-box-headline').innerHTML = OSRM.loc("SEARCH_RESULTS")+":";
document.getElementById('information-box').innerHTML = html;
onclickGeocoderResult(marker_id, response[0].lat, response[0].lon);
if(!response){
showGeocoderResults_Empty(marker_id);
return;
}
if(response.length == 0) {
showGeocoderResults_Empty(marker_id);
return;
}
var html = "";
html += '<table class="results-table">';
for(var i=0; i < response.length; i++){
var result = response[i];
//odd or even ?
var rowstyle='results-odd';
if(i%2==0) { rowstyle='results-even'; }
html += '<tr class="'+rowstyle+'">';
html += '<td class="result-counter"><span">'+(i+1)+'.</span></td>';
html += '<td class="result-items">';
if(result.display_name){
html += '<div class="result-item" onclick="onclickGeocoderResult(\''+marker_id+'\', '+result.lat+', '+result.lon+');">'+result.display_name+'</div>';
}
html += "</td></tr>";
}
html += '</table>';
document.getElementById('information-box-headline').innerHTML = OSRM.loc("SEARCH_RESULTS")+":";
document.getElementById('information-box').innerHTML = html;
onclickGeocoderResult(marker_id, response[0].lat, response[0].lon);
}
function showGeocoderResults_Empty(marker_id) {
document.getElementById('information-box-headline').innerHTML = OSRM.loc("SEARCH_RESULTS")+":";
@ -143,6 +137,7 @@ function showGeocoderResults_Timeout() {
// update address in input boxes
function updateAddress(marker_id, do_fallback_to_lat_lng) {
// build request for reverse geocoder
var lat = null;
var lng = null;
@ -156,22 +151,31 @@ function updateAddress(marker_id, do_fallback_to_lat_lng) {
return;
var call = OSRM.DEFAULTS.HOST_REVERSE_GEOCODER_URL + "?format=json" + "&lat=" + lat + "&lon=" + lng;
OSRM.JSONP.call( call, processReverseGeocoderResponse, processReverseGeocoderResponse_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 processReverseGeocoderResponse_Timeout(response, parameters) {
if(parameters.do_fallback)
updateLocation(parameters.marker_id);
function showReverseGeocoderResults_Timeout(response, parameters) {
if(!parameters.do_fallback)
return;
updateLocation(parameters.marker_id);
if(parameters.marker_id == OSRM.C.SOURCE_LABEL && OSRM.G.markers.hasSource() ) {
OSRM.G.markers.route[0].dirty_move = false;
OSRM.G.markers.route[0].dirty_type = false;
} else if(parameters.marker_id == OSRM.C.TARGET_LABEL && OSRM.G.markers.hasTarget() ) {
OSRM.G.markers.route[OSRM.G.markers.route.length-1].dirty_move = false;
OSRM.G.markers.route[OSRM.G.markers.route.length-1].dirty_type = false;
}
}
function processReverseGeocoderResponse(response, parameters) {
function showReverseGeocoderResults(response, parameters) {
if(!response) {
processReverseGeocoderResponse_Timeout(response, parameters);
showReverseGeocoderResults_Timeout(response, parameters);
return;
}
if(response.address == undefined) {
processReverseGeocoderResponse_Timeout(response, parameters);
showReverseGeocoderResults_Timeout(response, parameters);
return;
}
@ -200,7 +204,7 @@ function processReverseGeocoderResponse(response, parameters) {
used_address_data++;
}
if( used_address_data == 0 ) {
processReverseGeocoderResponse_Timeout(response, parameters);
showReverseGeocoderResults_Timeout(response, parameters);
return;
}

View File

@ -88,13 +88,13 @@ or see http://www.gnu.org/licenses/agpl.txt.
<table class="full">
<tr>
<td id="gui-search-source-label">Start:</td>
<td><input id="input-source-name" class="input-box" type="text" maxlength="200" value="" title="Startposition eingeben" onchange="inputChanged(OSRM.C.SOURCE_LABEL);"/></td>
<td><input id="input-source-name" class="input-box" type="text" maxlength="200" value="" title="Startposition eingeben" onchange="inputChanged(OSRM.C.SOURCE_LABEL);" /></td>
<td class="right"><a class="button not-selectable" id="gui-search-source" onclick="showMarker('source')">Zeigen</a></td>
</tr>
<tr>
<td id="gui-search-target-label">Ende:</td>
<td><input id="input-target-name" class="input-box" type="text" maxlength="200" value="" title="Zielposition eingeben" onchange="inputChanged(OSRM.C.TARGET_LABEL);"/></td>
<td><input id="input-target-name" class="input-box" type="text" maxlength="200" value="" title="Zielposition eingeben" onchange="inputChanged(OSRM.C.TARGET_LABEL);" /></td>
<td class="right"><a class="button not-selectable" id="gui-search-target" onclick="showMarker('target');">Zeigen</a></td>
</tr>
</table>

View File

@ -157,10 +157,19 @@ function initMap() {
// click on map to set source and target nodes
OSRM.G.map.on('click', function(e) {
if( !OSRM.G.markers.hasSource() )
onclickGeocoderResult(OSRM.C.SOURCE_LABEL, e.latlng.lat, e.latlng.lng, true, false );
else if( !OSRM.G.markers.hasTarget() )
onclickGeocoderResult(OSRM.C.TARGET_LABEL, e.latlng.lat, e.latlng.lng, true, false );
if( !OSRM.G.markers.hasSource() ) {
var index = OSRM.G.markers.setSource( e.latlng );
updateAddress( OSRM.C.SOURCE_LABEL, OSRM.C.DO_FALLBACK_TO_LAT_LNG );
OSRM.G.markers.route[index].show();
OSRM.G.markers.route[index].centerView( OSRM.G.map.getZoom() );
getRoute( OSRM.C.FULL_DESCRIPTION );
} else if( !OSRM.G.markers.hasTarget() ) {
var index = OSRM.G.markers.setTarget( e.latlng );
updateAddress( OSRM.C.TARGET_LABEL, OSRM.C.DO_FALLBACK_TO_LAT_LNG );
OSRM.G.markers.route[index].show();
OSRM.G.markers.route[index].centerView( OSRM.G.map.getZoom() );
getRoute( OSRM.C.FULL_DESCRIPTION );
}
} );
}
@ -217,9 +226,13 @@ function checkURL(){
// case 1: destination given
if( destination != undefined ) {
onclickGeocoderResult(OSRM.C.TARGET_LABEL, destination.lat, destination.lng, (destination_name == undefined) );
if( destination_name != undefined )
var index = OSRM.G.markers.setTarget( e.latlng );
if( destination_name == null )
updateAddress( OSRM.C.TARGET_LABEL, OSRM.C.DO_FALLBACK_TO_LAT_LNG );
else
document.getElementById("input-target-name").value = destination_name;
OSRM.G.markers.route[index].show();
OSRM.G.markers.route[index].centerView();
return;
}
@ -227,12 +240,12 @@ function checkURL(){
if( positions != []) {
// draw via points
if( positions.length > 0) {
onclickGeocoderResult(OSRM.C.SOURCE_LABEL, positions[0].lat, positions[0].lng, true, false );
//OSRM.G.markers.setSource( positions[0] );
OSRM.G.markers.setSource( positions[0] );
updateAddress( OSRM.C.SOURCE_LABEL, OSRM.C.DO_FALLBACK_TO_LAT_LNG );
}
if(positions.length > 1) {
onclickGeocoderResult(OSRM.C.TARGET_LABEL, positions[positions.length-1].lat, positions[positions.length-1].lng, true, false );
//OSRM.G.markers.setTarget( positions[positions.length-1] );
OSRM.G.markers.setTarget( positions[positions.length-1] );
updateAddress( OSRM.C.TARGET_LABEL, OSRM.C.DO_FALLBACK_TO_LAT_LNG );
}
for(var i=1; i<positions.length-1;i++)
OSRM.G.markers.setVia( i-1, positions[i] );

View File

@ -399,7 +399,7 @@ function getDirectionIcon(name) {
"Enter roundabout and leave at first exit":"round-about.png",
"Enter roundabout and leave at second exit":"round-about.png",
"Enter roundabout and leave at third exit":"round-about.png",
"Enter roundabout and leave at forth exit":"round-about.png",
"Enter roundabout and leave at fourth exit":"round-about.png",
"Enter roundabout and leave at fifth exit":"round-about.png",
"Enter roundabout and leave at sixth exit":"round-about.png",
"Enter roundabout and leave at seventh exit":"round-about.png",