Skip to content

Instantly share code, notes, and snippets.

@rezaiyan
Last active April 19, 2020 04:53
Show Gist options
  • Save rezaiyan/58be5d14a2409fdfac9b043364dca483 to your computer and use it in GitHub Desktop.
Save rezaiyan/58be5d14a2409fdfac9b043364dca483 to your computer and use it in GitHub Desktop.
EPSG 4326 , EPSG 3857 converter
private static double EARTH_RADIUS = 6378137.0;
public static LatLng to4326(Wgs84 wgs84) {
double lat = Math.toDegrees(Math.atan(Math.exp(wgs84.getY() / EARTH_RADIUS)) * 2 - Math.PI/2);
double lng = Math.toDegrees(wgs84.getX() / EARTH_RADIUS);
return new LatLng(lat, lng);
}
public static Wgs84 toWgs84(LatLng latLng) {
double x = Math.toRadians(latLng.longitude) * EARTH_RADIUS;
double y = Math.log(Math.tan(Math.PI / 4 + Math.toRadians(latLng.latitude) / 2)) * EARTH_RADIUS;
return new Wgs84(x, y);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment