MapUtil.java
资源名称:GMapsTest.rar [点击查看]
上传用户:hygd004
上传日期:2022-07-01
资源大小:246k
文件大小:2k
源码类别:
J2ME
开发平台:
Java
- import net.dclausen.microfloat.MicroDouble;
- //Download by http://www.codefans.net
- public class MapUtil {
- private static int offset = 268435456;
- private static double radius = offset / Math.PI;
- /**
- * Utility method for map scrolling
- * If you need to scroll your map, you'll need to calculate a new center for your static image. The following adjust() method will return the new map center latitude and longitude, accepting the following arguments:
- * the current center latitute and longitude coordinates the deltaX and deltaY, in pixels, of new map center
- * the map zoom level
- * @param lat
- * @param lng
- * @param deltaX
- * @param deltaY
- * @param z zoom level
- * @return
- */
- public static double[] adjust(double lat, double lon, int deltaX, int deltaY, int z)
- {
- System.out.println("adjust:lat:"+lat+" lon:"+lon+" deltaX:"+deltaX+" deltaY:"+deltaY+" z:"+z);
- return new double[]{
- XToL(LToX(lat) + (deltaX<<(21-z))),
- YToL(LToY(lon) + (deltaY<<(21-z)))
- };
- }
- private static double LToX(double x)
- {
- return round(offset + radius * x * Math.PI / 180);
- }
- private static double LToY(double y)
- {
- return round(
- offset - radius *
- Double.longBitsToDouble(MicroDouble.log(
- Double.doubleToLongBits(
- (1 + Math.sin(y * Math.PI / 180))
- /
- (1 - Math.sin(y * Math.PI / 180))
- )
- )) / 2);
- }
- private static double XToL(double x)
- {
- return ((round(x) - offset) / radius) * 180 / Math.PI;
- }
- private static double YToL(double y)
- {
- return (Math.PI / 2 - 2 * Double.longBitsToDouble(
- MicroDouble.atan(
- MicroDouble.exp(Double.doubleToLongBits((round(y)-offset)/radius))
- )
- )) * 180 / Math.PI;
- }
- private static double round(double num)
- {
- double floor = Math.floor(num);
- if(num - floor >= 0.5)
- return Math.ceil(num);
- else
- return floor;
- }
- }