Refactored Geocoder -> now resides in OSRM namespace

This commit is contained in:
DennisSchiefer 2012-03-23 09:55:55 +01:00
parent fdace26222
commit e0602934b2
5 changed files with 46 additions and 42 deletions

View File

@ -23,32 +23,34 @@ or see http://www.gnu.org/licenses/agpl.txt.
OSRM.CONSTANTS.SOURCE_LABEL = "source"; OSRM.CONSTANTS.SOURCE_LABEL = "source";
OSRM.CONSTANTS.TARGET_LABEL = "target"; OSRM.CONSTANTS.TARGET_LABEL = "target";
OSRM.CONSTANTS.VIA_LABEL = "via"; OSRM.CONSTANTS.VIA_LABEL = "via";
OSRM.C.DO_FALLBACK_TO_LAT_LNG = true; OSRM.CONSTANTS.DO_FALLBACK_TO_LAT_LNG = true;
OSRM.Geocoder = {
//[normal geocoding] //[normal geocoding]
// process input request and call geocoder if needed // process input request and call geocoder if needed
function callGeocoder(marker_id, query) { call: function(marker_id, query) {
if(query=="") if(query=="")
return; return;
//geo coordinates given -> directly draw results //geo coordinates given -> directly draw results
if(query.match(/^\s*[-+]?[0-9]*\.?[0-9]+\s*[,;]\s*[-+]?[0-9]*\.?[0-9]+\s*$/)){ if(query.match(/^\s*[-+]?[0-9]*\.?[0-9]+\s*[,;]\s*[-+]?[0-9]*\.?[0-9]+\s*$/)){
var coord = query.split(/[,;]/); var coord = query.split(/[,;]/);
onclickGeocoderResult(marker_id, coord[0], coord[1]); OSRM.Geocoder._onclickResult(marker_id, coord[0], coord[1]);
updateAddress( marker_id ); OSRM.Geocoder.updateAddress( marker_id );
return; return;
} }
//build request for geocoder //build request for geocoder
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, showGeocoderResults_Timeout, OSRM.DEFAULTS.JSONP_TIMEOUT, "geocoder_"+marker_id, {marker_id:marker_id,query:query} ); OSRM.JSONP.call( call, OSRM.Geocoder._showResults, OSRM.Geocoder._showResults_Timeout, OSRM.DEFAULTS.JSONP_TIMEOUT, "geocoder_"+marker_id, {marker_id:marker_id,query:query} );
} },
// helper function for clicks on geocoder search results // helper function for clicks on geocoder search results
function onclickGeocoderResult(marker_id, lat, lon) { _onclickResult: function(marker_id, lat, lon) {
var index; var index;
if( marker_id == OSRM.C.SOURCE_LABEL ) if( marker_id == OSRM.C.SOURCE_LABEL )
index = OSRM.G.markers.setSource( new L.LatLng(lat, lon) ); index = OSRM.G.markers.setSource( new L.LatLng(lat, lon) );
@ -60,18 +62,18 @@ function onclickGeocoderResult(marker_id, lat, lon) {
OSRM.G.markers.route[index].show(); OSRM.G.markers.route[index].show();
OSRM.G.markers.route[index].centerView(); OSRM.G.markers.route[index].centerView();
getRoute(OSRM.C.FULL_DESCRIPTION); getRoute(OSRM.C.FULL_DESCRIPTION);
} },
// process geocoder response // process geocoder response
function showGeocoderResults(response, parameters) { _showResults: function(response, parameters) {
if(!response){ if(!response){
showGeocoderResults_Empty(parameters); OSRM.Geocoder._showResults_Empty(parameters);
return; return;
} }
if(response.length == 0) { if(response.length == 0) {
showGeocoderResults_Empty(parameters); OSRM.Geocoder._showResults_Empty(parameters);
return; return;
} }
@ -90,7 +92,7 @@ function showGeocoderResults(response, parameters) {
html += '<td class="result-items">'; html += '<td class="result-items">';
if(result.display_name){ if(result.display_name){
html += '<div class="result-item" onclick="onclickGeocoderResult(\''+parameters.marker_id+'\', '+result.lat+', '+result.lon+');">'+result.display_name+'</div>'; html += '<div class="result-item" onclick="OSRM.Geocoder._onclickResult(\''+parameters.marker_id+'\', '+result.lat+', '+result.lon+');">'+result.display_name+'</div>';
} }
html += "</td></tr>"; html += "</td></tr>";
} }
@ -99,9 +101,9 @@ function showGeocoderResults(response, parameters) {
document.getElementById('information-box-headline').innerHTML = OSRM.loc("SEARCH_RESULTS")+":"; document.getElementById('information-box-headline').innerHTML = OSRM.loc("SEARCH_RESULTS")+":";
document.getElementById('information-box').innerHTML = html; document.getElementById('information-box').innerHTML = html;
onclickGeocoderResult(parameters.marker_id, response[0].lat, response[0].lon); OSRM.Geocoder._onclickResult(parameters.marker_id, response[0].lat, response[0].lon);
} },
function showGeocoderResults_Empty(parameters) { _showResults_Empty: function(parameters) {
document.getElementById('information-box-headline').innerHTML = OSRM.loc("SEARCH_RESULTS")+":"; document.getElementById('information-box-headline').innerHTML = OSRM.loc("SEARCH_RESULTS")+":";
if(parameters.marker_id == OSRM.C.SOURCE_LABEL) if(parameters.marker_id == OSRM.C.SOURCE_LABEL)
document.getElementById('information-box').innerHTML = "<br><p style='font-size:14px;font-weight:bold;text-align:center;'>"+OSRM.loc("NO_RESULTS_FOUND_SOURCE")+": "+parameters.query +".<p>"; document.getElementById('information-box').innerHTML = "<br><p style='font-size:14px;font-weight:bold;text-align:center;'>"+OSRM.loc("NO_RESULTS_FOUND_SOURCE")+": "+parameters.query +".<p>";
@ -109,27 +111,27 @@ function showGeocoderResults_Empty(parameters) {
document.getElementById('information-box').innerHTML = "<br><p style='font-size:14px;font-weight:bold;text-align:center;'>"+OSRM.loc("NO_RESULTS_FOUND_TARGET")+": "+parameters.query +".<p>"; document.getElementById('information-box').innerHTML = "<br><p style='font-size:14px;font-weight:bold;text-align:center;'>"+OSRM.loc("NO_RESULTS_FOUND_TARGET")+": "+parameters.query +".<p>";
else else
document.getElementById('information-box').innerHTML = "<br><p style='font-size:14px;font-weight:bold;text-align:center;'>"+OSRM.loc("NO_RESULTS_FOUND")+": "+parameters.query +".<p>"; document.getElementById('information-box').innerHTML = "<br><p style='font-size:14px;font-weight:bold;text-align:center;'>"+OSRM.loc("NO_RESULTS_FOUND")+": "+parameters.query +".<p>";
} },
function showGeocoderResults_Timeout() { _showResults_Timeout: function() {
document.getElementById('information-box-headline').innerHTML = OSRM.loc("SEARCH_RESULTS")+":"; document.getElementById('information-box-headline').innerHTML = OSRM.loc("SEARCH_RESULTS")+":";
document.getElementById('information-box').innerHTML = "<br><p style='font-size:14px;font-weight:bold;text-align:center;'>"+OSRM.loc("TIMED_OUT")+".<p>"; document.getElementById('information-box').innerHTML = "<br><p style='font-size:14px;font-weight:bold;text-align:center;'>"+OSRM.loc("TIMED_OUT")+".<p>";
} },
// [reverse geocoding] // [reverse geocoding]
//update geo coordinates in input boxes //update geo coordinates in input boxes
function updateLocation(marker_id) { updateLocation: function(marker_id) {
if (marker_id == OSRM.C.SOURCE_LABEL && OSRM.G.markers.hasSource()) { if (marker_id == OSRM.C.SOURCE_LABEL && OSRM.G.markers.hasSource()) {
document.getElementById("input-source-name").value = OSRM.G.markers.route[0].getLat().toFixed(6) + ", " + OSRM.G.markers.route[0].getLng().toFixed(6); document.getElementById("input-source-name").value = OSRM.G.markers.route[0].getLat().toFixed(6) + ", " + OSRM.G.markers.route[0].getLng().toFixed(6);
} else if (marker_id == OSRM.C.TARGET_LABEL && OSRM.G.markers.hasTarget()) { } 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].getLat().toFixed(6) + ", " + OSRM.G.markers.route[OSRM.G.markers.route.length-1].getLng().toFixed(6); document.getElementById("input-target-name").value = OSRM.G.markers.route[OSRM.G.markers.route.length-1].getLat().toFixed(6) + ", " + OSRM.G.markers.route[OSRM.G.markers.route.length-1].getLng().toFixed(6);
} }
} },
// update address in input boxes // update address in input boxes
function updateAddress(marker_id, do_fallback_to_lat_lng) { updateAddress: function(marker_id, do_fallback_to_lat_lng) {
// build request for reverse geocoder // build request for reverse geocoder
var lat = null; var lat = null;
var lng = null; var lng = null;
@ -144,19 +146,19 @@ function updateAddress(marker_id, do_fallback_to_lat_lng) {
return; return;
var call = OSRM.DEFAULTS.HOST_REVERSE_GEOCODER_URL + "?format=json" + "&lat=" + lat + "&lon=" + lng; var call = OSRM.DEFAULTS.HOST_REVERSE_GEOCODER_URL + "?format=json" + "&lat=" + lat + "&lon=" + 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, OSRM.Geocoder._showReverseResults, OSRM.Geocoder._showReverseResults_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 // processing JSONP response of reverse geocoder
function showReverseGeocoderResults(response, parameters) { _showReverseResults: function(response, parameters) {
if(!response) { if(!response) {
showReverseGeocoderResults_Timeout(response, parameters); OSRM.Geocoder._showReverseResults_Timeout(response, parameters);
return; return;
} }
if(response.address == undefined) { if(response.address == undefined) {
showReverseGeocoderResults_Timeout(response, parameters); OSRM.Geocoder._showReverseResults_Timeout(response, parameters);
return; return;
} }
@ -185,7 +187,7 @@ function showReverseGeocoderResults(response, parameters) {
used_address_data++; used_address_data++;
} }
if( used_address_data == 0 ) { if( used_address_data == 0 ) {
showReverseGeocoderResults_Timeout(response, parameters); OSRM.Geocoder._showReverseResults_Timeout(response, parameters);
return; return;
} }
@ -194,10 +196,12 @@ function showReverseGeocoderResults(response, parameters) {
document.getElementById("input-source-name").value = address; document.getElementById("input-source-name").value = address;
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) { _showReverseResults_Timeout: function(response, parameters) {
if(!parameters.do_fallback) if(!parameters.do_fallback)
return; return;
updateLocation(parameters.marker_id); updateLocation(parameters.marker_id);
} }
};

View File

@ -120,7 +120,7 @@ onClick: function(e) {
onDrag: function(e) { onDrag: function(e) {
this.parent.setPosition( e.target.getLatLng() ); this.parent.setPosition( e.target.getLatLng() );
getRoute(OSRM.C.NO_DESCRIPTION); getRoute(OSRM.C.NO_DESCRIPTION);
updateLocation( this.parent.label ); OSRM.Geocoder.updateLocation( this.parent.label );
}, },
onDragStart: function(e) { onDragStart: function(e) {
OSRM.G.dragging = true; OSRM.G.dragging = true;
@ -146,7 +146,7 @@ onDragEnd: function(e) {
} }
if(OSRM.G.route.isShown()==false) if(OSRM.G.route.isShown()==false)
updateAddress(this.parent.label); OSRM.Geocoder.updateAddress(this.parent.label);
}, },
toString: function() { toString: function() {
return "OSRM.RouteMarker: \""+this.label+"\", "+this.position+")"; return "OSRM.RouteMarker: \""+this.label+"\", "+this.position+")";

View File

@ -53,10 +53,10 @@ or see http://www.gnu.org/licenses/agpl.txt.
<script src="OSRM.Route.js" type="text/javascript"></script> <script src="OSRM.Route.js" type="text/javascript"></script>
<script src="OSRM.Localization.js" type="text/javascript"></script> <script src="OSRM.Localization.js" type="text/javascript"></script>
<script src="OSRM.Geocoder.js" type="text/javascript"></script>
<script src="main.js" type="text/javascript"></script> <script src="main.js" type="text/javascript"></script>
<script src="routing.js" type="text/javascript"></script> <script src="routing.js" type="text/javascript"></script>
<script src="geocoder.js" type="text/javascript"></script>
<script src="via.js" type="text/javascript"></script> <script src="via.js" type="text/javascript"></script>
<script src="utils.js" type="text/javascript"></script> <script src="utils.js" type="text/javascript"></script>

View File

@ -159,13 +159,13 @@ function initMap() {
OSRM.G.map.on('click', function(e) { OSRM.G.map.on('click', function(e) {
if( !OSRM.G.markers.hasSource() ) { if( !OSRM.G.markers.hasSource() ) {
var index = OSRM.G.markers.setSource( e.latlng ); var index = OSRM.G.markers.setSource( e.latlng );
updateAddress( OSRM.C.SOURCE_LABEL, OSRM.C.DO_FALLBACK_TO_LAT_LNG ); OSRM.Geocoder.updateAddress( OSRM.C.SOURCE_LABEL, OSRM.C.DO_FALLBACK_TO_LAT_LNG );
OSRM.G.markers.route[index].show(); OSRM.G.markers.route[index].show();
OSRM.G.markers.route[index].centerView( OSRM.G.map.getZoom() ); OSRM.G.markers.route[index].centerView( OSRM.G.map.getZoom() );
getRoute( OSRM.C.FULL_DESCRIPTION ); getRoute( OSRM.C.FULL_DESCRIPTION );
} else if( !OSRM.G.markers.hasTarget() ) { } else if( !OSRM.G.markers.hasTarget() ) {
var index = OSRM.G.markers.setTarget( e.latlng ); var index = OSRM.G.markers.setTarget( e.latlng );
updateAddress( OSRM.C.TARGET_LABEL, OSRM.C.DO_FALLBACK_TO_LAT_LNG ); OSRM.Geocoder.updateAddress( OSRM.C.TARGET_LABEL, OSRM.C.DO_FALLBACK_TO_LAT_LNG );
OSRM.G.markers.route[index].show(); OSRM.G.markers.route[index].show();
OSRM.G.markers.route[index].centerView( OSRM.G.map.getZoom() ); OSRM.G.markers.route[index].centerView( OSRM.G.map.getZoom() );
getRoute( OSRM.C.FULL_DESCRIPTION ); getRoute( OSRM.C.FULL_DESCRIPTION );
@ -228,7 +228,7 @@ function checkURL(){
if( destination != undefined ) { if( destination != undefined ) {
var index = OSRM.G.markers.setTarget( e.latlng ); var index = OSRM.G.markers.setTarget( e.latlng );
if( destination_name == null ) if( destination_name == null )
updateAddress( OSRM.C.TARGET_LABEL, OSRM.C.DO_FALLBACK_TO_LAT_LNG ); OSRM.Geocoder.updateAddress( OSRM.C.TARGET_LABEL, OSRM.C.DO_FALLBACK_TO_LAT_LNG );
else else
document.getElementById("input-target-name").value = destination_name; document.getElementById("input-target-name").value = destination_name;
OSRM.G.markers.route[index].show(); OSRM.G.markers.route[index].show();
@ -241,11 +241,11 @@ function checkURL(){
// draw via points // draw via points
if( positions.length > 0) { if( positions.length > 0) {
OSRM.G.markers.setSource( positions[0] ); OSRM.G.markers.setSource( positions[0] );
updateAddress( OSRM.C.SOURCE_LABEL, OSRM.C.DO_FALLBACK_TO_LAT_LNG ); OSRM.Geocoder.updateAddress( OSRM.C.SOURCE_LABEL, OSRM.C.DO_FALLBACK_TO_LAT_LNG );
} }
if(positions.length > 1) { if(positions.length > 1) {
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 ); OSRM.Geocoder.updateAddress( OSRM.C.TARGET_LABEL, OSRM.C.DO_FALLBACK_TO_LAT_LNG );
} }
for(var i=1; i<positions.length-1;i++) for(var i=1; i<positions.length-1;i++)
OSRM.G.markers.setVia( i-1, positions[i] ); OSRM.G.markers.setVia( i-1, positions[i] );

View File

@ -379,8 +379,8 @@ function snapRoute() {
for(var i=0; i<OSRM.G.via_points.length; i++) for(var i=0; i<OSRM.G.via_points.length; i++)
OSRM.G.markers.route[i+1].setPosition( new L.LatLng(OSRM.G.via_points[i][0], OSRM.G.via_points[i][1]) ); OSRM.G.markers.route[i+1].setPosition( new L.LatLng(OSRM.G.via_points[i][0], OSRM.G.via_points[i][1]) );
updateAddress(OSRM.C.SOURCE_LABEL); OSRM.Geocoder.updateAddress(OSRM.C.SOURCE_LABEL);
updateAddress(OSRM.C.TARGET_LABEL); OSRM.Geocoder.updateAddress(OSRM.C.TARGET_LABEL);
} }
// map driving instructions to icons // map driving instructions to icons
@ -484,7 +484,7 @@ function showMarker(marker_id) {
// changed: any inputbox (is called when return is pressed [after] or focus is lost [before]) // changed: any inputbox (is called when return is pressed [after] or focus is lost [before])
function inputChanged(marker_id) { function inputChanged(marker_id) {
if( marker_id == OSRM.C.SOURCE_LABEL) if( marker_id == OSRM.C.SOURCE_LABEL)
callGeocoder(OSRM.C.SOURCE_LABEL, document.getElementById('input-source-name').value); OSRM.Geocoder.call(OSRM.C.SOURCE_LABEL, document.getElementById('input-source-name').value);
else if( marker_id == OSRM.C.TARGET_LABEL) else if( marker_id == OSRM.C.TARGET_LABEL)
callGeocoder(OSRM.C.TARGET_LABEL, document.getElementById('input-target-name').value); OSRM.Geocoder.call(OSRM.C.TARGET_LABEL, document.getElementById('input-target-name').value);
} }