[OpenLayers-Users] Re: Handler.Box units
Puneet Kishor
punk.kish at gmail.com
Thu Nov 3 10:02:45 EDT 2011
On Nov 2, 2011, at 5:46 PM, Puneet Kishor wrote:
>
> On Nov 2, 2011, at 4:37 PM, Puneet Kishor wrote:
>
>> I am reusing the sample code from the Handler.Box documentation
>>
>> var control = new OpenLayers.Control();
>> OpenLayers.Util.extend(control, {
>> draw: function () {
>> // this Handler.Box will intercept the shift-mousedown
>> // before Control.MouseDefault gets to see it
>> this.box = new OpenLayers.Handler.Box(
>> control,
>> {done: this.notice},
>> {keyMask: OpenLayers.Handler.MOD_SHIFT}
>> );
>> this.box.activate();
>> },
>>
>> notice: function (bounds) {
>> OpenLayers.Console.userError("bounds: " + bounds);
>> var bbox = bounds.transform(proj.google, proj.latlng).toBBOX();
>> OpenLayers.Console.userError("bbox: " + bbox);
>> }
>> });
>>
>
> I am doing something wrong here trying to convert pixels to lat lng
>
> var bbox = bounds.toArray();
> var p1 = map.getLonLatFromPixel(bbox[0]);
> var str = "Lat: " + p1.lat + " (Pixel.x:" + p1.x + ") Lon: " + p1.lon + " (Pixel.y:" + p1.y + ")\n";
> alert(str);
>
> produces the following alert
>
> Lat: NaN (Pixel.x:undefined) Lon: NaN (Pixel.y:undefined)
>
>
The following is my solution
// 'p' is not really lon lat point even though it seems like one
// actually it is pixel in image space
var p = bounds.getCenterLonLat();
var x = p.lon, y = p.lat;
// 'pixel' is not really pixel, but is a point in base map coords
// which, in this case, is in spherical mercator since my base map
// is a Google map
var pixel = new OpenLayers.Pixel(x, y);
// finally, a real lat lng point
var ll = map.getLonLatFromPixel(pixel).transform(proj.google, proj.latlng);
The above works for me. It would, in my opinion, be useful to embellish the documentation with such information.
Many thanks,
--
Puneet Kishor
More information about the Users
mailing list