Java tool class to get the distance between latitude and longitude

Source: Internet
Author: User
Tags cos

public class geoutil {        /**      *  returns a rectangular range based on latitude and longitude      *      * @ param lng     *              Longitude      *  @param  lat     *              Latitude      *  @param  distance     *              distance (in meters)      *  @return  [lng1,lat1, lng2,lat2]  lower-left corner of the rectangle ( LNG1,LAT1) and upper right Corner (LNG2,LAT2)      */     public static  double[] getrectangle (double lng, double lat, long distance)  {          float delta = 111000;         if  (lng != 0 && lat != 0)  {              double lng1 = lng - distance                      /  math.abs (Math.Cos (Math.toradians (LAT))  * delta);              double lng2 = lng + distance                      /  math.abs (Math.Cos (Math.toradians (LAT))  * delta);              double lat1 = lat -  (Distance / delta);              double lat2 = lat +  (distance  / delta);             return  new double[] { lng1, lat1, lng2, lat2 };          } else {              // TODO ZHCH  equals 0 o'clock calculation formula               double lng1 = lng - distance / delta;              double lng2 = lng +  distance / delta;              double lat1 = lat -  (Distance / delta);              double lat2 = lat +  (Distance / delta);              return new double[] { lng1,  lat1, lng2, lat2 };         }      }        /**     *  get the distance between two points   m      *      *  @param  lat1  1th latitude      *  @param  lng1  1th longitude      *  @param  lat2  2nd latitude      *  @param  lng2  2nd Longitude       *  @return      */     public static  Double getdistanceofmeter (double lat1, double lng1,               double lat2, double lng2)  {          Double radlat1 = rad (LAT1);         double  Radlat2 = rad (LAT2);         double a =  radlat1 - radlat2;         double b =  rad (lng1)  - rad (lng2);          double s =  2 * math.asin (Math.sqrt (Math.pow (Math.sin (a / 2),  2)                   + math.cos (RADLAT1)  *  math.cos (RADLAT2)                   * math.pow (Math.sin (b / 2),  2));          s =&nbsP;s * earth_radius;         s = math.round (S  * 10000)  / 10;         return s;      }            private  Static double rad (double d)  {         return  d * Math.PI / 180.0;     }         /**     *  Earth Radius: 6378.137km     */      private static double EARTH_RADIUS = 6378.137;     }


Java tool class to get the distance between latitude and longitude

Related Article

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.