refactored reverse geocoder
This commit is contained in:
parent
92dbadebae
commit
97b9c65c97
@ -140,7 +140,7 @@ onDragEnd: function(e) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if(OSRM.G.route.isShown()==false)
|
if(OSRM.G.route.isShown()==false)
|
||||||
updateReverseGeocoder(this.parent.label);
|
updateAddress(this.parent.label);
|
||||||
},
|
},
|
||||||
toString: function() {
|
toString: function() {
|
||||||
return "OSRM.RouteMarker: \""+this.label+"\", "+this.position+")";
|
return "OSRM.RouteMarker: \""+this.label+"\", "+this.position+")";
|
||||||
|
@ -27,9 +27,9 @@ OSRM.CONSTANTS.VIA_LABEL = "via";
|
|||||||
|
|
||||||
// update geo coordinates in input boxes
|
// update geo coordinates in input boxes
|
||||||
function updateLocation(marker_id) {
|
function updateLocation(marker_id) {
|
||||||
if (marker_id == OSRM.C.SOURCE_LABEL) {
|
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);
|
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) {
|
} 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);
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -73,7 +73,7 @@ function onclickGeocoderResult(marker_id, lat, lon, do_reverse_geocode, do_zoom
|
|||||||
index = -1; // via nodes not yet implemented
|
index = -1; // via nodes not yet implemented
|
||||||
|
|
||||||
if( do_reverse_geocode == true )
|
if( do_reverse_geocode == true )
|
||||||
updateReverseGeocoder(marker_id);
|
updateAddress(marker_id);
|
||||||
var zoom = undefined;
|
var zoom = undefined;
|
||||||
if( do_zoom == false )
|
if( do_zoom == false )
|
||||||
zoom = OSRM.G.map.getZoom();
|
zoom = OSRM.G.map.getZoom();
|
||||||
@ -141,75 +141,77 @@ function showGeocoderResults_Timeout() {
|
|||||||
|
|
||||||
// - [upcoming feature: reverse geocoding (untested) ] -
|
// - [upcoming feature: reverse geocoding (untested) ] -
|
||||||
|
|
||||||
//update reverse geocoder informatiopn in input boxes
|
// update address in input boxes
|
||||||
function updateReverseGeocoder(marker_id) {
|
function updateAddress(marker_id, do_fallback_to_lat_lng) {
|
||||||
if (marker_id == OSRM.C.SOURCE_LABEL && OSRM.G.markers.hasSource()) { //&& OSRM.G.markers.route[0].dirty == true ) {
|
var lat = null;
|
||||||
//document.getElementById("input-source-name").value = OSRM.G.markers.route[0].getPosition().lat.toFixed(6) + ", " + OSRM.G.markers.route[0].getPosition().lng.toFixed(6);
|
var lng = null;
|
||||||
callReverseGeocoder(OSRM.C.SOURCE_LABEL, OSRM.G.markers.route[0].getPosition().lat, OSRM.G.markers.route[0].getPosition().lng);
|
|
||||||
} else if (marker_id == OSRM.C.TARGET_LABEL && OSRM.G.markers.hasTarget()) { //&& OSRM.G.markers.route[OSRM.G.markers.route.length-1].dirty == true) {
|
if(marker_id == OSRM.C.SOURCE_LABEL && OSRM.G.markers.hasSource()) {
|
||||||
//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);
|
lat = OSRM.G.markers.route[0].getPosition().lat;
|
||||||
callReverseGeocoder(OSRM.C.TARGET_LABEL, OSRM.G.markers.route[OSRM.G.markers.route.length-1].getPosition().lat, OSRM.G.markers.route[OSRM.G.markers.route.length-1].getPosition().lng);
|
lng = OSRM.G.markers.route[0].getPosition().lng;
|
||||||
}
|
} else if(marker_id == OSRM.C.TARGET_LABEL && OSRM.G.markers.hasTarget() ) {
|
||||||
|
lat = OSRM.G.markers.route[OSRM.G.markers.route.length-1].getPosition().lat;
|
||||||
|
lng = OSRM.G.markers.route[OSRM.G.markers.route.length-1].getPosition().lng;
|
||||||
|
} else
|
||||||
|
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} );
|
||||||
}
|
}
|
||||||
|
|
||||||
//prepare request and call reverse geocoder
|
// processing JSONP response of reverse geocoder
|
||||||
function callReverseGeocoder(marker_id, lat, lon) {
|
function processReverseGeocoderResponse_Timeout(response, parameters) {
|
||||||
//build request
|
if(parameters.do_fallback)
|
||||||
if (marker_id == OSRM.C.SOURCE_LABEL) {
|
updateLocation(parameters.marker_id);
|
||||||
var call = OSRM.DEFAULTS.HOST_REVERSE_GEOCODER_URL + "?format=json" + "&lat=" + lat + "&lon=" + lon;
|
|
||||||
OSRM.JSONP.call( call, showReverseGeocoderResults_Source, showReverseGeocoderResults_Timeout, OSRM.DEFAULTS.JSONP_TIMEOUT, "reverse_geocoder_source" );
|
|
||||||
} else if (marker_id == OSRM.C.TARGET_LABEL) {
|
|
||||||
var call = OSRM.DEFAULTS.HOST_REVERSE_GEOCODER_URL + "?format=json" + "&lat=" + lat + "&lon=" + lon;
|
|
||||||
OSRM.JSONP.call( call, showReverseGeocoderResults_Target, showReverseGeocoderResults_Timeout, OSRM.DEFAULTS.JSONP_TIMEOUT, "reverse_geocoder_target" );
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
//processing JSONP response of reverse geocoder
|
function processReverseGeocoderResponse(response, parameters) {
|
||||||
//(with wrapper functions for source/target jsonp)
|
if(!response) {
|
||||||
function showReverseGeocoderResults_Timeout() {}
|
processReverseGeocoderResponse_Timeout(response, parameters);
|
||||||
function showReverseGeocoderResults_Source(response) { showReverseGeocoderResults(OSRM.C.SOURCE_LABEL, response); }
|
return;
|
||||||
function showReverseGeocoderResults_Target(response) { showReverseGeocoderResults(OSRM.C.TARGET_LABEL, response); }
|
}
|
||||||
function showReverseGeocoderResults(marker_id, response) {
|
|
||||||
if(response){
|
if(response.address == undefined) {
|
||||||
if(response.address == undefined)
|
processReverseGeocoderResponse_Timeout(response, parameters);
|
||||||
return;
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// build reverse geocoding address
|
// build reverse geocoding address
|
||||||
var used_address_data = 0;
|
var used_address_data = 0;
|
||||||
var address = "";
|
var address = "";
|
||||||
if( response.address.road) {
|
if( response.address.road) {
|
||||||
address += response.address.road;
|
address += response.address.road;
|
||||||
used_address_data++;
|
used_address_data++;
|
||||||
}
|
}
|
||||||
if( response.address.city ) {
|
if( response.address.city ) {
|
||||||
if( used_address_data > 0 )
|
if( used_address_data > 0 )
|
||||||
address += ", ";
|
address += ", ";
|
||||||
address += response.address.city;
|
address += response.address.city;
|
||||||
used_address_data++;
|
used_address_data++;
|
||||||
} else if( response.address.village ) {
|
} else if( response.address.village ) {
|
||||||
if( used_address_data > 0 )
|
if( used_address_data > 0 )
|
||||||
address += ", ";
|
address += ", ";
|
||||||
address += response.address.village;
|
address += response.address.village;
|
||||||
used_address_data++;
|
used_address_data++;
|
||||||
}
|
}
|
||||||
if( used_address_data < 2 && response.address.country ) {
|
if( used_address_data < 2 && response.address.country ) {
|
||||||
if( used_address_data > 0 )
|
if( used_address_data > 0 )
|
||||||
address += ", ";
|
address += ", ";
|
||||||
address += response.address.country;
|
address += response.address.country;
|
||||||
used_address_data++;
|
used_address_data++;
|
||||||
}
|
}
|
||||||
if( used_address_data == 0 )
|
if( used_address_data == 0 ) {
|
||||||
return;
|
processReverseGeocoderResponse_Timeout(response, parameters);
|
||||||
|
return;
|
||||||
// add result to DOM
|
}
|
||||||
if(marker_id == OSRM.C.SOURCE_LABEL) {
|
|
||||||
document.getElementById("input-source-name").value = address;
|
|
||||||
OSRM.G.markers.route[0].dirty_move = false;
|
|
||||||
OSRM.G.markers.route[0].dirty_type = false;
|
|
||||||
} else if(marker_id == OSRM.C.TARGET_LABEL) {
|
|
||||||
document.getElementById("input-target-name").value = address;
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
// add result to DOM
|
||||||
|
if(parameters.marker_id == OSRM.C.SOURCE_LABEL && OSRM.G.markers.hasSource() ) {
|
||||||
|
document.getElementById("input-source-name").value = address;
|
||||||
|
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() ) {
|
||||||
|
document.getElementById("input-target-name").value = address;
|
||||||
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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]) );
|
||||||
|
|
||||||
updateReverseGeocoder(OSRM.C.SOURCE_LABEL);
|
updateAddress(OSRM.C.SOURCE_LABEL);
|
||||||
updateReverseGeocoder(OSRM.C.TARGET_LABEL);
|
updateAddress(OSRM.C.TARGET_LABEL);
|
||||||
}
|
}
|
||||||
|
|
||||||
// map driving instructions to icons
|
// map driving instructions to icons
|
||||||
|
Loading…
Reference in New Issue
Block a user