简体   繁体   中英

Different distance between two points on iOS and Android

I'm trying to measure the distance between two points (longitude, latitude). My problem is that I get different results on iOS then on Android.

I've checked it with this site and the result was that the Android values are correct.

I'm using this MapKit method to get the distance in iOS: distanceFromLocation:

Here are my test locations:

P1 : 48.643798, 9.453735 P2 : 49.495150, 9.782150

Distance iOS : 97717 m Distance Android : 97673 m

How is this possible and how can I fix this?

So I was having a different issue and stumbled upon the answer to both of our questions:

On iOS you can do the following:

 meters1 = [P1 distanceFromLocation:P2]
 // meters1 is 97,717

 meters2 = [P2 distanceFromLocation:P1]
 // meters2 is 97,630

I've searched and searched but haven't been able to find a reason for the difference. Since they are the exact same points, it should show the same distance no matter which way you are traveling. I submitted it to Apple as a bug and they closed it as a duplicate but have still not fixed it. I would suggest to anyone who wants this to be fixed to also submit it as a bug.

In the meantime, the average of the two is actually the correct value:

 meters = (meters1 + meters2)/2
 // meters (the average of the first two) is 97,673

Apparently Android does not have this problem.

The longitude and latitude are not all that you need. You have to use the same reference model like WGS84 or ETRS89. The earth is not an exact ellipsoid, so you need models, none of the models are entirely exact, and depending on which model you use, distances are somewhat different.

Please make sure you use the same reference for iOS and Android.

There is more than one way to calculate distance between long/lat coords based on how you compensate for the curvature of the earth, and there's no right or wrong approach. Most likely the two platforms use a slightly different model.

Here are some formulae for calculating it yourself. http://www.movable-type.co.uk/scripts/latlong.html

If you absolutely need them to be the same, just implement your own calculation using one of these formulae, then you can ensure you get the same result on both platforms.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM