I am trying to calculate the distance between two points (Pins) on the iPhone map.
Everything works fine however if you put one of the pin close to the left edge and the other one to the right edge of the map, distanceFromLocation method always returned the shortest distance between points (of course earth is not flat). I tried switching the CLLocations but it still shows me the shortest distance.
For my app I will need to calculate both, shortest and the longest distance between two pins.
The problem seems to be trivial but I can’t come out with anything to solve it.
Any help or clues appreciated. Thanks!
There is a function
MKCoordinateRegionForMapRectthat returns a MKCoordinateRegion. Without having tried it, seems like all you would need to do is accurately specify the rect as the region bounded at one diagonal by one pin and the other diagonal by the other pin. You could use CGRectUnion twice giving the pin origins as rect origin and an arbitrarily small width and height.Then the distance between the pins will be the square root of the distances represented by the latitudeDelta and longitudeDelta returned in the span squared and added. (i.e. Pythagoras theorem, the two side lengths are the horizontal and vertical sides of a triangle and the diagonal is a straight line between your points.)
Finding latitudeDelta in metres is easy, each degree of latitude is 111km.
To find meters for the longitudeDelta you need a little bit more code.
Give that function the longitude of the location you are interested in and multiply the result by the longitudeDelta in degrees to get a distance in meters. Then you have a horizontal distance and a vertical and you can find the diagonal which is what you were looking for.
Inaccuracy will get bad if you are looking for a distance which is very large in N-S as the length of a degree longitude will be varying over the calculation. And you can refine the 111km for a degree of latitude a bit too, that’s just from memory.