[ka-Map-users] GeoCoordinates to Pixel position

Wei Yang, Ng - Wayne wayne at datalinktech.com.au
Mon May 15 00:22:45 EDT 2006


Hi,

    Thanks for mentioning the scale change event, that will come in 
handy. The following is the code I use for moving to the correct map 
position. The function accepts lat/longs and translates them to pixel 
coordinates.

function moveToGeoCoord( x, y) {
    var pixPos = myKaMap.geoToPix(x,y);

    var nPixPos = myKaMap.currentTool.adjustPixPosition( pixPos[0]*(-1), 
pixPos[1]*(-1) );

    var posX = nPixPos[0];
    var posY = nPixPos[1];

    var aPixPos = myFixedOverlay.adjustPixPosition( posX, posY);
   
    var vpX = myFixedOverlay.kaMap.viewportWidth/2;
    var vpY = myFixedOverlay.kaMap.viewportHeight/2;

    var dx = parseInt(myFixedOverlay.kaMap.theInsideLayer.style.left) - 
myFixedOverlay.kaMap.xOrigin
- vpX - aPixPos[0];
    var dy = parseInt(myFixedOverlay.kaMap.theInsideLayer.style.top) - 
myFixedOverlay.kaMap.yOrigin -
 vpY - aPixPos[1];
   
    //Slide to the point if it is near, else jump to it
    if ((-dx < 2000 && -dy < 2000) && (dx < 2000 && dy < 2000))
        myFixedOverlay.kaMap.slideBy(-dx, -dy);
    else
        myFixedOverlay.kaMap.moveBy(-dx, -dy);
}

Cheers,
Wayne

Paul Spencer wrote:
> Hi,
>
> this is tricky math :)  The correct pixel position needs to be 
> calculated in a number of steps depending on how you want to use the 
> pixel position.
>
> The math for taking a geographic position and calculating its pixel 
> position is handled starting with addObjectGeo ... you can trace the 
> calls from there.
>
> The simple calculation is to take the geographic coordinates and 
> multiply by the cellsize (geographic coordinates per pixel).
>
> var px = gx * this.cellSize;
> var py = gy * this.cellSize; //or myKaMap.cellSize
>
> now you have to convert from geographic space to browser space because 
> in the real world up is positive and in browser space, down is positive
>
> py = -1 * py;
>
> now you have to convert to viewport coordinates, which are mirrored on 
> x/y to get the image to show up in the viewport:
>
> px = -1*px;
> py = -1*py;
>
> now you have to adjust for a variable origin
>
> px = px - this.xOrigin;
> py = py - this.yOrigin;  // or maybe it is +?
>
> px and py are now relative to the top, left of the viewport element.  
> If you want page-relative coordinates, you need to find the top/left 
> of the viewport and add px/py.
>
> I may not have this exactly right, whenever I need to do something 
> like this I usually end up spending time in the debugger tracing the 
> values to figure out exactly which combination I need :(
>
> The pixel coordinates are going to be different at every zoom scale 
> because the origin and cell size changes.
>
> You can get notified of changes in the scale by registering for 
> KAMAP_SCALE_CHANGED event.
>
> Cheers
>
> Paul
>
> PS not sure why you want to do this.  kaMap already includes functions 
> for doing most of this in the other direction.  Is there a need for 
> the reverse?
>
> On 11-May-06, at 7:52 PM, Wei Yang, Ng - Wayne wrote:
>
>> Hi Ramesh,
>>     I am doing similar stuff and I am having the same problem. (The 
>> map slides to anywhere else but the point that I want)A possible 
>> solution
>> (not tested) is to create a javascript function that kicks in when 
>> the zoom level changes. (a few events trigger a zoom level change eg. 
>> mouse scroll, icon click, KB +/-)
>> The function finds out(a set variable perhaps) if the map is supposed 
>> to zoom to a certain geographic coordinate and reinvokes the 
>> function(supplying lat/long of course) that moves/slides to it. This 
>> idea is based on the observation that while the map is sliding, I am 
>> able to interrupt the action by dbl clicking anywhere on the map.
>>
>> Cheers,
>> Wayne
>>> Hi,
>>>
>>> I am trying to figure out the correct pixel position
>>> for a given geo-coordinates in all zoom levels.
>>>
>>> I looked at the code, and it looks like the following
>>> sequence
>>> p = kaMap.geoToPix(geox,geoy) newxy = 
>>> kaMap.currentTool.adjustPixPosition(p[0]*-1,
>>> p[1]*-1)
>>>
>>> seem to work fine if the zoom level is not changed but
>>> only extents are changed.
>>>
>>> How do I calculate the correct pixel position when
>>> zoom level(or scales) change?
>>>
>>> Thanks
>>> Ramesh
>> _______________________________________________
>> ka-Map-users mailing list
>> ka-Map-users at lists.maptools.org
>> http://lists.maptools.org/mailman/listinfo/ka-map-users
>
> +-----------------------------------------------------------------+
> |Paul Spencer                           pspencer at dmsolutions.ca   |
> +-----------------------------------------------------------------+
> |Applications & Software Development                              |
> |DM Solutions Group Inc                 http://www.dmsolutions.ca/|
> +-----------------------------------------------------------------+
>
>
>
>


More information about the ka-Map-users mailing list