Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find out distance between coordinates?

I want to make it so that it will show the amount of distance between two CLLocation coordinates. Is there someway to do this without a complex math formula? If there isn't how would you do it with a formula?

like image 203
John Doe Avatar asked Oct 23 '15 14:10

John Doe


People also ask

Can you measure distance by coordinates?

The difference between 10 and 0 is 10, and so Z = 10. Thus, the square of the distance between the coordinates is 141. Take the square root of D2 to find D, the actual distance between the two points. For example, if D2 = 141, then D = 11.874, and so the distance between (-3, 7, 10) and (1, 2, 0) is 11.87.

What is the distance formula for 3 coordinates?

Similarly, the distance between two points P1 = (x1,y1,z1) and P2 = (x2,y2,z2) in xyz-space is given by the following generalization of the distance formula, d(P1,P2) = (x2 x1)2 + (y2 y1)2 + (z2 z1)2.

How do you find distance on a graph?

When dealing with horizontal lines, the length of the line is simply the difference between the two points' x-coordinates. In a similar fashion, a vertical line's length can be found by subtracting one of the y-coordinates with the other.


2 Answers

CLLocation has a distanceFromLocation method so given two CLLocations:

CLLocationDistance distanceInMeters = [location1 distanceFromLocation:location2]; 

or in Swift 4:

//: Playground - noun: a place where people can play  import CoreLocation   let coordinate₀ = CLLocation(latitude: 5.0, longitude: 5.0) let coordinate₁ = CLLocation(latitude: 5.0, longitude: 3.0)  let distanceInMeters = coordinate₀.distance(from: coordinate₁) // result is in meters 

you get here distance in meter so 1 miles = 1609 meter

if(distanceInMeters <= 1609)  {  // under 1 mile  }  else {  // out of 1 mile  } 
like image 71
Glenn Howes Avatar answered Oct 02 '22 09:10

Glenn Howes


Swift 4.1

import CoreLocation  //My location let myLocation = CLLocation(latitude: 59.244696, longitude: 17.813868)  //My buddy's location let myBuddysLocation = CLLocation(latitude: 59.326354, longitude: 18.072310)  //Measuring my distance to my buddy's (in km) let distance = myLocation.distance(from: myBuddysLocation) / 1000  //Display the result in km print(String(format: "The distance to my buddy is %.01fkm", distance)) 
like image 41
Mohammed Abunada Avatar answered Oct 02 '22 09:10

Mohammed Abunada