zoom/pan to route when it doesn't fit on the screen

This commit is contained in:
DennisSchiefer 2012-07-03 13:37:12 +01:00
parent 232409d70b
commit d88baaf1a4
2 changed files with 49 additions and 0 deletions

View File

@ -18,6 +18,36 @@ or see http://www.gnu.org/licenses/agpl.txt.
// map view/model
// [extending Leaflet L.Map with setView/fitBounds methods that respect UI visibility, better layerControl]
OSRM.MapView = L.Map.extend({
_boundsInsideView: function(bounds) {
var viewBounds = this.getBounds(),
viewSw = this.project(viewBounds.getSouthWest()),
viewNe = this.project(viewBounds.getNorthEast()),
sw = this.project(bounds.getSouthWest()),
ne = this.project(bounds.getNorthEast());
if (viewNe.y > ne.y) { // north
return false;
}
if (viewNe.x < ne.x) { // east
return false;
}
if (viewSw.y < sw.y) { // south
return false;
}
if (viewSw.x > sw.x) { // west
return false;
}
return true;
},
setViewBounds: function(bounds) {
var zoom = this.getBoundsZoom(bounds); // maximum zoom level at which the bounds fit onto the map
if( this._zoom > zoom ) { // if current zoom level is too close change zoom level and recenter
this.setView(bounds.getCenter(), zoom);
} else if(!this._boundsInsideView(bounds)){ // if current zoom level is okay, but bounds are outside the viewport, pan
this.setView(bounds.getCenter(), this._zoom);
}
},
setViewUI: function(position, zoom, no_animation) {
if( OSRM.G.main_handle.boxVisible() ) {
var point = this.project( position, zoom);
@ -26,6 +56,23 @@ OSRM.MapView = L.Map.extend({
}
this.setView( position, zoom, no_animation);
},
setViewBoundsUI: function(bounds) {
var southwest = bounds.getSouthWest();
var northeast = bounds.getNorthEast();
var zoom = this.getBoundsZoom(bounds);
var sw_point = this.project( southwest, zoom);
if( OSRM.G.main_handle.boxVisible() )
sw_point.x-=OSRM.G.main_handle.boxWidth()+20;
else
sw_point.x-=20;
sw_point.y+=20;
var ne_point = this.project( northeast, zoom);
ne_point.y-=20;
ne_point.x+=20;
bounds.extend( this.unproject(sw_point,zoom) );
bounds.extend( this.unproject(ne_point,zoom) );
this.setViewBounds( bounds );
},
fitBoundsUI: function(bounds) {
var southwest = bounds.getSouthWest();
var northeast = bounds.getNorthEast();

View File

@ -71,6 +71,8 @@ showRoute: function(response) {
OSRM.Routing._snapRoute();
}
OSRM.Routing._updateHints(response);
var bounds = new L.LatLngBounds( OSRM.G.route._current_route.getPositions() );
OSRM.G.map.setViewBoundsUI(bounds);
},
showRoute_Dragging: function(response) {
if(!response)