- removed bug in centering routes when UI was hidden

- moved all browser dependencies to OSRM.Browser
- refactored via code to use Leaflet functions
- added function to extract route layerPoints
This commit is contained in:
DennisSchiefer 2012-03-28 10:51:54 +01:00
parent 251a43980a
commit 342a0d22bd
6 changed files with 57 additions and 81 deletions

View File

@ -15,10 +15,11 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
or see http://www.gnu.org/licenses/agpl.txt.
*/
// OSRM browser detection
// [simple detection routines to respect some browser peculiarities]
// OSRM old browser support
// [simple browser detection and routines to support some old browsers]
// browser detection (runs anonymous function to prevent local variables cluttering global namespace)
(function() {
var useragent = navigator.userAgent;
@ -28,4 +29,14 @@ or see http://www.gnu.org/licenses/agpl.txt.
};
}());
// (runs anonymous function to prevent local variables cluttering global namespace)
// compatibility tools for old browsers
function getElementsByClassName(node, classname) {
var a = [];
var re = new RegExp('(^| )'+classname+'( |$)');
var els = node.getElementsByTagName("*");
for(var i=0,j=els.length; i<j; i++)
if(re.test(els[i].className))a.push(els[i]);
return a;
}
document.head = document.head || document.getElementsByTagName('head')[0];

View File

@ -17,7 +17,6 @@ or see http://www.gnu.org/licenses/agpl.txt.
// OSRM geocoding routines
// [geocoder query, management and display of geocoder results]
// [TODO: better separation of GUI and geocoding routines, reverse geocoding]
// some constants
OSRM.CONSTANTS.SOURCE_LABEL = "source";

View File

@ -42,9 +42,12 @@ hide: function() {
isShown: function() {
return this.shown;
},
getPoints: function() {
return this.route._originalPoints;
},
getPositions: function() {
return this.route.getLatLngs();
},
},
setPositions: function(positions) {
this.route.setLatLngs( positions );
},
@ -54,25 +57,25 @@ setStyle: function(style) {
centerView: function() {
var bounds = new L.LatLngBounds( this.getPositions() );
if( OSRM.GUI.visible == true ) {
var southwest = bounds.getSouthWest();
var northeast = bounds.getNorthEast();
var zoom = OSRM.G.map.getBoundsZoom(bounds);
var sw_point = OSRM.G.map.project( southwest, zoom);
var southwest = bounds.getSouthWest();
var northeast = bounds.getNorthEast();
var zoom = OSRM.G.map.getBoundsZoom(bounds);
var sw_point = OSRM.G.map.project( southwest, zoom);
if( OSRM.GUI.visible == true )
sw_point.x-=OSRM.GUI.width/2;
sw_point.y+=10;
var ne_point = OSRM.G.map.project( northeast, zoom);
ne_point.y-=10;
sw_point.x+=10;
bounds.extend( OSRM.G.map.unproject(sw_point,zoom) );
bounds.extend( OSRM.G.map.unproject(ne_point,zoom) );
OSRM.G.map.fitBounds( bounds );
}
else
sw_point.x-=10;
sw_point.y+=10;
var ne_point = OSRM.G.map.project( northeast, zoom);
ne_point.y-=10;
sw_point.x+=10;
bounds.extend( OSRM.G.map.unproject(sw_point,zoom) );
bounds.extend( OSRM.G.map.unproject(ne_point,zoom) );
OSRM.G.map.fitBounds( bounds );
},
onClick: function(e) {
if(OSRM.G.route.isRoute())
findViaPosition( e.latlng );
OSRM.Via.findViaPosition( e.latlng );
},
toString: function() {
return "OSRM.Route("+ this.label + ", " + this.route.getLatLngs().length + " points)";
@ -199,6 +202,9 @@ OSRM.extend( OSRM.Route,{
getPositions: function() {
return this._current_route.getPositions();
},
getPoints: function() {
return this._current_route.getPoints();
},
fire: function(type,event) {
this._current_route.route.fire(type,event);
},

View File

@ -15,38 +15,44 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
or see http://www.gnu.org/licenses/agpl.txt.
*/
// OSRM via marker routines
// [find correct position for a via marker]
// store location of via points returned by server
OSRM.GLOBALS.via_points = null;
OSRM.Via = {
// find route segment of current route geometry that is closest to the new via node (marked by index of its endpoint)
function findNearestRouteSegment( new_via ) {
_findNearestRouteSegment: function( new_via ) {
var min_dist = Number.MAX_VALUE;
var min_index = undefined;
var positions = OSRM.G.route.getPositions();
for(var i=0; i<positions.length-1; i++) {
var dist = dotLineLength( new_via.lng, new_via.lat, positions[i].lng, positions[i].lat, positions[i+1].lng, positions[i+1].lat, true);
if( dist < min_dist) {
min_dist = dist;
min_index = i+1;
var p = OSRM.G.map.latLngToLayerPoint( new_via );
var positions = OSRM.G.route.getPoints();
for(var i=1; i<positions.length; i++) {
var _sqDist = L.LineUtil._sqClosestPointOnSegment(p, positions[i-1], positions[i], true);
if( _sqDist < min_dist) {
min_dist = _sqDist;
min_index = i;
}
}
return min_index;
}
},
// find the correct index among all via nodes to insert the new via node, and insert it
function findViaPosition( new_via_position ) {
findViaPosition: function( new_via_position ) {
// find route segment that is closest to click position (marked by last index)
var nearest_index = findNearestRouteSegment( new_via_position );
var nearest_index = OSRM.Via._findNearestRouteSegment( new_via_position );
// find correct index to insert new via node
var new_via_index = OSRM.G.via_points.length;
var via_index = Array();
for(var i=0; i<OSRM.G.via_points.length; i++) {
via_index[i] = findNearestRouteSegment( new L.LatLng(OSRM.G.via_points[i][0], OSRM.G.via_points[i][1]) );
via_index[i] = OSRM.Via._findNearestRouteSegment( new L.LatLng(OSRM.G.via_points[i][0], OSRM.G.via_points[i][1]) );
if(via_index[i] > nearest_index) {
new_via_index = i;
break;
@ -60,4 +66,6 @@ function findViaPosition( new_via_position ) {
getRoute(OSRM.C.FULL_DESCRIPTION);
return new_via_index;
}
}
};

View File

@ -57,7 +57,7 @@ or see http://www.gnu.org/licenses/agpl.txt.
<script src="main.js" type="text/javascript"></script>
<script src="routing.js" type="text/javascript"></script>
<script src="via.js" type="text/javascript"></script>
<script src="OSRM.Via.js" type="text/javascript"></script>
<script src="utils.js" type="text/javascript"></script>
</head>

View File

@ -15,19 +15,6 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
or see http://www.gnu.org/licenses/agpl.txt.
*/
// compatibility tools for old browsers
function getElementsByClassName(node, classname) {
var a = [];
var re = new RegExp('(^| )'+classname+'( |$)');
var els = node.getElementsByTagName("*");
for(var i=0,j=els.length; i<j; i++)
if(re.test(els[i].className))a.push(els[i]);
return a;
}
document.head = document.head || document.getElementsByTagName('head')[0];
// ------------------------------------------------------
// human readable time
function secondsToTime(seconds){
seconds = parseInt(seconds);
@ -68,38 +55,3 @@ function isLongitude(value) {
else
return false;
}
// ------------------------------------------------------
// distance between two points
function distanceBetweenPoint(x1, y1, x2, y2) {
var a = x1 - x2;
var b = y1 - y2;
return Math.sqrt(a*a + b*b);
}
// distance from a point to a line or segment
// (x,y) point
// (x0,y0) line point A
// (x1,y1) line point B
// o specifies if the distance should respect the limits of the segment (overLine = true)
// or if it should consider the segment as an infinite line (overLine = false);
// if false returns the distance from the point to the line,
// otherwise the distance from the point to the segment
function dotLineLength(x, y, x0, y0, x1, y1, o){
function lineLength(x, y, x0, y0){return Math.sqrt((x -= x0) * x + (y -= y0) * y);}
if(o && !(o = function(x, y, x0, y0, x1, y1){
if(!(x1 - x0)) return {x: x0, y: y};
else if(!(y1 - y0)) return {x: x, y: y0};
var left, tg = -1 / ((y1 - y0) / (x1 - x0));
return {x: left = (x1 * (x * tg - y + y0) + x0 * (x * - tg + y - y1)) / (tg * (x1 - x0) + y0 - y1), y: tg * left - tg * x + y};
}(x, y, x0, y0, x1, y1) && o.x >= Math.min(x0, x1) && o.x <= Math.max(x0, x1) && o.y >= Math.min(y0, y1) && o.y <= Math.max(y0, y1))){
var l1 = lineLength(x, y, x0, y0), l2 = lineLength(x, y, x1, y1);
return l1 > l2 ? l2 : l1;
}
else {
var a = y0 - y1, b = x1 - x0, c = x0 * y1 - y0 * x1;
return Math.abs(a * x + b * y + c) / Math.sqrt(a * a + b * b);
}
};