Android google map&nbs…

  在Android google map中,有时候会碰到计算两地的距离,下面的辅助类就可以帮助你计算距离:

public class DistanceHelper {
 
 public final static int KILOMETERS = 0;
 public final static int STATUTE_MILES = 1;
 public final static int NAUTICAL_MILES = 2;

 
 private final static double EARTHS_RADIUS[] = { 6378.1, // Kilometers
   3963.1676, // Statue miles
   3443.89849 // Nautical miles
 };

 
 private static final double DEGREES_TO_RADIANS = (180 / Math.PI);

 
 private static double calclateArc(double aLat, double aLong, double bLat,
   double bLong) {
  
  double aLatRad = aLat / DEGREES_TO_RADIANS;
  double aLongRad = aLong / DEGREES_TO_RADIANS;
  double bLatRad = bLat / DEGREES_TO_RADIANS;
  double bLongRad = bLong / DEGREES_TO_RADIANS;

  // Calculate the length of the arc that subtends point a and b
  double t1 = Math.cos(aLatRad) * Math.cos(aLongRad) * Math.cos(bLatRad)
    * Math.cos(bLongRad);
  double t2 = Math.cos(aLatRad) * Math.sin(aLongRad) * Math.cos(bLatRad)
    * Math.sin(bLongRad);
  double t3 = Math.sin(aLatRad) * Math.sin(bLatRad);
  double tt = Math.acos(t1 + t2 + t3);

  // Return a "naked" length for the calculated arc
  return tt;
 }

 
 public static double calculateDistance(GeoPoint pointA, GeoPoint pointB,
   int units) {
  return calclateArc(pointA.getLatitudeE6() / 1E6,
    pointA.getLongitudeE6() / 1E6, pointB.getLatitudeE6() / 1E6,
    pointB.getLongitudeE6() / 1E6) * EARTHS_RADIUS[units];
 }

 
 public static double calculateDistance(Location pointA, Location pointB,
   int units) {
  return calclateArc(pointA.getLatitude(), pointA.getLongitude(),
    pointB.getLatitude(), pointB.getLongitude())
    * EARTHS_RADIUS[units];
 }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值