[OpenLayers-Users] How to print map area with openlayers
guillaumev
guillaume at viguierjust.com
Mon May 3 17:58:41 EDT 2010
Hi,
Just in case someone encounters the same problem (i. e. when moving the map,
it is printed in its initial position), I solved it by adding the position
of the viewport (map.layerContainerDiv.style.left and
map.layerContainerDiv.style.top), and then adding the left value of the
viewport to the $tile->x and the top value of the viewport to the $tile->y
in the calls to imagecopymerge_alpha.
Here is my full client code (I encapsulated the map object within an
object):
this.print = function(print_url, callback) {
var size = this.map.getSize();
var tiles = [];
var nb_layers = this.map.layers.length;
var layeri = 0;
var nb_vector_layers = 0;
var features = [];
for (layeri = 0; layeri < nb_layers; layeri++) {
// if the layer isn't visible at this range, or is turned off, skip it
var layer = this.map.layers[layeri];
if (!layer.getVisibility()) continue;
if (!layer.calculateInRange()) continue;
if (layer.grid) {
// iterate through their grid's tiles, collecting each tile's extent and
pixel location at this moment
var grid_length = layer.grid.length;
var row_length = 0;
var tilerow = 0;
var tilei = 0;
for (tilerow = 0; tilerow < grid_length; tilerow++) {
row_length = layer.grid[tilerow].length;
tilei = 0;
for (tilei = 0; tilei < row_length; tilei++) {
var tile = layer.grid[tilerow][tilei];
var url = layer.getURL(tile.bounds);
var position = tile.position;
var opacity = layer.opacity ? parseInt(100*layer.opacity) : 100;
var bounds = tile.bounds;
tiles[tiles.length] = {url:url, x:position.x, y:position.y,
opacity:opacity, bounds:{left:bounds.left, right: bounds.right, top:
bounds.top, bottom: bounds.bottom}};
}
}
} else {
// get the features of the layer
var olFeatures = layer.features;
var features_temp = [];
var styles = {};
var nb_features = layer.features.length;
var featuresi = 0;
var nextId = 1;
for (var i = 0; i < nb_features; i++) {
var feature = olFeatures[i];
var style = feature.style || layer.style ||
layer.styleMap.createSymbolizer(feature, feature.renderIntent);
if(feature.geometry) {
if (feature.geometry.CLASS_NAME.search(/point$/i) >= 0) {
var fpos = this.map.getLayerPxFromLonLat(new
OpenLayers.LonLat(feature.geometry.x, feature.geometry.y));
if(fpos != null) {
features_temp[featuresi] = {type: 'point', x: fpos.x, y:fpos.y,
style: style};
featuresi++;
}
}
}
}
features[nb_vector_layers] = features_temp;
nb_vector_layers++;
}
}
// hand off the list to our server-side script, which will do the heavy
lifting
var tiles_json = JSON.stringify(tiles);
var features_json = JSON.stringify(features);
var viewport_left = parseInt(this.map.layerContainerDiv.style.left);
var viewport_top = parseInt(this.map.layerContainerDiv.style.top);
var viewport = {top: viewport_top, left: viewport_left};
var viewport_json = JSON.stringify(viewport);
var scale = Math.round(this.map.getScale());
OpenLayers.Request.POST(
{ url:print_url,
data:OpenLayers.Util.getParameterString({width:size.w,height:size.h,scale:scale,viewport:
viewport_json,tiles:tiles_json,features:features_json}),
headers:{'Content-Type':'application/x-www-form-urlencoded'},
callback: callback
}
);
}
--
View this message in context: http://osgeo-org.1803224.n2.nabble.com/How-to-print-map-area-with-openlayers-tp4901023p5000414.html
Sent from the OpenLayers Users mailing list archive at Nabble.com.
More information about the Users
mailing list