Skip to content

Instantly share code, notes, and snippets.

@muyiou
Created November 28, 2013 10:14
Show Gist options
  • Select an option

  • Save muyiou/7689781 to your computer and use it in GitHub Desktop.

Select an option

Save muyiou/7689781 to your computer and use it in GitHub Desktop.
解决国产手机Location.distancTo()距离不准的问题
private static final double EARTH_RADIUS = 6378137;
private static double rad(double d)
{
return d * Math.PI / 180.0;
}
public static double getDistanceofPoint(Location locationA, Location locationB) {
double radLat1 = rad(locationA.getLatitude());
double radLat2 = rad(locationB.getLatitude());
double a = radLat1 - radLat2;
double b = rad(locationA.getLongitude()) - rad(locationB.getLongitude());
double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a/2),2) +
Math.cos(radLat1)*Math.cos(radLat2)*Math.pow(Math.sin(b/2),2)));
s = s * EARTH_RADIUS;
s = Math.round(s * 10000) / 10000;
return s;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment