首页 > 代码库 > java------4.根据经纬度排序,并计算距离。。。。。。。。根据地址计算出经纬度

java------4.根据经纬度排序,并计算距离。。。。。。。。根据地址计算出经纬度


1
String sql = "select xm.*,xs.*,xs.id as shopId,sqrt ( ( (( " + customerLongitude 2 + "-xs.longitude)*PI()*12656*COS(((" + customerLatitude 3 + "+xs.latitude)/2)*PI()/180)/180)" + "*" + "(( " 4 + customerLongitude + "-xs.longitude)*PI()*12656*COS(((" 5 + customerLatitude + "+xs.latitude)/2)*PI()/180)/180) " + ")" 6 + "+(" + " (( " + customerLatitude 7 + "-xs.latitude)*PI()*12656/180) " + "*" + "((" + customerLatitude 8 + "-xs.latitude)*PI()*12656/180)" + " )" + " ) as juli "; 9 10 11 double LonB = Double.parseDouble(m.get("longitude").toString()); 12 double LatB = Double.parseDouble(m.get("latitude").toString()); 13 double distance = Math.floor(XiNaiInterfaceUtils.getDistance(customerLongitude,customerLatitude, LonB, LatB) / 1000*10)/10;

 

计算经纬度 需要 commons-lang3-3.1.jar 

 1 import java.io.BufferedReader;  
 2 import java.io.IOException;  
 3 import java.io.InputStreamReader;  
 4 import java.net.URL;  
 5 import java.net.URLEncoder;  
 6 import java.util.HashMap;  
 7 import java.util.Map;  
 8 
 9 import org.apache.commons.lang3.StringUtils;
10 /**
11  *经纬度
12  * @author Administrator
13  *
14  */
15 public class LantiDemo {
16 
17     public static final String KEY_1 = "7d9fbeb43e975cd1e9477a7e5d5e192a";  
18 
19     public static Map<String,String> getGeocoderLatitude(String address){  
20         BufferedReader in = null;  
21         try {  
22             address = URLEncoder.encode(address, "UTF-8");  
23             URL tirc = new URL("http://api.map.baidu.com/geocoder?address="+ address +"&output=json&key="+ KEY_1);  
24 
25 
26             in = new BufferedReader(new InputStreamReader(tirc.openStream(),"UTF-8"));  
27             String res;  
28             StringBuilder sb = new StringBuilder("");  
29             while((res = in.readLine())!=null){  
30                 sb.append(res.trim());  
31             }  
32             String str = sb.toString();  
33             Map<String,String> map = null;  
34             if(StringUtils.isNotEmpty(str)){  
35                 int lngStart = str.indexOf("lng\":");  
36                 int lngEnd = str.indexOf(",\"lat");  
37                 int latEnd = str.indexOf("},\"precise");  
38                 if(lngStart > 0 && lngEnd > 0 && latEnd > 0){  
39                     String lng = str.substring(lngStart+5, lngEnd);  
40                     String lat = str.substring(lngEnd+7, latEnd);  
41                     map = new HashMap<String,String>();  
42                     map.put("lng", lng);  
43                     map.put("lat", lat);  
44                     return map;  
45                 }  
46             }  
47         }catch (Exception e) {  
48             e.printStackTrace();  
49         }finally{  
50             try {  
51                 in.close();  
52             } catch (IOException e) {  
53                 e.printStackTrace();  
54             }  
55         }  
56         return null;  
57     }  
58 
59     public static void main(String args[]){  
60         try {  
61             Map<String, String> json = LantiDemo.getGeocoderLatitude("南京市栖霞区仙隐北路5号");  
62             System.out.println("lng : " + json.get("lng"));  
63             System.out.println("lat : " + json.get("lat"));  
64         }catch (Exception e ){  
65             e.printStackTrace();  
66         }  
67     }  
68 }

 

java------4.根据经纬度排序,并计算距离。。。。。。。。根据地址计算出经纬度