Java------4. Sort by latitude and longitude, and calculate distance ... Calculate the latitude and longitude according to the address

Source: Internet
Author: User
Tags cos


1String sql = "Select Xm.*,xs.*,xs.id as Shopid,sqrt (((" +Customerlongitude2+ "-xs.longitude) *pi () *12656*cos ((" +Customerlatitude3+ "+xs.latitude)/2) *pi ()/180)/180)" + "*" + "(("4+ Customerlongitude + "-xs.longitude) *pi () *12656*cos (("5+ Customerlatitude + "+xs.latitude)/2) *pi ()/180)/180)" + ")"6+ "+(" + " (( " +Customerlatitude7+ "-xs.latitude) *pi () *12656/180)" + "*" + "((" +Customerlatitude8+ "-xs.latitude) *pi () *12656/180)" + ")" + ") as Juli";9 Ten One DoubleLONB = double.parsedouble (M.get ("longitude"). toString ()); A DoubleLATB = double.parsedouble (M.get ("Latitude"). toString ()); - DoubleDistance = Math.floor (Xinaiinterfaceutils.getdistance (Customerlongitude,customerlatitude, LonB, LatB)/1000*10)/10;

Calculation of latitude and longitude requires Commons-lang3-3.1.jar

1 ImportJava.io.BufferedReader; 2 Importjava.io.IOException; 3 ImportJava.io.InputStreamReader; 4 ImportJava.net.URL; 5 ImportJava.net.URLEncoder; 6 ImportJava.util.HashMap; 7 ImportJava.util.Map; 8 9 Importorg.apache.commons.lang3.StringUtils;Ten /** One * Latitude and longitude A  * @authorAdministrator -  * -  */ the  Public classLantidemo { -  -      Public Static FinalString key_1 = "7d9fbeb43e975cd1e9477a7e5d5e192a";  -  +      Public StaticMap<string,string>getgeocoderlatitude (String address) { -BufferedReader in =NULL;  +         Try {   AAddress = Urlencoder.encode (address, "UTF-8");  atURL TIRC =NewURL ("http://api.map.baidu.com/geocoder?address=" + address + "&output=json&key=" +key_1);  -  -  -in =NewBufferedReader (NewInputStreamReader (Tirc.openstream (), "UTF-8"));  - String Res;  -StringBuilder SB =NewStringBuilder ("");  in              while(res = In.readline ())! =NULL){   - Sb.append (Res.trim ());  to             }   +String str =sb.tostring ();  -map<string,string> map =NULL;  the             if(Stringutils.isnotempty (str)) { *                 intLngstart = Str.indexof ("lng\": ");  $                 intLngend = Str.indexof (", \" Lat "); Panax Notoginseng                 intLatend = Str.indexof ("},\" precise ");  -                 if(Lngstart > 0 && lngend > 0 && latend > 0){   theString LNG = str.substring (lngstart+5, Lngend);  +String lat = str.substring (lngend+7, Latend);  AMap =NewHashmap<string,string>();  theMap.put ("LNG", LNG);  +Map.put ("Lat", LAT);  -                     returnmap;  $                 }   $             }   -}Catch(Exception e) { - E.printstacktrace ();  the}finally{   -             Try {  Wuyi In.close ();  the}Catch(IOException e) { - E.printstacktrace ();  Wu             }   -         }   About         return NULL;  $     }   -  -      Public Static voidMain (String args[]) { -         Try {   Amap<string, string> json = Lantidemo.getgeocoderlatitude ("Nanjing Qixia District North Road No. 5th");  +System.out.println ("LNG:" + json.get ("LNG")));  theSystem.out.println ("Lat:" + json.get ("lat"));  -}Catch(Exception e) { $ E.printstacktrace ();  the         }   the     }   the}

Java------4. Sort by latitude and longitude, and calculate distance ... Calculate the latitude and longitude according to the address

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.