IOS calculates the distance between two latitude and longitude degrees

Source: Internet
Author: User
Tags acos cos



IOS calculates the distance between two latitude and longitude degrees


One,


-(double)distanceBetweenOrderBy:(double) lat1 :(double) lat2 :(double) lng1 :(double) lng2{

           CLLocation *curLocation = [[CLLocation alloc] initWithLatitude:lat1 longitude:lng1];

           CLLocation *otherLocation = [[CLLocation alloc] initWithLatitude:lat2 longitude:lng2]; double distance  = [curLocation distanceFromLocation:otherLocation]; return distance;

}




Second,


Convert Angle to radians


+ (float) radians: (float) degrees{

Return (degrees*3.14159265)/180.0;

}


Line distance is converted according to latitude and longitude


+ (float) getdistance: (float) lat1 lng1: (float) lng1 lat2: (float) lat2 lng2: (float) lng2

{

Earth radius

int R = 6378137;

Convert Angle to radians

float RADLAT1 = [self RADIANS:LAT1];

float RADLAT2 = [self radians:lat2];

float radLng1 = [self radians:lng1];

float radLng2 = [self radians:lng2];

Results

float s = acos (cos (RADLAT1) *cos (RADLAT2) *cos (radlng1-radlng2) +sin (RADLAT1) *sin (RADLAT2)) *r;

Precision

s = Round (s* 10000)/10000;

Return round (s);

}




Introduction to Latitude How are these longitude determined? The earth is constantly spinning around its axis (the earth's axis is an imaginary line through both the north and the South Poles and the center of the Earth), and the Earth's waist draws a large circle perpendicular to its axis, so that every point on the circle is equal to the north and South poles, and this circle is called "the equator". At the north and south sides of the equator, many circles parallel to the equator are drawn, that is, "weft rings"; the segments that make up these circles are called parallels. We set the equator at 0 degrees to the north and 90 degrees to the south, to the southern latitude south of the equator, to the north of the equator called latitude. North Pole is latitude 90 degrees, Antarctica is south latitude 90 degrees. Latitude also marked the climate of hot and cold, such as the equator and low latitudes of the region without winter, polar and high latitudes without summer, mid-latitude regions distinct four seasons.   Second, from the North Pole to the Antarctic point, you can draw many large circles in the north and south that are perpendicular to the Earth's equator, called the "Warp Circle"; the segments that make up these circles are called meridians. Year 1884, the International rules of the Greenwich Observatory in the suburbs of London by longitude as the starting point for the calculation of longitude, that is, latitude of 0 minutes and 0 seconds, also known as the "Prime Meridian." In the east of it is longitude, a total of 180 degrees, the west of it for the western longitude, a total of 180 degrees. Because the earth is round, the longitude of 180 degrees east longitude and longitude 180 degrees West is the same meridian line. The 180-degree meridian is "International Date Line" in all countries. In order to avoid the use of two different dates in the same area, the International Date line has slightly deviated from the land.    each longitude and latitude can be subdivided into 60 points, each divided into 60 seconds and fractional seconds. Using longitude, we can determine the exact location of every place on the Earth and show it on a map or globe. For example, what is the latitude and longitude of Beijing? We can easily find out from the map is 116 degrees east longitude 24 points, north latitude 39 degrees 54 points. Ships sailing in the sea can determine the position and direction of the ship as long as the longitude of the location is measured. Latitude has a total of 90 degrees. The equator is 0 degrees, aligned to the Poles, the smaller the circle, the greater the degree. The horizontal line is latitude, and the vertical line is longitude.   Of course can calculate, four Yuan two times equation.   Longitude and latitude are an angle. Longitude is a two-sided angle, which is the angle of two meridians. Because all meridians are the same length, in order to measure longitude to select a starting point, after 1884 years of International conference, decided to take the meridian line of a major afternoon cross of the Royal Observatory (formerly) in the outskirts of London, Thames, R., the South Bank of the United Kingdom to the beginning meridian, known as the Prime Meridian. The Prime Meridian plane is the starting plane, and the end face is the local meridian plane. The longitude of a point is the angle between the meridian plane and the Prime Meridian plane. Measured on the equator, from the Prime Meridian plane as the starting point, respectively east to West Measurement, the east measure value is called the longitude degree, the westward measure is called the longitude degree. Thus, a land ofLongitude is the direction and angular distance of the place for the Prime meridian. The Prime meridian is 0° longitude, the maximum longitude is 180°, the maximum longitude is 180°, the east and west longitude 180 ° meridian is the same meridian, so regardless of longitude or west longitude, and collectively, 180° meridians.    latitude is a line face angle. The starting plane is the equatorial plane, and the line is the local ground normal. The so-called normal, that is perpendicular to the reference flat sphere surface of the line. The latitude of a place is the angle between the normal of the ground and the equatorial plane. Latitude is measured on the local meridian, measured from the equator to the south, north to north, and to the south as latitude. Thus, the latitude of a land is the direction and angular distance of the equator. The equator is the 0° latitude, the maximum latitude is 90°, that is the North Pole, and the maximum latitude is 90°, which is the Antarctic point. Latitude interchange conversion degree of degrees minutes (DDD): E 108.90593 degrees    n 34.21630 degrees   How to degree (DDD):: 108.90593 degrees of conversion into degrees seconds (DMS) longitude E 108 degrees 54 minutes 22.2 seconds? The conversion method is to take 108.90593 integer digits unchanged 108 (degrees), with 0.90593*60=54.3558, take the whole number of bits 54 (min), 0.3558*60=21.348 and then take the integer digit 21 (seconds), it is converted to 108 degrees 54 minutes 21 seconds. will also be a fraction of a second (DMS ): East longitude e 108 degrees 54 minutes 22.2 seconds conversion into degrees (DDD) method is as follows: 108 degrees 54 minutes 22.2 seconds =108+ (54/60) + (22.2/3600) =108.90616 degrees because of the reason for the number of decimal digits in the calculation, there is a certain error in positive and negative calculation, But the error effect is not very large. The 1-second error is a few meters. GPS car Friends can use the above method to convert to their own desired unit coordinates.    about latitude and longitude decimal notation
For two points, in the case of equal latitude:
Longitude every 0.00001 degrees, the distance is about 1 meters, every 0.0001 degrees, the distance is about 10 meters, every 0.001 degrees, the distance is about 100 meters, every 0.01 degrees, the distance is about 1000 meters, every 0.1 degrees, the distance is about 10000 meters.

For two points, in the case of equal longitude:

Latitude every 0.00001 degrees, the distance is about 1.1 meters, every 0.0001 degrees, the distance is about 11 meters, every 0.001 degrees, the distance is about 111 meters, every 0.01 degrees, the distance is about 1113 meters, every 0.1 degrees, the distance is about 11132 meters.       calculates the distance between two points according to the latitude and longitude of any two points on Earth   Method 1: Because the earth is an ellipsoid, this is too difficult to forget, if the earth is a sphere, you can use the following formula: Set the longitude of a point on the earth as a, Latitude is B, the spatial coordinates of this point are X=cos (b) *cos (a) Y=cos (b) *sin (a) Z=sin (b) Set the space coordinates of two points on Earth (X1,Y1,Z1) respectively, (X2,Y2,Z2) Their angle is C=acos (x1*x2+y1* Y2+Z1*Z2), C is the angle of the distance between the two places is C/180*pi*r, where R is the Earth's average radius of 6371 error does not exceed 1%   Earth is a nearly standard ellipsoid, its equatorial radius of 6378.140 km, the polar radius of 6356.755 km , with an average radius of 6371.004 km. If we assume that the earth is a perfect sphere, then its radius is the average radius of the Earth, recorded as R. If the 0-degree meridian is used as a benchmark, the surface distance between the two points can be calculated based on the latitude and longitude of any two points on the Earth's surface (this ignores the error caused by the Earth's surface topography, which is only a theoretical estimate). The latitude and longitude of 1th A is (Lona, LatA), the latitude and longitude of 2nd B is (LONB, LATB), according to the datum of 0 degrees longitude, the positive value of longitude (longitude), the longitude is negative (-longitude), north latitude takes 90-latitude value (90- Latitude), 90+ latitude value (90+latitude), the two points after the above treatment are counted as (Mlona, Mlata) and (Mlonb, MLATB). Then, according to triangular derivation, the following formula can be obtained for calculating the distance of two points: c = sin (mlata) *sin (MLATB) *cos (mlona-mlonb) + cos (mlata) *cos (MLATB) Distance = R*arccos (C) *pi /180  here, R and distance units are the same, if the use of 6371.004 km as a radius, then distance is the unit, if you want to use other units, such as mile, also need to do unit conversion, 1-kilometer = 0.621371192mile If only longitude is treated as positive or negative, not latitudeFor 90-latitude (assuming all the northern hemisphere, the southern hemisphere is only applied to Australia), then the formula will be: C = sin (LatA) *sin (LATB) + cos (LatA) *cos (LATB) *cos (MLONA-MLONB) Distance = R*arccos (C) *pi/180 above can be introduced by simple triangular transformations. If the input and output of trigonometric functions are in radians, then the formula can also be written:  c = sin (lata*pi/180) *sin (latb*pi/180) + cos (lata*pi/180) *cos (latb*pi/180) *cos (( MLONA-MLONB) *pi/180) Distance = R*arccos (c) *pi/180 that is: c = sin (lata/57.2958) *sin (latb/57.2958) + cos (lata/57.2958) *cos (latb/57.2958) *cos ((mlona-mlonb)/57.2958) Distance = R*arccos (c) = 6371.004*arccos (c) kilometer = 0.621371192*6371.004 *arccos (c) mile = 3958.758349716768*arccos (c) mile[Java]View PlainCopy

<span style="font-size:14px;font-weight:normal;" > private static final double earth_radius = 6378137; Equatorial radius (unit m)
/** 
* Convert to radians (RAD)
* */
private static double rad (double D)
{
return d * Math.PI/ 180.0;
}
/** 
* Calculate the distance between two latitude and longitude based on cosine theorem
* @param the accuracy of Lon1 1th
* @param the latitude of lat1 1th
* @param the accuracy of Lon2 2nd
* @param the latitude of LAT3 2nd
* @return return distance, units km
* */
public static double lantitudelongitudedist (double lon1, double lat1, double lon2, double LAT2) {
Double radLat1 = rad (LAT1);
Double radLat2 = rad (LAT2);
Double radLon1 = rad (Lon1);
Double radLon2 = rad (Lon2);
if (RadLat1 < 0)
RADLAT1 = Math.PI/ 2 + math.abs (RADLAT1); South
if (RadLat1 > 0)
RADLAT1 = Math.PI/ 2-math.abs (RADLAT1); North
if (RadLon1 < 0)
RadLon1 = Math.PI * 2-math.abs (radLon1); West
if (RadLat2 < 0)
RADLAT2 = Math.PI/ 2 + math.abs (RADLAT2); South
if (RadLat2 > 0)
RADLAT2 = Math.PI/ 2-math.abs (RADLAT2); North
if (RadLon2 < 0)
RadLon2 = Math.PI * 2-math.abs (radLon2); West
Double x1 = Earth_radius * Math.Cos (radLon1) * Math.sin (RADLAT1);
Double y1 = Earth_radius * Math.sin (radLon1) * Math.sin (RADLAT1);
Double z1 = Earth_radius * Math.Cos (RADLAT1);
Double x2 = Earth_radius * Math.Cos (radLon2) * Math.sin (RADLAT2);
Double y2 = Earth_radius * Math.sin (radLon2) * Math.sin (RADLAT2);
Double z2 = Earth_radius * Math.Cos (RADLAT2);
Double D = math.sqrt ((x1-x2) * (X1-X2) + (y1-y2) * (y1-y2) + (Z1-Z2) * (Z1-Z2));
//cosine theorem seeking angle
Double theta = Math.acos ((Earth_radius * Earth_radius + Earth_radius * earth_radius-d * d)/(2 * Earth_radius *  Earth_radius));
Double dist = theta * Earth_radius;
return dist;
}</span>





Method 2:google Map provides the method: The above formula is explained as follows:
1.lat1 Lung1 represents the latitude and longitude of a point, Lat2 Lung2 represents the latitude and longitude of B point;
2.A=LAT1–LAT2 the difference between the latitude of two points and b=lung1-lung2 two points of longitude;
3.6378.137 is the radius of the Earth, the unit is kilometer;
The calculated results are in kilometers, and if the radius is changed to meters, the result is measured in meters.
The accuracy of the calculation is similar to that of Google Maps, with a range of less than 0.2 meters.

[Java]View PlainCopy< param name= "wmode" value= "Transparent" >


<span style="FONT-SIZE:14PX;" > private static final double earth_radius = 6378137; Equatorial radius (unit m)
/** 
* Convert to radians (RAD)
* */
private static double rad (double D)
{
return d * Math.PI/ 180.0;
}
/** 
* Based on the algorithm in the GoogleMap to obtain the distance between the two latitude and longitude, the accuracy of the calculation is similar to the accuracy of Google Maps, the difference range of 0.2 meters below
* @param the accuracy of Lon1 1th
* @param the latitude of lat1 1th
* @param the accuracy of Lon2 2nd
* @param the latitude of LAT3 2nd
* @return return distance, units km
* */
public static double getdistance (double lon1,double lat1,double lon2, double lat2)
{
Double radLat1 = rad (LAT1);
Double radLat2 = rad (LAT2);
Double A = RADLAT1-RADLAT2;
Double b = rad (Lon1)-Rad (Lon2);
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;
}</span>





IOS calculates the distance between two latitude and longitude degrees


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.