I'm trying to create a mouse wheel zoom behavior similar to Google Maps for
my JMapPanel. I started with the sample code that is documented here:

http://docs.geotools.org/stable/userguide/unsupported/swing/jmappane.html#actions

I want to improve that example such that the zoom focuses and follows the
mouse cursor position -- like Google Maps.

My implementation has an issue though with what seems to be a scaling
problem of the MapViewport. I request a specific Envelop width, but get a
different width. The behaviour I'm observing sounds exactly like what's
documented here:

http://docs.geotools.org/stable/userguide/unsupported/swing/jmappane.html#how-map-position-and-scale-are-calculated

Specifically I have a window of size 1280x720 pixels (minus the map layers
side panel) and I request a new Envelope of width scaled by 0.5, but then
get one that has been scaled only by ~0.8. How can I programatically
determine the scale I'm going to get when the envelope is passed to
MapPane#setDisplayArea? I need this value because I use it to scale how
much envelope needs to be translated to "follow" the the cursor. I don't
quite understand if or how the MapViewPort#getScreenToWorld or
getWorldToScreen AffineTransform's can help? The value I was getting from
the AffineTransform#scaleX didn't seem to line up with the 0.8 that I was
seeing in my particular case.

Please view my code below. Notice that I use a different scaleFactor for
translating the envelope. This is the problem, because it falls on it's
face as soon as I resize the window. Thoughts?

P.S. Once I get this sorted out I hope to turn it into a pull request so
everyone can benefit.

@Override
public void onMouseWheelMoved(MapMouseEvent mme) {
    int clicks = mme.getWheelAmount();
    double scaleFactor = (clicks < 0 ? 0.5 : 2); // half or double size

    DirectPosition2D mouseOriginalPixels = mme.getWorldPos();
    ReferencedEnvelope origEnv = mapFrame.getMapPane().getDisplayArea();
    DirectPosition2D centerOriginal = new
DirectPosition2D(origEnv.getMedian(0), origEnv.getMedian(1));
    DirectPosition2D delta = new DirectPosition2D(centerOriginal.x -
mouseOriginalPixels.x, centerOriginal.y - mouseOriginalPixels.y);

    ReferencedEnvelope env =
mme.getEnvelopeByWorld(mme.getSource().getDisplayArea().getSpan(0) *
scaleFactor);
    scaleFactor = clicks < 0 ? 0.804 : 3.219; // FIXME this hack only works
until we resize the window!
    env.translate(delta.x * scaleFactor, delta.y * scaleFactor);
    mapFrame.getMapPane().setDisplayArea(env);
}
------------------------------------------------------------------------------
Comprehensive Server Monitoring with Site24x7.
Monitor 10 servers for $9/Month.
Get alerted through email, SMS, voice calls or mobile push notifications.
Take corrective actions from your mobile device.
http://pubads.g.doubleclick.net/gampad/clk?id=154624111&iu=/4140/ostg.clktrk
_______________________________________________
GeoTools-GT2-Users mailing list
GeoTools-GT2-Users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users

Reply via email to