Calculate distance between two places according to Latitude and longitude

Source: Internet
Author: User
Tags cos

 import java.util.HashMap;import java.util.Map;public class MapDistance {    private static double earth_radius = 6378.137;   private  static double rad (double d)  {       return d  * Math.PI / 180.0;   }   /**    *  Calculates the distance (in km) between the two places according to the latitude and longitude of the two positions     *  parameter is string type     *  @param  lat1Str  user Longitude     *  @param  lng1Str  user Latitude     *   @param  lat2Str  Business Longitude     *  @param  lng2Str  business Latitude      *  @return     */   public static String  Getdistance (STRING LAT1STR, STRING LNG1STR, STRING LAT2STR, STRING LNG2STR)  {   &nBsp;   double lat1 = double.parsedouble (LAT1STR);        double lng1 = double.parsedouble (LNG1STR);        double lat2 = double.parsedouble (LAT2STR);        Double lng2 = double.parsedouble (LNG2STR);        double  patm = 2;       double radlat1 = rad (LAT1);        double radlat2 = rad (LAT2);        double difference = radLat1 - radLat2;        double mdifference = rad (lng1)  - rad (lng2);        double distance = patm * math.asin (Math.sqrt (Math.pow (Math.sin ( DIFFERENCE / PATM),  patm)                + math.cos ( RADLAT1)  * math.cos (RADLAT2)                 * math.pow (Math.sin (MDIFFERENCE / PATM),  PATM));        distance = distance * EARTH_RADIUS;        string distancestr = string.valueof (distance);        return distanceStr;   }   /**    *  Gets the latitude and longitude values of the current user within a certain distance     *  unit meters  return minLat    *  min longitude  minLng    *  min latitude  maxLat    *  max longitude  maxlng     *  Max Latitude  minlat    */   public static  map getaround (STRING&NBsp;latstr, string lngstr, string raidus)  {        Map map = new hashmap ();        double latitude  = double.parsedouble (LATSTR);//  value to longitude        Double  Longitude = double.parsedouble (LNGSTR);//  value to Latitude        double  degree =  (24901 * 1609)  / 360.0; //  get per degree         double raidusmile = double.parsedouble (Raidus);        double mpdlng = double.parsedouble ((Degree * math.cos (latitude *   (math.pi / 180) + ""). Replace ("-",  ""));        double  dpmlng = 1 / mpdlng;       double radiuslng  = dpmlng  * raidusmile;       //get the minimum longitude         Double minLat = longitude - radiusLng;        //  get the maximum longitude        double maxlat = longitude +  radiusLng;       Double dpmLat = 1 / degree;        Double radiusLat = dpmLat * raidusMile;        //  get the minimum latitude        Double  minlng = latitude - radiuslat;       //  Get maximum Latitude        Double maxLng = latitude + radiusLat;        map.put ("Minlat",  minlat+ "");        map.put ("Maxlat",  maxlAt+ "");        map.put ("Minlng",  minlng+ "");        map.put ("Maxlng",  maxlng+ "");       return map;    }   public static void main (String[] args)  {        //Jinan International Exhibition Center latitude and longitude:117.11811  36.68484        //Baotu Spring:117.00999000000002  36.66123        System.out.println (Getdistance ("116.97265", "36.694514", "116.597805", "36.738024"));        system.out.println (Getaround ("117.11811",  "36.68484",  "13000"));        //117.01028712333508 (double),  117.22593287666493 (double),        //36.44829619896034 (double),  36.92138380103966 (double)    }

Calculate distance between two places according to Latitude and longitude

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.