views:

357

answers:

1

I’m downloading maps using Static Maps V2 API then display map on my mobile device, now I would like to move map using my finger and a touch screen on my device.

How can I calculate a new center for moved map, knowing previous map center, zoom level and how many pixels map has been moved?

+2  A: 

The following code should do the trick; Call adjust***ByPixels(), with delta being the offset in pixels.

   static final double GOOGLEOFFSET = 268435456;
   static final double GOOGLEOFFSET_RADIUS = GOOGLEOFFSET / Math.PI;
   static final double MATHPI_180 = Math.PI/180;

   static private final double preLonToX1 = GOOGLEOFFSET_RADIUS * (Math.PI/180);

   public final static double LonToX( double lon ) {
     return Math.round(GOOGLEOFFSET + preLonToX1 * lon);
   }

   public final static double LatToY( double lat ) {
     return Math.round( GOOGLEOFFSET - GOOGLEOFFSET_RADIUS * Math.log((1 + Math.sin(lat * MATHPI_180)) / (1 - Math.sin(lat * MATHPI_180))) / 2);
   }

   public final static double XToLon( double x) {
     return ((Math.round(x) - GOOGLEOFFSET) / GOOGLEOFFSET_RADIUS) * 180/ Math.PI;
   }

   public final static double YToLat( double y) {
     return (Math.PI / 2 - 2 * Math.atan(Math.exp((Math.round(y) - GOOGLEOFFSET) / GOOGLEOFFSET_RADIUS))) * 180 / Math.PI;
   }

   public final static double adjustLonByPixels( double lon, int delta, int zoom) {
     return XToLon(LonToX(lon) + (delta << (21 - zoom)));
   }

   public final static double adjustLatByPixels( double lat,  int delta, int zoom) {
     return YToLat(LatToY(lat) + (delta << (21 - zoom)));
    }
Waverick