Google: SphericalUtil.computeDistanceBetween() vs Location.distanceBetween()

Leo N
2 min readMar 29, 2020

--

https://en.wikipedia.org/wiki/Spherical_cap

Situation

When we are making application with maps, somehow we want to have essential tools for any application that requires distance calculations between geographic coordinates, which is a fundamental aspect of many location-based and geospatial applications. We are looking for some same tools too, and found that: SphericalUtil and Android SDK.

So, What’s the difference between Google Maps Android API Utility Library SphericalUtil.computeDistanceBetween() and Android [Location.distanceBetween()]?

Comparison

Both SphericalUtil.computeDistanceBetween() from the Google Maps Android API Utility Library and Location.distanceBetween() from the Android SDK can be used to calculate the distance between two geographic coordinates, but they have some differences:

1.Input Parameters:

  • SphericalUtil.computeDistanceBetween() takes two LatLng objects representing the coordinates.
  • Location.distanceBetween() takes latitude and longitude values directly as input parameters.

2. Return Value:

  • SphericalUtil.computeDistanceBetween() returns the distance between the two points in meters as a double value.
  • Location.distanceBetween() calculates the distance between two locations in meters and stores the result in the third parameter, which is an array of floats. It returns void.

3. Dependency:

  • SphericalUtil.computeDistanceBetween() is part of the Google Maps Android API Utility Library, so if you're already using this library for other mapping functionalities, it might be more convenient.
  • Location.distanceBetween() is part of the Android SDK, so it's available without needing additional dependencies.

4. Accuracy:

  • Both methods use different algorithms for calculating distance, but in practical terms, they are both accurate for most applications. However, it’s worth noting that SphericalUtil.computeDistanceBetween() uses a more sophisticated spherical geometry approach, which might be more accurate for longer distances or when dealing with points across different latitudes.

In summary, if you’re already using Google Maps in your Android app and want to stick with the Google ecosystem, SphericalUtil.computeDistanceBetween() might be more convenient. However, if you want a more lightweight solution or don't want to introduce additional dependencies, Location.distanceBetween() from the Android SDK is perfectly suitable.

Answer

In my opinion, it better to use Location.distanceBetween(), it tend to be more precise.

Sample

LatLng point1 = new LatLng(40.6974034, -74.1197638); // NewYork
LatLng point2 = new LatLng(40.7440702, -73.9353235); // Seattle
Double distance1 = SphericalUtil.computeDistanceBetween(point1, point2);
float[] results = new float[1];
Location.distanceBetween(
point1.latitude, point1.longitude,
point2.latitude, point2.longitude,
results);
Float distance2 = results[0];
Result

References

  1. WGS84 ellipsoid : https://en.wikipedia.org/wiki/World_Geodetic_System
  2. https://developers.google.com/maps/documentation/android-sdk/utility
  3. https://googlemaps.github.io/android-maps-utils/javadoc/com/google/maps/android/SphericalUtil.html

--

--

Leo N
Leo N

Written by Leo N

🇻🇳 🇸🇬 🇲🇾 🇦🇺 🇹🇭 Engineer @ GXS Bank, Singapore | MSc 🎓 | Technical Writer . https://github.com/nphausg

No responses yet