MapUtil.java
上传用户:hygd004
上传日期:2022-07-01
资源大小:246k
文件大小:2k
源码类别:

J2ME

开发平台:

Java

  1. import net.dclausen.microfloat.MicroDouble;
  2. //Download by http://www.codefans.net
  3. public class MapUtil {
  4.   private static int offset = 268435456;
  5.   private static double radius = offset / Math.PI;
  6.  /**
  7.   * Utility method for map scrolling
  8.   * 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: 
  9.   * the current center latitute and longitude coordinates the deltaX and deltaY, in pixels, of new map center 
  10.   * the map zoom level 
  11.   * @param lat
  12.   * @param lng
  13.   * @param deltaX
  14.   * @param deltaY
  15.   * @param z  zoom level
  16.   * @return
  17.   */
  18.  public static double[] adjust(double lat, double lon, int deltaX, int deltaY, int z)
  19.  {
  20.   System.out.println("adjust:lat:"+lat+" lon:"+lon+" deltaX:"+deltaX+" deltaY:"+deltaY+" z:"+z);
  21.   return new double[]{
  22.    XToL(LToX(lat) + (deltaX<<(21-z))),
  23.    YToL(LToY(lon) + (deltaY<<(21-z)))
  24.   };
  25.  }
  26.  private static double LToX(double x)
  27.  {
  28.   return round(offset + radius * x * Math.PI / 180);
  29.  }
  30.   
  31.  private static double LToY(double y)
  32.  {
  33.   return round(
  34.    offset - radius * 
  35.    Double.longBitsToDouble(MicroDouble.log(
  36.     Double.doubleToLongBits(
  37.     (1 + Math.sin(y * Math.PI / 180))
  38.     /
  39.     (1 - Math.sin(y * Math.PI / 180))
  40.     )
  41.    )) / 2);
  42.  }
  43.   
  44.  private static double XToL(double x)
  45.  {
  46.   return ((round(x) - offset) / radius) * 180 / Math.PI;
  47.  }
  48.   
  49.  private static double YToL(double y)
  50.  {
  51.   return (Math.PI / 2 - 2 * Double.longBitsToDouble(
  52.      MicroDouble.atan(
  53.       MicroDouble.exp(Double.doubleToLongBits((round(y)-offset)/radius))
  54.      )
  55.     )) * 180 / Math.PI;
  56.  }
  57.  private static double round(double num)
  58.  {
  59.   double floor = Math.floor(num);
  60.   
  61.   if(num - floor >= 0.5)
  62.    return Math.ceil(num);
  63.   else
  64.    return floor;
  65.  }
  66. }