Calculate the distance between geographical locations and geographical distance

Source: Internet
Author: User
Tags asin

Calculate the distance between geographical locations and geographical distance

. Net calculates the maximum and minimum latitude and longitude in the specified range based on coordinates, and calculates the distance between two points based on the latitude and longitude coordinates between two points (double value) (Note: The unit here is meter)

# Region calculates the maximum and minimum latitude and longitude in the specified range based on the coordinates, and calculates the distance between two points based on the longitude and latitude coordinates (double value). The Unit is meter private static double PI = Math. PI; // 3.14159265; // π private static double EARTH_RADIUS = 6378137; // Earth radius private static double RAD = Math. PI/180.0; // π/180 # region calculates the maximum and minimum latitude and longitude in the specified range based on the coordinates. // <summary> /// Based on the coordinates, calculate the maximum and minimum longitude and latitude in the specified range /// </summary> /// <param name = "lng"> longitude </param> /// <param name = "lat "> latitude </param> /// <param name =" raidus "> range (meters) </param> // <returns> returns the maximum and minimum latitude and longitude of minLng, minLat, maxLng, and maxLat </returns> public double [] getAround (double lng, double lat, int raidus) {// The circumference of the earth is 24,901 miles. // 24,901/360 = 69.17 miles/degree Double latitude = lat; Double longpolling = lng; Double degree = (24901*1609)/360.0; // The Earth's perimeter is 24901 Miles double raidusMile = raidus; // calculate the latitude Double dpmLat = 1/degree; Double radiusLat = dpmLat * raidusMile; Double minLat = latitude-radiusLat; double maxLat = latitude + radiusLat; // calculate the longitude Double mpdLng = degree * Math. cos (latitude * (PI/180); // cosine of the latitude Double dpmLng = 1/mpdLng; Double radiusLng = dpmLng * raidusMile; Double minLng = longbench-radiusLng; double maxLng = longbench + radiusLng; // System. out. println ("[" + minLat + "," + minLng + "," + maxLat + "," + maxLng + "]"); // minimum longitude, minimum latitude, maximum longitude, maximum latitude return new double [] {minLng, minLat, maxLng, maxLat };#endregion # region calculates the distance between two points based on the longitude and latitude coordinates (double value) between two points, the Unit is meter /// <summary> /// calculates the distance between two points based on the longitude and latitude coordinates (double value) between two points, the Unit is meter /// </summary> /// <param name = "lng1"> longitude 1 </param> /// <param name = "lat1"> latitude 1 </param> /// <param name = "lng2"> longitude 2 </param> /// <param name = "lat2"> latitude 2 </param> // /<returns> return distance (meters) </returns> public double getDistance (double lng1, double lat1, double lng2, double lat2) {double radLat1 = lat1 * RAD; /// RAD = π/180 double radLat2 = lat2 * RAD; double a = radLat1-radLat2; double B = (lng1-lng2) * RAD; 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 = s * EARTH_RADIUS; s = Math. round (s * 10000)/10000; return s ;}# endregion # endregion


 

SQL: calculates the distance between two coordinate points (longitude and latitude) on the earth. (Note: The unit here is kilometers)

Go -- calculate the distance between two coordinate points (longitude and latitude) on the earth from the SQL FUNCTION CREATE FUNCTION [dbo]. [fnGetDistance] (@ LatBegin REAL, @ LngBegin REAL, @ LatEnd REAL, @ LngEnd REAL) returns float as begin -- distance (km) DECLARE @ Distance real declare @ EARTH_RADIUS real set @ EARTH_RADIUS = 6378.137 DECLARE @ RadLatBegin REAL, @ RadLatEnd REAL, @ RadLatDiff REAL, @ RadLngDiff real set @ RadLatBegin = @ LatBegin * PI () /180.0 SET @ RadLatEnd = @ LatEnd * PI ()/180.0 SET @ RadLatDiff = @ RadLatBegin-@ RadLatEnd SET @ RadLngDiff = @ LngBegin * PI () /180.0-@ LngEnd * PI ()/180.0 SET @ Distance = 2 * ASIN (SQRT (POWER (SIN (@ RadLatDiff/2), 2) + COS (@ RadLatBegin) * COS (@ RadLatEnd) * POWER (SIN (@ RadLngDiff/2), 2 ))) SET @ Distance = @ Distance * @ EARTH_RADIUS SET @ Distance = Round (@ Distance * 10000)/10000 RETURN @ Distance END

SQL

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.