java,经纬度转换行政区域
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
竭诚为您提供优质文档/双击可除java,经纬度转换行政区域
篇一:geohash算法经纬度转换
geohash算法经纬度转换
geohash在查找周围朋友,lbs中有很多的应用,在网上查找到geohash算法实现分享给大家。
importjava.util.hashmap;
importjava.util.map;
publicclassgeohashkit{
//geohashscharmap
//noasislsos
//oldmacdonalwouldntbehappy
privatestaticchar[]_base32={0,1,2,3,4,5,6,7,
8,9,b,c,d,e,f,g,h,j,k,m,n,
p,q,r,s,t,u,v,w,x,y,z};
privatefinalstaticmap_decodemap=newhashmap();static
{
intsz=_base32.length;
for(inti=0;i _decodemap.put(_base32[i],i);
}
}
privatestaticintprecision=12;
privatestaticint[]bits={16,8,4,2,1};
publicstaticvoidmain(string[]args){
geohashkitghf=newgeohashkit();
stringgc1=ghf.encode(31.277631,120.53916300000003);
stringgc2=ghf.encode(51.4797,-0.0124);
system.out.println(gc1);
system.out.println(gc2);
double[]gd1=ghf.decode(gc1);
double[]gd2=ghf.decode(gc2);
system.out.println(gd1[0]
+","+gd1[1]);
system.out.println(gd2[0]+","+gd2[1]);
}
publicstaticstringencode(doublelatitude,doublelongi
double[]lat_interval={-90.0,90.0};
double[]lon_interval={-180.0,180.0}; stringbuildergeohash=newstringbuilder(); booleanis_even=true;
intbit=0,ch=0;
while(geohash.length() doublemid=0.0; if(is_even){
mid=(lon_interval[0]+lon_interval[1])/2;
ch|=bits[bit];
lon_interval[0]=mid;
}else{
lon_interval[1]=mid;
}
}else{
mid=(lat_interval[0]+lat_interval[1])/2;
if(latitude>mid){
ch|=bits[bit];
lat_interval[0]=mid;
}else{
lat_interval[1]=mid;
}
is_even=is_evenfalse:true;
if(bit bit++;
}else{
geohash.append(_base32[ch]);
bit=0;
ch=0;
}
}
returngeohash.tostring();
}
publicstaticdouble[]decode(stringgeohash){
double[]ge=decode_exactly(geohash);
doublelat,lon,lat_err,lon_err;
lat=ge[0];
lon=ge[1];
lat_err=ge[2];
lon_err=ge[3];
doublelat_precision=math.max(1,math.round(-math.log 10(lat_err)))-1;doublelon_precision=math.max(1,math .round(-math.log10(lon_err)))-1;lat=getprecision(la
t,lat_precision);
lon=getprecision(lon,lon_precision);
returnnewdouble[]{lat,lon};
}
publicstaticdouble[]decode_exactly(stringgeohash){ double[]lat_interval={-90.0,90.0};
double[]lon_interval={-180.0,180.0};
doublelat_err=90.0;
doublelon_err=180.0;
intsz=geohash.length();
intbsz=bits.length;
doublelatitude,longitude;
for(inti=0;i
intcd=_decodemap.get(geohash.charat(i));
for(intz=0;z intmask=bits[z];
if(is_even){
lon_err/=2;
if((cd
}else{
lon_interval[1]=(lon_interval[0]+lon_interval[1])/2;