firebase / geofire-android

GeoFire for Android apps
Apache License 2.0
133 stars 36 forks source link

Docs issue: GeoFireUtils.getDistanceBetween() returns result in meters, not kilometers. #14

Closed tomislavhofman closed 3 years ago

tomislavhofman commented 3 years ago
package com.firebase.geofire;

  /**
   * Database-agnostic utilities for creating and querying GeoHashes.
   */
  public class GeoFireUtils {

    ...

    /**
     * Calculates the distance between two locations in kilometers.
     *
     * @param a the first location.
     * @param b the second location.
     * @return the distance between the two locations, in kilometers.
     */
    public static double getDistanceBetween(@NonNull GeoLocation a, @NonNull GeoLocation b) {
        return GeoUtils.distance(a, b);
    }

    ...

  }

If we look a little bit deeper

   package com.firebase.geofire.util;

    public final class GeoUtils {

    ...

      public static double distance(GeoLocation location1, GeoLocation location2) {
          return distance(location1.latitude, location1.longitude, location2.latitude, location2.longitude);
      }

      public static double distance(double lat1, double long1, double lat2, double long2) {
          // Earth's mean radius in meters
          final double radius = (Constants.EARTH_EQ_RADIUS + Constants.EARTH_POLAR_RADIUS)/2;
          double latDelta = Math.toRadians(lat1 - lat2);
          double lonDelta = Math.toRadians(long1 - long2);

          double a = (Math.sin(latDelta/2)*Math.sin(latDelta/2)) +
                     (Math.cos(Math.toRadians(lat1))*Math.cos(Math.toRadians(lat2)) *
                             Math.sin(lonDelta/2) * Math.sin(lonDelta/2));
          return radius * 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
      }

    ...

    }

public static double distance(double lat1, double long1, double lat2, double long2) calculates the distance in meters. To calculate its radius it is using Constants.EARTH_EQ_RADIUS which is 6378137 (earth's radius in meters).

tomislavhofman commented 3 years ago

Nevermind. Just saw that you fixed it. 🤦‍♂

samtstern commented 3 years ago

@tomislavhofman thanks for reporting! Yeah I fixed the docs and added tests but haven't done a release since then because I feel weird doing a "docs only" release. If more people get confused though I will definitely do that.