I found a way to calculate the scale factor that the envelop I get back is
(vs. requested). Maybe this is a total kluge and there is a better way?
I added this to the above implementation:
MapViewport originalMapViewport =
mme.getSource().getMapContent().getViewport();
ReferencedEnvelope envNoScale =
mme.getEnvelopeByWorld(originalMapViewport.getBounds().getWidth());
double actualScale = envNoScale.getHeight() * scaleFactor /
originalMapViewport.getBounds().getHeight();
env.translate(delta.x * actualScale, delta.y * actualScale);
One thing I'm not certain about is why calculating the actualScale value
with getHeight works but getWidth doesn't?
Anyhow I hope to tidy this up and submit a pull request in the near future.
I'm going to propose that having a Google Maps style mouse wheel zoom as
the default in JMapFrame would be a good thing!
On Thu Nov 13 2014 at 11:35:00 AM Leif Gruenwoldt <lei...@gmail.com> wrote:
> 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