| 68 | | // printMap() assumes that an iframe named 'map' contains a Map object named 'map' |
| 69 | | // It should be very easy to change those first two lines to accommodate your situation. |
| 70 | | function printMap() { |
| 71 | | var mapview = document.getElementById('map').contentWindow.map; |
| 72 | | var printurl = 'print.php'; |
| 73 | | var size = mapview.getSize(); |
| | 68 | // this assumes that the Map object is a JavaScript variable named "map" |
| | 69 | // changed 20090928: |
| | 70 | // * fixed the weird references about iframes, since they're not the usual use case |
| | 71 | // * added the Please Wait window |
| | 72 | // * used newer-style OpenLayers.Request.POST() for final AJAX call |
| | 73 | var print_wait_win = null; |
| | 74 | function PrintMap() { |
| | 75 | //-- post a wait message |
| | 76 | print_wait_win = window.open("pleasewait.html", "print_wait_win", "scrollbars=no, status=0, height=5, width=10, resizable=1"); |
| | 77 | |
| 77 | | for (layername in mapview.layers) { |
| 78 | | // if the layer isn't visible at this range, or is turned off, skip it |
| 79 | | var layer = mapview.layers[layername]; |
| 80 | | if (!layer.getVisibility()) continue; |
| 81 | | if (!layer.calculateInRange()) continue; |
| 82 | | // iterate through their grid's tiles, collecting each tile's extent and pixel location at this moment |
| 83 | | for (tilerow in layer.grid) { |
| 84 | | for (tilei in layer.grid[tilerow]) { |
| 85 | | var tile = layer.grid[tilerow][tilei] |
| 86 | | var url = layer.getURL(tile.bounds); |
| 87 | | var position = tile.position; |
| 88 | | var opacity = layer.opacity ? parseInt(100*layer.opacity) : 100; |
| 89 | | tiles[tiles.length] = {url:url, x:position.x, y:position.y, opacity:opacity}; |
| 90 | | } |
| 91 | | } |
| | 82 | for (layername in map.layers) { |
| | 83 | // if the layer isn't visible at this range, or is turned off, skip it |
| | 84 | var layer = map.layers[layername]; |
| | 85 | if (!layer.getVisibility()) continue; |
| | 86 | if (!layer.calculateInRange()) continue; |
| | 87 | // iterate through their grid's tiles, collecting each tile's extent and pixel location at this moment |
| | 88 | for (tilerow in layer.grid) { |
| | 89 | for (tilei in layer.grid[tilerow]) { |
| | 90 | var tile = layer.grid[tilerow][tilei] |
| | 91 | var url = layer.getURL(tile.bounds); |
| | 92 | var position = tile.position; |
| | 93 | var opacity = layer.opacity ? parseInt(100*layer.opacity) : 100; |
| | 94 | tiles[tiles.length] = {url:url, x:position.x, y:position.y, opacity:opacity}; |
| | 95 | } |
| | 96 | } |
| 96 | | new OpenLayers.Ajax.Request(printurl, {method:'post', parameters:printparams, onComplete:printDone }); |
| 97 | | // if using OpenLayers 2.7 or later use this request (printDone function not necessary): |
| 98 | | // OpenLayers.Request.POST({url:printurl, data:OpenLayers.Util.getParameterString({width:size.w, |
| 99 | | // height:size.h, tiles:tiles_json}), headers:{"Content-Type":"application/x-www-form-urlencoded"}, |
| 100 | | // callback: function(request) {window.open(request.responseText);}}); |
| 101 | | } |
| 102 | | |
| 103 | | // when the request is ready (the image has been generated) this callback expects the output |
| 104 | | // to have been simply an URL indicating where to find the generated document. |
| 105 | | function printDone(request) { |
| 106 | | var url = request.responseText; |
| 107 | | window.open(url); |
| 108 | | } |
| | 102 | OpenLayers.Request.POST( |
| | 103 | { url:'lib/print.php', |
| | 104 | data:OpenLayers.Util.getParameterString({width:size.w,height:size.h,tiles:tiles_json}), |
| | 105 | headers:{'Content-Type':'application/x-www-form-urlencoded'}, |
| | 106 | callback: function(request) { |
| | 107 | print_wait_win.close(); |
| | 108 | window.open(request.responseText); |
| | 109 | } |
| | 110 | } |
| | 111 | ); |
| | 112 | } |