Skip to content

Instantly share code, notes, and snippets.

@yeluolanyan
Created August 6, 2014 08:44
Show Gist options
  • Save yeluolanyan/7afb1ec19e8f17544e3d to your computer and use it in GitHub Desktop.
Save yeluolanyan/7afb1ec19e8f17544e3d to your computer and use it in GitHub Desktop.
计算地图上两点之间的距离
/**
* 计算地图上两点之间的距离
* @param longitude
* @param latitude
* @param long2
* @param lat2
* @return
*/
public Double Distance(double longitude, double latitude, double long2,
double lat2) {
double a, b, R;
R = 6371; // 地球半径
latitude = latitude * Math.PI / 180.0;
lat2 = lat2 * Math.PI / 180.0;
a = latitude - lat2;
b = (longitude - long2) * Math.PI / 180.0;
double d;
double sa2, sb2;
sa2 = Math.sin(a / 2.0);
sb2 = Math.sin(b / 2.0);
d = 2 * R * Math.asin(
Math.sqrt(sa2 * sa2 + Math.cos(latitude)
* Math.cos(lat2) * sb2 * sb2));
return d;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment