I'm developing an application that needs to retrieve maps adjacent to each other. Google static maps only allows to set location based on an address (geocoding) or a center (latLng). I need to be able to get a map based on a viewport (top-left latlng, bottom-right latlng) or simply by getting the adjacent map to the one y currently have. I've tried to do some type of math to get next center based on the current zoom level and pixel size with no success.
My client will get business license so don't worry those restrictions.
Thxs for the help really stuck on this one. All suggestions are welcome.
Found the answer on the web. Turns out Google Maps uses the Mercator projection. After finding that out the math became easier. I needed the code to translate from pixels to latlng at a determinate zoomlevel.
This is the code in java:
static final double OFFSET = 268435456;
static final double OFFSET_RADIUS = OFFSET / Math.PI;
static final double MATHPI_180 = Math.PI / 180;
static private final double preLonToX1 = OFFSET_RADIUS * (Math.PI / 180);
public static double LonToX(double lon) {
return Math.round(OFFSET + preLonToX1 * lon);
}
public static double LatToY(double lat) {
return Math.round(OFFSET - OFFSET_RADIUS * Math.log((1 + Math.sin(lat * MATHPI_180)) / (1 - Math.sin(lat * MATHPI_180))) / 2);
}
public static double XToLon(double x) {
return ((Math.round(x) - OFFSET) / OFFSET_RADIUS) * 180 / Math.PI;
}
public static double YToLat(double y) {
return (Math.PI / 2 - 2 * Math.atan(Math.exp((Math.round(y) - OFFSET) / OFFSET_RADIUS))) * 180 / Math.PI;
}
public static double adjustLonByPixels(double lon, int delta, int zoom) {
return XToLon(LonToX(lon) + (delta << (21 - zoom)));
}
public static double adjustLatByPixels(double lat, int delta, int zoom) {
return YToLat(LatToY(lat) + (delta << (21 - zoom)));
}
Hope this helps someone.