starting to reverse geocoding
This commit is contained in:
parent
4918549bac
commit
805402b230
@ -9,8 +9,8 @@ OSRM.JSONP = {
|
|||||||
|
|
||||||
TIMEOUT: OSRM.DEFAULTS.JSONP_TIMEOUT,
|
TIMEOUT: OSRM.DEFAULTS.JSONP_TIMEOUT,
|
||||||
|
|
||||||
late: function() { console.log("reply too late");},
|
late: function() { },//console.log("reply too late");},
|
||||||
empty: function() { console.log("empty callback");},
|
empty: function() { },//console.log("empty callback");},
|
||||||
|
|
||||||
call: function(source, callback_function, timeout_function, timeout, id) {
|
call: function(source, callback_function, timeout_function, timeout, id) {
|
||||||
// only one active JSONP call per id
|
// only one active JSONP call per id
|
||||||
|
@ -1,9 +1,70 @@
|
|||||||
// some constants
|
// some constants
|
||||||
OSRM.GEOCODE_POST = 'http://nominatim.openstreetmap.org/search?format=json';
|
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.SOURCE_MARKER_LABEL = "source";
|
||||||
OSRM.TARGET_MARKER_LABEL = "target";
|
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
|
// prepare request and call geocoder
|
||||||
function callGeocoder(marker_id, query) {
|
function callGeocoder(marker_id, query) {
|
||||||
//build request
|
//build request
|
||||||
|
@ -23,7 +23,7 @@
|
|||||||
|
|
||||||
<script src="OSRM.base.js"></script>
|
<script src="OSRM.base.js"></script>
|
||||||
<script src="OSRM.config.js"></script>
|
<script src="OSRM.config.js"></script>
|
||||||
<!-- <script defer="defer" src="OSRM.debug.js"></script> -->
|
<script defer="defer" src="OSRM.debug.js"></script>
|
||||||
|
|
||||||
<script src="OSRM.Browser.js"></script>
|
<script src="OSRM.Browser.js"></script>
|
||||||
<script src="OSRM.GUI.js"></script>
|
<script src="OSRM.GUI.js"></script>
|
||||||
|
@ -85,6 +85,7 @@ function showRoute(response) {
|
|||||||
snapRoute();
|
snapRoute();
|
||||||
}
|
}
|
||||||
updateHints(response);
|
updateHints(response);
|
||||||
|
updateLocations();
|
||||||
}
|
}
|
||||||
|
|
||||||
function showNoRouteGeometry() {
|
function showNoRouteGeometry() {
|
||||||
@ -346,6 +347,12 @@ function positionsToInput() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function reverseRouting() {
|
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();
|
my_markers.route.reverse();
|
||||||
if(my_markers.route.length == 1) {
|
if(my_markers.route.length == 1) {
|
||||||
if(my_markers.route[0].label == OSRM.TARGET_MARKER_LABEL) {
|
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].label = OSRM.TARGET_MARKER_LABEL;
|
||||||
my_markers.route[my_markers.route.length-1].marker.setIcon( new L.Icon('images/marker-target.png') );
|
my_markers.route[my_markers.route.length-1].marker.setIcon( new L.Icon('images/marker-target.png') );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// recompute route
|
||||||
getRoute(OSRM.FULL_DESCRIPTION);
|
getRoute(OSRM.FULL_DESCRIPTION);
|
||||||
my_markers.highlight.hide();
|
my_markers.highlight.hide();
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user