Skip to content

Instantly share code, notes, and snippets.

@codebutler
Last active April 19, 2016 02:54
Show Gist options
  • Save codebutler/6925097 to your computer and use it in GitHub Desktop.
Save codebutler/6925097 to your computer and use it in GitHub Desktop.
Code to offset Android Google Map positions in pixels.
// MapAdjust.java
// Based on adjust.py
// Gatubit <gatubit@gmail.com>
// https://gist.github.com/astrocosa/724526
// Based on adjust.js
// Bratliff <bratliff@umich.edu>
// http://www.polyarc.us/adjust.js
import com.google.android.gms.maps.model.LatLng;
public abstract class MapAdjust {
private static final double OFFSET = 268435456;
private static final double RADIUS = OFFSET / Math.PI;
public static LatLng xyToLL(int x, int y, LatLng latLng, int z) {
return xyToLL(x, y, latLng.longitude, latLng.latitude, z);
}
public static LatLng xyToLL(int x, int y, double lon, double lat, int z) {
double resultX = xToLon(
lonToX(lon) + (
x << (21 - z)
)
);
double resultY = yToLat(
latToY(lat) + (
y << (21 - z)
)
);
return new LatLng(resultY, resultX);
}
private static long lonToX(double lon) {
return Math.round(OFFSET + RADIUS * Math.toRadians(lon));
}
private static long latToY(double lat) {
return Math.round(
OFFSET - RADIUS * (Math.log(
(1 + Math.sin(Math.toRadians(lat)))
/
(1 - Math.sin(Math.toRadians(lat)))
)) / 2
);
}
private static double xToLon(double x) {
return Math.toDegrees(
(Math.round(x) - OFFSET) / RADIUS
);
}
private static double yToLat(double y) {
return Math.toDegrees(
Math.PI / 2 - 2 * Math.atan(
Math.exp((Math.round(y) - OFFSET) / RADIUS)
)
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment