From d88baaf1a488bdf547905e581d60cdf396994f66 Mon Sep 17 00:00:00 2001 From: DennisSchiefer Date: Tue, 3 Jul 2012 13:37:12 +0100 Subject: [PATCH 1/2] zoom/pan to route when it doesn't fit on the screen --- WebContent/base/osrm/OSRM.MapView.js | 47 ++++++++++++++++++++++++++++ WebContent/routing/OSRM.Routing.js | 2 ++ 2 files changed, 49 insertions(+) diff --git a/WebContent/base/osrm/OSRM.MapView.js b/WebContent/base/osrm/OSRM.MapView.js index bceea9f1c..0f5f79e18 100644 --- a/WebContent/base/osrm/OSRM.MapView.js +++ b/WebContent/base/osrm/OSRM.MapView.js @@ -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(); diff --git a/WebContent/routing/OSRM.Routing.js b/WebContent/routing/OSRM.Routing.js index 8dccb7f63..b408a8df7 100644 --- a/WebContent/routing/OSRM.Routing.js +++ b/WebContent/routing/OSRM.Routing.js @@ -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) From 5e3de973d7361be87d4474a2ebed5bae79814668 Mon Sep 17 00:00:00 2001 From: DennisSchiefer Date: Wed, 4 Jul 2012 13:31:45 +0100 Subject: [PATCH 2/2] recentering/zooming on a route is only done, when the route is first drawn by clicking on the map and only if the rout doesn't fit into the current viewport --- WebContent/base/OSRM.Map.js | 6 +++--- WebContent/routing/OSRM.Routing.js | 12 +++++++----- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/WebContent/base/OSRM.Map.js b/WebContent/base/OSRM.Map.js index 6a298fc04..7e3df6823 100644 --- a/WebContent/base/OSRM.Map.js +++ b/WebContent/base/OSRM.Map.js @@ -89,13 +89,13 @@ click: function(e) { if( !OSRM.G.markers.hasSource() ) { var index = OSRM.G.markers.setSource( e.latlng ); OSRM.Geocoder.updateAddress( OSRM.C.SOURCE_LABEL, OSRM.C.DO_FALLBACK_TO_LAT_LNG ); - OSRM.G.markers.route[index].show(); - OSRM.Routing.getRoute(); + OSRM.G.markers.route[index].show(); + OSRM.Routing.getRoute( {recenter:OSRM.G.markers.route.length == 2} ); // allow recentering when the route is first shown } else if( !OSRM.G.markers.hasTarget() ) { var index = OSRM.G.markers.setTarget( e.latlng ); OSRM.Geocoder.updateAddress( OSRM.C.TARGET_LABEL, OSRM.C.DO_FALLBACK_TO_LAT_LNG ); OSRM.G.markers.route[index].show(); - OSRM.Routing.getRoute(); + OSRM.Routing.getRoute( {recenter:OSRM.G.markers.route.length == 2} ); // allow recentering when the route is first shown } }, geolocationResponse: function(response) { diff --git a/WebContent/routing/OSRM.Routing.js b/WebContent/routing/OSRM.Routing.js index b408a8df7..84166c2cc 100644 --- a/WebContent/routing/OSRM.Routing.js +++ b/WebContent/routing/OSRM.Routing.js @@ -54,7 +54,7 @@ timeoutRoute_Reversed: function() { OSRM.G.markers.reverseMarkers(); OSRM.Routing.timeoutRoute(); }, -showRoute: function(response) { +showRoute: function(response, parameters) { if(!response) return; @@ -71,8 +71,10 @@ 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); + if( parameters && parameters.recenter == true ) { // allow recentering when the route is first shown + var bounds = new L.LatLngBounds( OSRM.G.route._current_route.getPositions() ); + OSRM.G.map.setViewBoundsUI(bounds); + } }, showRoute_Dragging: function(response) { if(!response) @@ -109,7 +111,7 @@ showRoute_Redraw: function(response) { //-- main function -- //generate server calls to query routes -getRoute: function() { +getRoute: function(parameters) { // if source or target are not set -> hide route if( OSRM.G.markers.route.length < 2 ) { @@ -119,7 +121,7 @@ getRoute: function() { OSRM.JSONP.clear('dragging'); OSRM.JSONP.clear('redraw'); OSRM.JSONP.clear('route'); - OSRM.JSONP.call(OSRM.Routing._buildCall()+'&instructions=true', OSRM.Routing.showRoute, OSRM.Routing.timeoutRoute, OSRM.DEFAULTS.JSONP_TIMEOUT, 'route'); + OSRM.JSONP.call(OSRM.Routing._buildCall()+'&instructions=true', OSRM.Routing.showRoute, OSRM.Routing.timeoutRoute, OSRM.DEFAULTS.JSONP_TIMEOUT, 'route', parameters); }, getRoute_Reversed: function() { if( OSRM.G.markers.route.length < 2 )