more work on history routes

This commit is contained in:
shiin 2012-05-13 20:28:24 +02:00
parent 101fc5d02f
commit 36f5b36c6b
7 changed files with 427 additions and 133 deletions

View File

@ -0,0 +1,178 @@
/*
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU AFFERO General Public License as published by
the Free Software Foundation; either version 3 of the License, or
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
or see http://www.gnu.org/licenses/agpl.txt.
*/
// OSRM route management (handles drawing of route geometry - current route, old route, unnamed route, highlight unnamed streets)
// [this holds the route geometry]
OSRM.HistoryRoute = function() {
// how to draw history routes and how many
this._history_styles = [{dashed:false, color:'#FFFFFF', opacity:0.5, weight:5},
{dashed:false, color:'#0000DD', opacity:0.45, weight:5},
{dashed:false, color:'#0000BB', opacity:0.40, weight:5},
{dashed:false, color:'#000099', opacity:0.35, weight:5},
{dashed:false, color:'#000077', opacity:0.30, weight:5},
{dashed:false, color:'#000055', opacity:0.25, weight:5},
{dashed:false, color:'#000033', opacity:0.20, weight:5},
{dashed:false, color:'#000011', opacity:0.15, weight:5},
{dashed:false, color:'#FF0000', opacity:0.50, weight:5}
];
this._history_routes = this._history_styles.length;
// actual history data
this._history_route = [];
this._history_data = [];
for(var i=0, size=this._history_routes; i<size; i++) {
this._history_route.push( new OSRM.SimpleRoute("current" , {dashed:false} ) );
this._history_data[i] = [];
}
// helper functions bound to this
this._initiate_redrawHistory = OSRM.bind(this, this._getRoute_RedrawHistory);
this._callback_redrawHistory = OSRM.bind(this, this._showRoute_RedrawHistory);
};
OSRM.extend( OSRM.HistoryRoute,{
activate: function() {
this.storeHistoryRoute = this._storeHistoryRoute;
this.fetchHistoryRoute = this._fetchHistoryRoute;
this.showHistoryRoutes = this._showHistoryRoutes;
this.clearHistoryRoutes = this._clearHistoryRoutes;
OSRM.G.map.on('zoomend', this._initiate_redrawHistory );
this.storeHistoryRoute();
},
deactivate: function() {
this.clearHistoryRoutes();
this.storeHistoryRoute = this.empty;
this.fetchHistoryRoute = this.empty;
this.showHistoryRoutes = this.empty;
this.clearHistoryRoutes = this.empty;
OSRM.G.map.off('zoomend', this._initiate_redrawHistory );
},
// empty function
empty: function() {},
storeHistoryRoute: function() {},
fetchHistoryRoute: function() {},
showHistoryRoutes: function() {},
clearHistoryRoutes: function() {},
// qctual functions
_storeHistoryRoute: function() {
var route = OSRM.G.route;
if(route.isShown() && route.isRoute()) {
console.log("store");
// store new route and positions in staging spot
this._history_route[0].setPositions( route.getPositions() );
this._history_data[0] = [];
var markers = OSRM.G.markers.route;
for(var i=0,size=markers.length; i<size; i++) {
var position = {
lat:markers[i].getLat(),
lng:markers[i].getLng(),
hint:markers[i].hint
};
this._history_data[0].push(position);
}
this._history_data[0].checksum = OSRM.G.markers.checksum;
}
},
_showHistoryRoutes: function() {
console.log("show");
for(var i=1,size=this._history_routes; i<size; i++) {
this._history_route[i].setStyle( this._history_styles[i] );
this._history_route[i].show();
}
},
_clearHistoryRoutes: function() {
for(var i=0,size=this._history_routes; i<size; i++) {
this._history_route[i].hide();
this._history_route[i].setPositions( [] );
this._history_data[i] = [];
}
},
_fetchHistoryRoute: function() {
if( this._history_data[0].length == 0)
return;
if( this._equalRoute() )
return;
console.log("fetch");
// move route and positions
for(var i=this._history_routes-1; i>0; i--) {
this._history_route[i].setPositions( this._history_route[i-1].getPositions() );
this._history_data[i] = this._history_data[i-1];
}
// reset staging spot
this._history_route[0].setPositions( [] );
this._history_data[0] = [];
},
_equalRoute: function() {
var lhs = OSRM.G.markers.route;
var rhs = this._history_data[0];
for(var i=0,size=Math.min(rhs.length,lhs.length); i<size; i++) {
if( lhs[i].getLat() != rhs[i].lat || lhs[i].getLng() != rhs[i].lng) {
console.log("different routes");
return false;
}
}
console.log("same routes");
return true;
// OSRM.G.markers.route[0].setPosition( positions[0] );
// OSRM.G.markers.route[OSRM.G.markers.route.length-1].setPosition( positions[positions.length-1] );
// 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]) );
},
// redraw
_showRoute_RedrawHistory: function(response, history_id) {
if(!response)
return;
var positions = OSRM.RoutingGeometry._decode(response.route_geometry, 5);
this._history_route[history_id].setPositions(positions);
this._updateHints(response, history_id);
},
_getRoute_RedrawHistory: function() {
for(var i=0,size=this._history_routes; i<size; i++)
if( this._history_data[i].length > 0 ) {
OSRM.JSONP.clear('history'+i);
OSRM.JSONP.call(this._buildCall(i)+'&instructions=false', this._callback_redrawHistory, OSRM.JSONP.empty, OSRM.DEFAULTS.JSONP_TIMEOUT, 'history'+i, i);
}
},
_buildCall: function(history_id) {
var source = OSRM.DEFAULTS.HOST_ROUTING_URL;
source += '?z=' + OSRM.G.map.getZoom() + '&output=json&jsonp=%jsonp&geomformat=cmp';
var data = this._history_data[history_id];
if(data.checksum)
source += '&checksum=' + data.checksum;
for(var i=0,size=data.length; i<size; i++) {
source += '&loc=' + data[i].lat.toFixed(6) + ',' + data[i].lng.toFixed(6);
if( data[i].hint)
source += '&hint=' + data[i].hint;
}
return source;
},
_updateHints: function(response, history_id) {
var data = this._history_data[history_id];
data.checksum = response.hint_data.checksum;
var hint_locations = response.hint_data.locations;
for(var i=0; i<hint_locations.length; i++)
data[i].hint = hint_locations[i];
}
});

View File

@ -0,0 +1,212 @@
/*
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU AFFERO General Public License as published by
the Free Software Foundation; either version 3 of the License, or
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
or see http://www.gnu.org/licenses/agpl.txt.
*/
// OSRM route management (handles drawing of route geometry - current route, old route, unnamed route, highlight unnamed streets)
// [this holds the route geometry]
OSRM.HistoryRoute = function() {
// style and count of history routes
this._history_styles = [{dashed:false, color:'#FFFFFF', opacity:0.5, weight:5},
{dashed:false, color:'#0000DD', opacity:0.45, weight:5},
{dashed:false, color:'#0000BB', opacity:0.40, weight:5},
{dashed:false, color:'#000099', opacity:0.35, weight:5},
{dashed:false, color:'#000077', opacity:0.30, weight:5},
{dashed:false, color:'#000055', opacity:0.25, weight:5},
{dashed:false, color:'#000033', opacity:0.20, weight:5},
{dashed:false, color:'#000011', opacity:0.15, weight:5},
{dashed:false, color:'#FF0000', opacity:0.50, weight:5}
];
this._history_length = this._history_styles.length;
// actual history data
this._history = [];
for(var i=0, size=this._history_length; i<size; i++) {
var history = {};
history.route = new OSRM.SimpleRoute("current" , {dashed:false} );
history.markers = [];
history.checksum = null;
this._history.push(history);
}
// helper functions bound to this
this._initiate_redrawHistory = OSRM.bind(this, this._getRoute_RedrawHistory);
this._callback_redrawHistory = OSRM.bind(this, this._showRoute_RedrawHistory);
};
OSRM.extend( OSRM.HistoryRoute,{
// switch history routes on/off
activate: function() {
this.storeHistoryRoute = this._storeHistoryRoute;
this.fetchHistoryRoute = this._fetchHistoryRoute;
this.showHistoryRoutes = this._showHistoryRoutes;
this.clearHistoryRoutes = this._clearHistoryRoutes;
OSRM.G.map.on('zoomend', this._initiate_redrawHistory );
this.storeHistoryRoute();
},
deactivate: function() {
this.clearHistoryRoutes();
this.storeHistoryRoute = this.empty;
this.fetchHistoryRoute = this.empty;
this.showHistoryRoutes = this.empty;
this.clearHistoryRoutes = this.empty;
OSRM.G.map.off('zoomend', this._initiate_redrawHistory );
},
// empty function
empty: function() {},
storeHistoryRoute: function() {},
fetchHistoryRoute: function() {},
showHistoryRoutes: function() {},
clearHistoryRoutes: function() {},
// actual functions
_storeHistoryRoute: function() {
var route = OSRM.G.route;
if(route.isShown() && route.isRoute()) {
// store current route in staging spot
this._history[0].route.setPositions( route.getPositions() );
this._history[0].checksum = OSRM.G.markers.checksum;
this._history[0].markers = [];
var route = this._currentRoute();
for(var i=0,size=OSRM.G.response.hint_data.locations.length; i<size; i++) {
var position = {
lat:route[i].lat,
lng:route[i].lng,
//hint:OSRM.G.response.hint_data.locations[i]
};
this._history[0].markers.push(position);
}
console.log("store", this._history[0].markers);
}
},
_fetchHistoryRoute: function() {
var route = this._currentRoute();
if( OSRM.G.route.isShown() && this._equalRoute(this._history[0].markers, route) ) {
console.log("fetch failed 1");
return;
}
if( this._equalRoute(this._history[1].markers, this._history[0].markers) ) {
console.log("fetch failed 2");
return;
}
// move all routes down one position
for(var i=this._history_length-1; i>0; i--) {
this._history[i].route.setPositions( this._history[i-1].route.getPositions() ); // copying positions quicker than creating new route!
this._history[i].markers = this._history[i-1].markers;
this._history[i].checksum = this._history[i-1].checksum;
}
// reset staging spot
this._history[0].route.setPositions( [] );
this._history[0].markers = [];
this._history[0].checksum = null;
console.log("fetch", this._history[1].markers);
},
_showHistoryRoutes: function() {
console.log("show");
for(var i=1,size=this._history_length; i<size; i++) {
this._history[i].route.setStyle( this._history_styles[i] );
this._history[i].route.show();
}
},
_clearHistoryRoutes: function() {
console.log("clear");
for(var i=0,size=this._history_length; i<size; i++) {
this._history[i].route.hide();
this._history[i].route.setPositions( [] );
this._history[i].markers = [];
this._history[i].checksum = null;
}
},
//get current route
_currentRoute: function() {
var route = [];
var positions = OSRM.G.route.getPositions();
if(positions.length == 0)
return route;
route.push( {lat: positions[0].lat, lng: positions[0].lng });
for(var i=0; i<OSRM.G.response.via_points.length; i++)
route.push( {lat:OSRM.G.response.via_points[i][0], lng:OSRM.G.response.via_points[i][1]} );
route.push( {lat: positions[positions.length-1].lat, lng: positions[positions.length-1].lng });
return route;
},
// check if routes are the same
_equalRoute: function(lhs, rhs) {
console.log("lhs",lhs);
console.log("rhs",rhs);
if(lhs.length == 0) {
console.log("different routes");
return false;
}
for(var i=0,size=Math.min(rhs.length,lhs.length); i<size; i++) {
if( lhs[i].lat != rhs[i].lat || lhs[i].lng != rhs[i].lng) {
console.log("different routes");
return false;
}
}
console.log("same routes");
return true;
},
// requery history routes
_showRoute_RedrawHistory: function(response, history_id) {
if(!response)
return;
var positions = OSRM.RoutingGeometry._decode(response.route_geometry, 5);
this._history[history_id].route.setPositions(positions);
//this._updateHints(response, history_id);
},
_getRoute_RedrawHistory: function() {
for(var i=0,size=this._history_length; i<size; i++)
if( this._history[i].markers.length > 0 ) {
OSRM.JSONP.clear('history'+i);
OSRM.JSONP.call(this._buildCall(i)+'&instructions=false', this._callback_redrawHistory, OSRM.JSONP.empty, OSRM.DEFAULTS.JSONP_TIMEOUT, 'history'+i, i);
}
},
_buildCall: function(history_id) {
var source = OSRM.DEFAULTS.HOST_ROUTING_URL;
source += '?z=' + OSRM.G.map.getZoom() + '&output=json&jsonp=%jsonp&geomformat=cmp';
if(this._history[history_id].checksum)
source += '&checksum=' + this._history[history_id].checksum;
var history_markers = this._history[history_id].markers;
for(var i=0,size=history_markers.length; i<size; i++) {
source += '&loc=' + history_markers[i].lat.toFixed(6) + ',' + history_markers[i].lng.toFixed(6);
if( history_markers[i].hint )
source += '&hint=' + history_markers[i].hint;
}
return source;
},
_updateHints: function(response, history_id) {
this._history[history_id].checksum = response.hint_data.checksum;
var hints = response.hint_data.locations;
for(var i=0; i<hints.length; i++)
this._history[history_id].markers[i].hint = hints[i];
}
});

View File

@ -78,7 +78,6 @@ zoomed: function(e) {
OSRM.Routing.getRoute_Dragging();
else
OSRM.Routing.getRoute_Redraw();
// OSRM.Routing.getRoute_RedrawHistory();
},
contextmenu: function(e) {;},
mousemove: function(e) { OSRM.Via.drawDragMarker(e); },

View File

@ -31,35 +31,15 @@ OSRM.Route = function() {
this._unnamed_route_style = {dashed:false, color:'#FF00FF', weight:10};
this._old_unnamed_route_style = {dashed:false, color:'#990099', weight:10};
this._noroute = OSRM.Route.ROUTE;
this._history_styles = [{dashed:false, color:'#FF0000', weight:5},
{dashed:false, color:'#00FF00', weight:5},
{dashed:false, color:'#0000FF', weight:5},
{dashed:false, color:'#FF00FF', weight:5},
{dashed:false, color:'#00FFFF', weight:5},
{dashed:false, color:'#770000', weight:5},
{dashed:false, color:'#007700', weight:5},
{dashed:false, color:'#000077', weight:5},
{dashed:false, color:'#770077', weight:5},
{dashed:false, color:'#007777', weight:5}
];
this._history_routes = this._history_styles.length;
this._history_route = [];
this._history_data = [];
for(var i=0, size=this._history_routes; i<size; i++) {
this._history_route.push( new OSRM.SimpleRoute("current" , {dashed:false} ) );
this._history_data[i] = [];
}
this._noroute = OSRM.Route.ROUTE;
this._history = new OSRM.HistoryRoute();
};
OSRM.Route.NOROUTE = true;
OSRM.Route.ROUTE = false;
OSRM.extend( OSRM.Route,{
// show/hide route
showRoute: function(positions, noroute) {
this.fetchHistoryRoute();
this.showHistoryRoutes();
this._noroute = noroute;
this._current_route.setPositions( positions );
if ( this._noroute == OSRM.Route.NOROUTE )
@ -68,23 +48,22 @@ OSRM.extend( OSRM.Route,{
this._current_route.setStyle( this._current_route_style );
this._current_route.show();
//this._raiseUnnamedRoute();
this._history.fetchHistoryRoute();
this._history.showHistoryRoutes();
this._history.storeHistoryRoute();
},
hideRoute: function() {
this._current_route.hide();
this._unnamed_route.hide();
this.fetchHistoryRoute();
this.showHistoryRoutes();
this._history.fetchHistoryRoute();
this._history.showHistoryRoutes();
// deactivate printing
OSRM.Printing.deactivate();
},
hideAll: function() {
this.hideRoute();
this._old_route.hide();
this._noroute = OSRM.Route.ROUTE;
this.clearHistoryRoutes();
},
// show/hide highlighting for unnamed routes
showUnnamedRoute: function(positions) {
this._unnamed_route.clearRoutes();
for(var i=0; i<positions.length; i++) {
@ -102,7 +81,9 @@ OSRM.extend( OSRM.Route,{
this._unnamed_route.hide();
this._unnamed_route.show();
}
},
},
// show/hide previous route as shadow
showOldRoute: function() {
this._old_route.setPositions( this._current_route.getPositions() );
if ( this._noroute == OSRM.Route.NOROUTE)
@ -118,6 +99,7 @@ OSRM.extend( OSRM.Route,{
this._old_route.hide();
},
// query routines
isShown: function() {
return this._current_route.isShown();
},
@ -129,74 +111,27 @@ OSRM.extend( OSRM.Route,{
},
getPoints: function() {
return this._current_route.getPoints();
},
},
// helper routines
reset: function() {
this.hideRoute();
this._old_route.hide();
this._noroute = OSRM.Route.ROUTE;
this._history.clearHistoryRoutes();
},
fire: function(type,event) {
this._current_route.route.fire(type,event);
},
centerView: function() {
this._current_route.centerView();
},
},
// history route handling
storeHistoryRoute: function() {
if( document.getElementById('option-show-previous-routes').checked == false)
return;
if(this.isShown() && this.isRoute()) {
console.log("store");
// store new route and positions in staging spot
this._history_route[0].setPositions( this._current_route.getPositions() );
this._history_data[0] = [];
var markers = OSRM.G.markers.route;
for(var i=0,size=markers.length; i<size; i++) {
var position = {
lat:markers[i].getLat(),
lng:markers[i].getLng(),
hint:markers[i].hint
};
this._history_data[0].push(position);
}
}
// handle history routes
activateHistoryRoutes: function() {
this._history.activate();
},
showHistoryRoutes: function() {
if( document.getElementById('option-show-previous-routes').checked == false)
return;
console.log("show");
for(var i=1,size=this._history_routes; i<size; i++) {
this._history_route[i].setStyle( this._history_styles[i] );
this._history_route[i].show();
}
},
clearHistoryRoutes: function() {
for(var i=0,size=this._history_route.length; i<size; i++) {
this._history_route[i].hide();
this._history_route[i].setPositions( [] );
this._history_data[i] = [];
}
},
fetchHistoryRoute: function() {
if( document.getElementById('option-show-previous-routes').checked == false)
return;
if( this._history_data[0].length == 0)
return;
if( this._equalRoute() )
return;
console.log("fetch");
// move route and positions
for(var i=this._history_routes-1; i>0; i--) {
this._history_route[i].setPositions( this._history_route[i-1].getPositions() );
this._history_data[i] = this._history_data[i-1];
}
// reset staging spot
this._history_route[0].setPositions( [] );
this._history_data[0] = [];
},
_equalRoute: function() {
var lhs = OSRM.G.markers.route;
var rhs = this._history_data[0];
for(var i=0,size=Math.min(rhs.length,lhs.length); i<size; i++) {
if( lhs[i].getLat() != rhs[i].lat || lhs[i].getLng() != rhs[i].lng)
return false;
}
return true;
}
deactivateHistoryRoutes: function() {
this._history.deactivate();
}
});

View File

@ -48,7 +48,7 @@ resetRouting: function() {
document.getElementById('gui-input-source').value = "";
document.getElementById('gui-input-target').value = "";
OSRM.G.route.hideAll();
OSRM.G.route.reset();
OSRM.G.markers.reset();
document.getElementById('information-box').innerHTML = "";
@ -141,9 +141,9 @@ deleteMarker: function(marker_id) {
//click: checkbox "show previous routes"
showPreviousRoutes: function(value) {
if( document.getElementById('option-show-previous-routes').checked == false)
OSRM.G.route.clearHistoryRoutes();
OSRM.G.route.deactivateHistoryRoutes();
else
OSRM.G.route.storeHistoryRoute();
OSRM.G.route.activateHistoryRoutes();
}
});

View File

@ -57,6 +57,7 @@ or see http://www.gnu.org/licenses/agpl.txt.
<script src="base/OSRM.Map.js" type="text/javascript"></script>
<script src="base/OSRM.Markers.js" type="text/javascript"></script>
<script src="base/OSRM.Routes.js" type="text/javascript"></script>
<script src="base/OSRM.HistoryRoutes.js" type="text/javascript"></script>
<script src="gui/OSRM.GUI.js" type="text/javascript"></script>
<script src="gui/OSRM.GUIBoxGroup.js" type="text/javascript"></script>
<script src="gui/OSRM.GUIBoxHandle.js" type="text/javascript"></script>

View File

@ -69,7 +69,6 @@ showRoute: function(response) {
OSRM.RoutingNoNames.show(response);
OSRM.RoutingDescription.show(response);
OSRM.Routing._snapRoute();
OSRM.G.route.storeHistoryRoute();
}
OSRM.Routing._updateHints(response);
},
@ -78,14 +77,14 @@ showRoute_Dragging: function(response) {
return;
if( !OSRM.G.dragging ) // prevent simple routing when not dragging (required as there can be drag events after a dragstop event!)
return;
OSRM.G.response = response;
if( response.status == 207) {
OSRM.RoutingGeometry.showNA();
OSRM.RoutingDescription.showNA( OSRM.loc("YOUR_ROUTE_IS_BEING_COMPUTED") );
} else {
OSRM.RoutingGeometry.show(response);
OSRM.RoutingDescription.showSimple(response);
OSRM.G.route.storeHistoryRoute();
}
OSRM.Routing._updateHints(response);
@ -102,16 +101,6 @@ showRoute_Redraw: function(response) {
}
OSRM.Routing._updateHints(response);
},
showRoute_RedrawHistory: function(response, history_id) {
if(!response)
return;
if(response.status != 207) {
var positions = OSRM.RoutingGeometry._decode(response.route_geometry, 5);
OSRM.G.route._history_route[history_id].setPositions(positions);
}
},
//-- main function --
@ -146,13 +135,6 @@ getRoute_Redraw: function() {
OSRM.JSONP.clear('redraw');
OSRM.JSONP.call(OSRM.Routing._buildCall()+'&instructions=true', OSRM.Routing.showRoute_Redraw, OSRM.Routing.timeoutRoute, OSRM.DEFAULTS.JSONP_TIMEOUT, 'redraw');
},
getRoute_RedrawHistory: function() {
for(var i=0; i<10; i++)
if( OSRM.G.route._history_data[i].length > 0 ) {
OSRM.JSONP.clear('history'+i);
OSRM.JSONP.call(OSRM.Routing._buildHistoryCall(i)+'&instructions=false', OSRM.Routing.showRoute_RedrawHistory, OSRM.JSONP.empty, OSRM.DEFAULTS.JSONP_TIMEOUT, 'history'+i, i);
}
},
getRoute_Dragging: function() {
OSRM.G.pending = !OSRM.JSONP.call(OSRM.Routing._buildCall()+'&instructions=false', OSRM.Routing.showRoute_Dragging, OSRM.Routing.timeoutRoute_Dragging, OSRM.DEFAULTS.JSONP_TIMEOUT, 'dragging');;
},
@ -174,19 +156,6 @@ _buildCall: function() {
}
return source;
},
_buildHistoryCall: function(history_id) {
var source = OSRM.DEFAULTS.HOST_ROUTING_URL;
source += '?z=' + OSRM.G.map.getZoom() + '&output=json&jsonp=%jsonp&geomformat=cmp';
if(OSRM.G.markers.checksum)
source += '&checksum=' + OSRM.G.markers.checksum;
var markers = OSRM.G.route._history_data[history_id];
for(var i=0,size=markers.length; i<size; i++) {
source += '&loc=' + markers[i].lat.toFixed(6) + ',' + markers[i].lng.toFixed(6);
if( markers[i].hint)
source += '&hint=' + markers[i].hint;
}
return source;
},
//-- helper functions --