最近需要做一个离线地图的项目,然后找地图,最后确定从Google那边得到地图切片,然后就准备着手做了,Google地图的编号一开始不知到应该怎么做,后来查了资料之后,就尝试解方程了,最后用二分法找的解,效率不高,不过将就用了。下面是代码:
public class Main { /** * @param args * @param Google Map中将经纬度转换为图片编号的方法 * @author mikecoder */ public static void main(String[] args) { double LongDegree = 120.6395065; double LaDegree = 31.309066; int z = 19; int[] a = getGMapLocation(LongDegree, LaDegree, z); System.out.print(String.valueOf(a[0]) + ":" + String.valueOf(a[1])); } /** * @param x y z * @param x:为经度 * @param y:为纬度 * @param z:为缩放大小 */ private static int[] getGMapLocation(double x, double y, int z){ double tw,mapsize,origin,longdeg,longppd,longppdrad,longtiles,e; double pixelx,pixely; int tilex, tiley; tw = 256; //每个小图的尺寸256×256(px) mapsize = tw * Math.pow((double)2, (double)z); origin = mapsize / 2; longdeg = Math.abs(180 + x); longppd = mapsize / 360; longppdrad = mapsize/(2*Math.PI); pixelx = longdeg * longppd; longtiles = pixelx / tw; tilex = (int)longtiles; e = Math.sin((y)/180*Math.PI); if (e>0.9999) { e=0.9999; } if (e<-0.9999) { e=-0.9999; } pixely = origin + 0.5*Math.log((1+e)/(1-e)) * (-longppdrad); tiley = (int)(pixely / tw); //保存在这里 int[] PNGNum = new int[2]; PNGNum[0] = tilex; PNGNum[1] = tiley; return PNGNum; } }