adding getPixelFromLonLat() function to Map.js -- the code is lifted from OpenLayers.Layer.Marker's drawMarker() function. also test added to ensure both translation functions return valid objects and that going back and forth is consistent. Note that i only test pixel-lonlat-pixel, because going from lonlat-pixel-lonlat there is loss due to the rounding in the lonlat-pixel phase
git-svn-id: http://svn.openlayers.org/trunk/openlayers@246 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
@@ -271,6 +271,23 @@ OpenLayers.Map.prototype = {
|
|||||||
center.lat - delta_y * res);
|
center.lat - delta_y * res);
|
||||||
},
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {OpenLayers.LonLat} lonlat
|
||||||
|
*
|
||||||
|
* @returns An OpenLayers.Pixel which is the passed-in OpenLayers.LonLat,
|
||||||
|
* translated into screen pixels given the current extent
|
||||||
|
* and resolution
|
||||||
|
* @type OpenLayers.Pixel
|
||||||
|
*/
|
||||||
|
getPixelFromLonLat: function (lonlat) {
|
||||||
|
var resolution = this.getResolution();
|
||||||
|
var extent = this.getExtent();
|
||||||
|
return new OpenLayers.Pixel(
|
||||||
|
Math.round(1/resolution * (lonlat.lon - extent.left)),
|
||||||
|
Math.round(1/resolution * (extent.top - lonlat.lat))
|
||||||
|
);
|
||||||
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {OpenLayers.LonLat} lonlat
|
* @param {OpenLayers.LonLat} lonlat
|
||||||
* @param {int} zoom
|
* @param {int} zoom
|
||||||
|
|||||||
@@ -113,6 +113,25 @@
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function test_08_Map_px_lonlat_translation (t) {
|
||||||
|
t.plan( 3 );
|
||||||
|
map = new OpenLayers.Map($('map'));
|
||||||
|
map.setCenter(new OpenLayers.LonLat(0, 0), 0);
|
||||||
|
|
||||||
|
var pixel = new OpenLayers.Pixel(50,150);
|
||||||
|
var lonlat = map.getLonLatFromPixel(pixel);
|
||||||
|
t.ok( lonlat instanceof OpenLayers.LonLat, "getLonLatFromPixel returns valid OpenLayers.LonLat" );
|
||||||
|
|
||||||
|
var newPixel = map.getPixelFromLonLat(lonlat);
|
||||||
|
t.ok( newPixel instanceof OpenLayers.Pixel, "getPixelFromLonLat returns valid OpenLayers.Pixel" );
|
||||||
|
|
||||||
|
// WARNING!!! I'm faily sure that the following test's validity
|
||||||
|
// depends highly on rounding and the resolution. For now,
|
||||||
|
// in the default case, it seems to work. This may not
|
||||||
|
// always be so.
|
||||||
|
t.ok( newPixel.equals(pixel), "Translation to pixel and back to lonlat is consistent");
|
||||||
|
}
|
||||||
|
|
||||||
function test_99_Map_destroy (t) {
|
function test_99_Map_destroy (t) {
|
||||||
t.plan( 2 );
|
t.plan( 2 );
|
||||||
map = new OpenLayers.Map($('map'));
|
map = new OpenLayers.Map($('map'));
|
||||||
|
|||||||
Reference in New Issue
Block a user