printing support finalized (yeah!)

This commit is contained in:
DennisSchiefer 2012-04-26 12:49:30 +01:00
parent a0ceee732e
commit a8a4f56292
3 changed files with 77 additions and 33 deletions

View File

@ -34,7 +34,7 @@ init: function() {
input_mask_header.appendChild(spacer,input_mask_header.lastChild);
input_mask_header.appendChild(icon,input_mask_header.lastChild);
document.getElementById("gui-printer-inactive").onclick = OSRM.Printing.print;
document.getElementById("gui-printer-inactive").onclick = OSRM.Printing.openPrintWindow;
},
@ -89,7 +89,7 @@ show: function(response) {
'<div class="quad"></div>' +
'</td></tr></thead>';
// create route description
var body = '<tbody class="description-body">';
for(var i=0; i < response.route_instructions.length; i++){
@ -121,41 +121,34 @@ show: function(response) {
body += '</tbody>';
// put everything in DOM
OSRM.G.printwindow.document.getElementById('description').innerHTML = '<table class="description medium-font">' + header + body + '</table>';
OSRM.G.printwindow.document.getElementById('overview-map-description').innerHTML = '<table class="description medium-font">' + header + '</table>';
var print_window = OSRM.G.printwindow;
print_window.document.getElementById('description').innerHTML = '<table class="description medium-font">' + header + body + '</table>';
print_window.document.getElementById('overview-map-description').innerHTML = '<table class="description medium-font">' + header + '</table>';
// draw map
var positions = OSRM.G.route.getPositions();
var tile_server_id = OSRM.G.map.getActiveLayerId();
var map = OSRM.G.printwindow.initialize( OSRM.DEFAULTS.TILE_SERVERS[tile_server_id] );
var zoom = print_window.drawMap( OSRM.DEFAULTS.TILE_SERVERS[tile_server_id], new L.LatLngBounds( positions ) );
// draw markers
var markers = OSRM.G.markers.route;
map.addLayer( new L.MouseMarker( markers[0].getPosition(), {draggable:false,clickable:false,icon:OSRM.G.icons['marker-source']} ) );
for(var i=1, size=markers.length-1; i<size; i++)
map.addLayer( new L.MouseMarker( markers[i].getPosition(), {draggable:false,clickable:false,icon:OSRM.G.icons['marker-via']} ) );
map.addLayer( new L.MouseMarker( markers[markers.length-1].getPosition(), {draggable:false,clickable:false,icon:OSRM.G.icons['marker-target']} ));
// draw route
OSRM.Printing.route = new L.DashedPolyline();
var route = OSRM.Printing.route;
route.setLatLngs( OSRM.G.route.getPositions() );
route.setStyle( {dashed:false,clickable:false,color:'#0033FF', weight:5} );
map.addLayer( route );
var bounds = new L.LatLngBounds( OSRM.G.route.getPositions() );
map.fitBoundsUI( bounds );
// query for a better route geometry
var zoom = map.getBoundsZoom(bounds);
print_window.prefetchIcons( OSRM.G.images );
print_window.drawMarkers( OSRM.G.markers.route );
// draw route & query for better geometry
print_window.drawRoute( positions );
OSRM.JSONP.call(OSRM.Routing._buildCall()+'&z='+zoom+'&instructions=false', OSRM.Printing.drawRoute, OSRM.Printing.timeoutRoute, OSRM.DEFAULTS.JSONP_TIMEOUT, 'print');
},
timeoutRoute: function() {},
drawRoute: function(response) {
if(!response)
return;
var geometry = OSRM.RoutingGeometry._decode(response.route_geometry, 5);
OSRM.Printing.route.setLatLngs( geometry );
var positions = OSRM.RoutingGeometry._decode(response.route_geometry, 5);
OSRM.G.printwindow.drawRoute( positions );
},
//open printWindow
print: function() {
// opens the print window and closes old instances
openPrintWindow: function() {
// do not open window if there is no route to draw
if( !OSRM.G.route.isRoute() || !OSRM.G.route.isShown() )
return;
@ -166,12 +159,11 @@ print: function() {
// generate a new window and wait till it has finished loading
OSRM.G.printwindow = window.open("printing/printing.html","","width=540,height=500,left=100,top=100,dependent=yes,location=no,menubar=no,scrollbars=yes,status=no,toolbar=no,resizable=yes");
OSRM.Browser.onLoadHandler( OSRM.Printing.printwindowLoaded, OSRM.G.printwindow );
},
//add content to printwindow after it has finished loading
printwindowLoaded: function(){
// add content to printwindow after it has finished loading
printWindowLoaded: function(){
var print_window = OSRM.G.printwindow;
var print_document = print_window.document;

View File

@ -37,6 +37,9 @@ or see http://www.gnu.org/licenses/agpl.txt.
<!-- scripts -->
<script src="printing.js" type="text/javascript"></script>
<script src="../leaflet/leaflet-src.js" type="text/javascript"></script>
<script src="../base/leaflet/L.DashedPolyline.js" type="text/javascript"></script>
<script src="../base/leaflet/L.MouseMarker.js" type="text/javascript"></script>
<script src="../base/leaflet/L.SwitchableIcon.js" type="text/javascript"></script>
<script src="../base/osrm/OSRM.MapView.js" type="text/javascript"></script>
</head>

View File

@ -23,8 +23,35 @@ OSRM.GLOBALS = {};
OSRM.GUI = { visible:false };
OSRM.G = OSRM.GLOBALS;
//print the window
function printWindow() {
window.print();
}
//prefetch icons
OSRM.GLOBALS.icons = {};
prefetchIcons = function(images_list) {
var icon_list = [ {id:'marker-source', image_id:'marker-source'},
{id:'marker-target', image_id:'marker-target'},
{id:'marker-via', image_id:'marker-via'},
{id:'marker-highlight', image_id:'marker-highlight'}
];
for(var i=0; i<icon_list.length; i++) {
var icon = {
iconUrl: "../"+images_list[icon_list[i].image_id].getAttribute("src"), iconSize: new L.Point(25, 41), iconAnchor: new L.Point(13, 41),
shadowUrl: "../"+images_list["marker-shadow"].getAttribute("src"), shadowSize: new L.Point(41, 41),
popupAnchor: new L.Point(0, -33)
};
OSRM.G.icons[icon_list[i].id] = new L.SwitchableIcon(icon);
}
};
// function to initialize a map in the new window
function initialize(tile_server) {
function drawMap(tile_server, bounds) {
// setup map
var tile_layer = new L.TileLayer(tile_server.url, tile_server.options);
OSRM.G.map = new OSRM.MapView("overview-map", {
@ -40,10 +67,32 @@ function initialize(tile_server) {
touchZoom:false,
doubleClickZoom:false
});
return OSRM.G.map;
OSRM.G.map.fitBoundsUI(bounds);
return OSRM.G.map.getBoundsZoom(bounds);
}
//print the window
function printWindow() {
window.print();
}
// manage makers
function drawMarkers( markers ) {
OSRM.G.map.addLayer( new L.MouseMarker( markers[0].getPosition(), {draggable:false,clickable:false,icon:OSRM.G.icons['marker-source']} ) );
for(var i=1, size=markers.length-1; i<size; i++)
OSRM.G.map.addLayer( new L.MouseMarker( markers[i].getPosition(), {draggable:false,clickable:false,icon:OSRM.G.icons['marker-via']} ) );
OSRM.G.map.addLayer( new L.MouseMarker( markers[markers.length-1].getPosition(), {draggable:false,clickable:false,icon:OSRM.G.icons['marker-target']} ) );
}
// manage route
function drawRoute( positions ) {
OSRM.G.route = new L.DashedPolyline();
OSRM.G.route.setLatLngs( positions );
OSRM.G.route.setStyle( {dashed:false,clickable:false,color:'#0033FF', weight:5} );
OSRM.G.map.addLayer( OSRM.G.route );
}
function updateRoute( positions ) {
OSRM.G.route.setLatLngs( positions );
}
// start populating the window when it is fully loaded
window.opener.OSRM.Browser.onLoadHandler( window.opener.OSRM.Printing.printWindowLoaded, window );