Calculate the distance between geographic locations

Source: Internet
Author: User
Tags asin cos

. NET calculates the maximum minimum latitude and longitude within a specified range based on coordinates, calculates the distance between two points according to the latitude and longitude coordinates of two points (note: The units here are meters)

#region calculates the maximum minimum latitude and longitude within a specified range according to coordinates, calculates the distance between two points, in meters private static, according to the latitude and longitude coordinates of two points (double value)Double PI =Math.PI;//3.14159265;//πprivate StaticDoubleEarth_radius= 6378137;//Earth radius private staticDoubleRad=Math.PI / 180.0;//Pi/ the#region calculates the maximum minimum latitude and longitude within a specified range based on coordinates/// <Summary>        ///calculates the maximum minimum latitude and longitude within a specified range, based on coordinates/// </Summary>        /// <param name="LNG">Longitude</Param>        /// <param name="Lat">Latitude</Param>        /// <param name="Raidus">Range (m)</Param>        /// <returns>Returns maximum, minimum latitude and longitude minlng, Minlat, MAXLNG, Maxlat</returns>         Public Double[]Getaround (DoubleLngDoubleLatintRaidus) {            //The circumference ofThe Earth is  -,901miles. // -,901/ the = 69.17Miles/degreeDoubleLatitude=lat; DoubleLongitude=LNG; DoubleDegree=(24901 * 1609)/ 360.0;//the perimeter of the earth is 24901 miles.DoubleRaidusmile=Raidus; //Calculate latitude FirstDoubleDpmlat= 1 /degree; DoubleRadiuslat=Dpmlat*Raidusmile; DoubleMinlat=Latitude-Radiuslat; DoubleMaxlat=Latitude+Radiuslat; //Calculate LongitudeDoubleMpdlng=Degree*Math.Cos(Latitude*(PI /  the));//cosine of the latitudeDoubleDpmlng= 1 /mpdlng; DoubleRadiuslng=Dpmlng*Raidusmile; DoubleMinlng=Longitude-radiuslng; DoubleMaxlng=Longitude+radiuslng; //System.out.println ("["+minlat+", "+minlng+", "+maxlat+", "+maxlng+"]"); //min. longitude, min latitude, maximum longitude, maximum latitudereturnNewDouble[]{minlng, Minlat, MAXLNG, Maxlat}; #endregion #region calculates the distance between two points, in meters, based on the latitude and longitude coordinates (double value) between two points/// <Summary>        ///calculates the distance between two points, in meters, based on the latitude-longitude coordinate of two points (double value)/// </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 (m)</returns>         Public DoubleGetdistance (DoubleLng1,DoubleLAT1,DoubleLng2,Doublelat2) {            DoubleRadLat1=Lat1*RAD;// //Rad=Pi/ the            DoubleRadLat2=Lat2*RAD; DoubleA=RadLat1-RadLat2; DoubleB=(lng1-LNG2)*RAD; DoubleS= 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; returns; } #endregion #endregion


SQL: Calculates the distance between two coordinate points (longitude, latitude) on the earth from the SQL function (Note: The units here are kilometers)

Go   --calculates the distance between two coordinate points (longitude, latitude) of the Earth 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 Transfer from: http://www.aspbc.com/tech/showtech.asp?id=1136
Baidu map get distance between two points online test: Http://developer.baidu.com/map/jsdemo.htm#a6_1

Calculate the distance between geographic locations

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.