Java应用:经纬度匹配(geohash加密)

本文采用http://gc.ditu.aliyun.com地址进行经纬度匹配,无数量限制

如果给定经纬度进行geohash加密操作,先解密得到相应gps坐标,具体程序如下所示:

 1 import java.text.DecimalFormat;
 2 import java.util.BitSet;
 3 import java.util.HashMap;
 4 
 5 public class Lon_Lat {
 6     
 7     public static String location;
 8     public static String strlatlon[];
 9     
10     private static int numbits=5*7;
11     final static char[] digits = { '0', '1', '2', '3', '4', '5', '6', '7', '8',            
12             '9', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'j', 'k', 'm', 'n', 'p',
13             'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };
14     final static HashMap<Character,Integer> lookup=new HashMap<Character,Integer>();
15     static {
16         int i = 0;
17         for (char c : digits) {
18             lookup.put(c, i++);
19         }    
20     }
21     
22     public static void main(String ages) {
23         double[] latlon=new Lon_Lat().decode(ages);
24         Location loc=new Location();
25         strlatlon=new String[2];
26         
27         strlatlon[0]=String.valueOf(latlon[0]);
28         strlatlon[1]=String.valueOf(latlon[1]);
29         loc.main(strlatlon);
30         
31         location=loc.location;
32         
33         //System.out.println("纬度:"+latlon[0]+"\n经度:"+latlon[1]+"\n地址:"+location);
34     }
35     /* 
36      * 将GeoHash字串解码成经纬度
37      */
38     public double[] decode(String geohash) {
39         StringBuffer buffer=new StringBuffer();
40         for(char c:geohash.toCharArray()) {
41             int i=lookup.get(c);
42             String _buffer=Integer.toBinaryString(i);
43             //_buffer=String.format("%05d",_buffer);
44             if(_buffer.length()==1)
45                 _buffer="0000"+_buffer;
46             else if(_buffer.length()==2)
47                 _buffer="000"+_buffer;
48             else if(_buffer.length()==3)
49                 _buffer="00"+_buffer;
50             else if(_buffer.length()==4)
51                 _buffer="0"+_buffer;
52             //System.out.println("i:"+i+"   "+_buffer);
53             buffer.append(_buffer);
54             
55         }
56         //System.out.println(buffer);
57         
58         BitSet lonset=new BitSet();
59         BitSet latset=new BitSet();
60         
61         int j=0;
62         for(int i=0;i<numbits*2;i+=2) {
63             boolean isSet=false;
64             if(i<buffer.length())
65                 isSet=buffer.charAt(i)=='1';
66             lonset.set(j++, isSet);
67         }
68         
69         j=0;
70         for(int i=1;i<numbits*2;i+=2) {
71             boolean isSet=false;
72             if(i<buffer.length())
73                 isSet=buffer.charAt(i)=='1';
74             latset.set(j++, isSet);
75         }
76         
77         double lat=decode(latset,-90,90);
78         double lon=decode(lonset,-180,180);
79         
80         DecimalFormat df=new DecimalFormat("0.000000");
81         return new double[] {Double.parseDouble(df.format(lat)),Double.parseDouble(df.format(lon))};
82     }
83     
84     /*
85      * 根据二进制编码串和指定的数值变化范围计算得到经纬值
86      */
87     private double decode(BitSet bs,double floor,double ceiling) {
88         double mid=0;
89         for(int i=0;i<bs.length();i++) {
90             mid=(floor+ceiling)/2;
91             if(bs.get(i)) 
92                 floor=mid;
93             else
94                 ceiling=mid;
95         }
96         return mid;
97     }
98 
99 }

 

根据上述得到的地址进行地址匹配,具体程序如下:

 1 import java.net.URL;
 2 import net.sf.json.JSONArray;
 3 import net.sf.json.JSONObject;
 4 
 5 public class Location {
 6     public static String location;
 7     public static  void main(String ags[]) {
 8         
 9         String add=getAdd("40.072632","116.424866");
10         //System.out.println(add.substring(0,1));
11         
12         JSONObject jsonObject=JSONObject.fromObject(add);
13         JSONArray jsonArray=JSONArray.fromObject(jsonObject.getString("addrList"));
14         JSONObject j_2=JSONObject.fromObject(jsonArray.get(1));
15         String allAdd=j_2.getString("admName");
16         String addr=j_2.getString("addr");
17         String name=j_2.getString("name");
18         
19         //String loc=allAdd+addr+name;
20         String arr[]=allAdd.split(",");
21         if(arr.length==3) {
22             if(arr[0]==arr[1]) 
23                 location=arr[0]+arr[2]+addr+name;
24             else 
25                 location=arr[0]+arr[1]+arr[2]+addr+name;
26         }else {
27             if(arr[0]==arr[1])
28                 location=arr[0]+addr+name;
29             else
30                 location=arr[0]+arr[1]+addr+name;
31         }
32         
33         System.out.println(allAdd+addr+name);
34         System.out.println("省:"+arr[0]+"\n市:"+arr[1]+"\n区:"+arr[2]+"\n"+name);
35     }
36     public static  String getAdd(String lat,String lon) {
37         String urlString="http://gc.ditu.aliyun.com/regeocoding?l="+lat+","+lon+"type=010";
38         String res="";
39         try {
40             URL url=new URL(urlString);
41             java.net.HttpURLConnection conn=(java.net.HttpURLConnection)url.openConnection();
42             conn.setDoOutput(true);
43             conn.setRequestMethod("POST");
44             java.io.BufferedReader in=new java.io.BufferedReader(new java.io.InputStreamReader(conn.getInputStream(),"UTF-8"));
45             String line;
46             while((line=in.readLine())!=null) {
47                 res+=line+"\n";
48             }
49             in.close();
50         }catch(Exception e) {
51             System.out.println("error in wapaction,and e is"+e.getMessage());
52         }
53         //System.out.println(res);
54         return res;
55     }
56     
57 }

 

地址匹配信息如下,可按名称提取相应信息:

 

posted @ 2018-06-05 11:06  程序猿Time  阅读(2801)  评论(0编辑  收藏  举报