# Java calculates the distance between two points of latitude and longitude

Source: Internet
Author: User
Tags cos pow sin

`ImportJava.util.HashMap;ImportJava.util.Map;  Public classMapdistance {Private Static DoubleEarth_radius = 6378.137; Private Static DoubleradDoubled) {returnD * math.pi/180.0; }          /*** Calculates the distance (in km) of the two places according to the latitude and longitude of two locations * parameter is String type *@paramLAT1 User Longitude *@paramlng1 User Latitude *@paramlat2 Business Longitude *@paramlng2 Merchant Latitude *@return     */     Public Staticstring Getdistance (String lat1str, String lng1str, String lat2str, String lng2str) {Double lat1=double.parsedouble (LAT1STR); Double lng1=double.parsedouble (LNG1STR); Double LAT2=double.parsedouble (LAT2STR); Double lng2=double.parsedouble (LNG2STR); DoubleRADLAT1 =Rad (LAT1); DoubleRADLAT2 =Rad (LAT2); Doubledifference = RADLAT1-RadLat2; DoubleMdifference = rad (lng1)-Rad (lng2); DoubleDistance = 2 * Math.asin (MATH.SQRT (Math.pow (Math.sin), 2)                + Math.Cos (RADLAT1) *Math.Cos (RADLAT2)* MATH.POW (Math.sin (MDIFFERENCE/2), 2))); Distance= Distance *Earth_radius; Distance= Math.Round (Distance * 10000)/10000; String Distancestr= distance+ ""; Distancestr=Distancestr. SUBSTRING (0, Distancestr.indexof ("."))); returnDistancestr; }          /*** Get the latitude and longitude values within a certain distance of the current user * unit meters return Minlat * min Longitude minlng * min latitude Maxlat * Maximum longitude maxlng * Maximum latitude Minl at*/     Public Staticmap Getaround (string latstr, String lngstr, String raidus) {map map=NewHashMap (); Double Latitude= Double.parsedouble (LATSTR);//Pass value to longitudeDouble longitude = double.parsedouble (LNGSTR);//pass value to LatitudeDouble degree= (24901 * 1609)/360.0;//get each degree        DoubleRaidusmile =double.parsedouble (Raidus); Double MPDLNG= Double.parsedouble ((degree * math.cos (latitude * (math.pi/180)) + ""). Replace ("-", "" ")); Double DPMLNG= 1/mpdlng; Double RADIUSLNG= DPMLNG *Raidusmile; //Get minimum LongitudeDouble Minlat = Longitude-radiuslng; //Get maximum longitudeDouble Maxlat = longitude +radiuslng; Double Dpmlat= 1/degree; Double Radiuslat= Dpmlat *Raidusmile; //Get minimum LatitudeDouble minlng = Latitude-Radiuslat; //Get maximum latitudeDouble maxlng = latitude +Radiuslat; Map.put ("Minlat", minlat+ ""); Map.put ("Maxlat", maxlat+ ""); Map.put ("Minlng", minlng+ ""); Map.put ("Maxlng", maxlng+ ""); returnmap; }           Public Static voidMain (string[] args) {//test latitude and Longitude: 117.11811 36.68484// test latitude and longitude 2: 117.00999000000002 36.66123//System.out.println (getdistance ("117.11811", "36.68484", "117.00999000000002", "36.66123"));System.out.println (Getaround ("117.11811", "36.68484", "13000")); //117.01028712333508 (double), 117.22593287666493 (double),//36.44829619896034 (double), 36.92138380103966 (double)              }      }`

Java calculates the distance between two points of latitude and longitude

Related Keywords:
Related Article

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.