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