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

 public class DistanceHelper {
/** Names for the units to use */
public final static int KILOMETERS = 0;
public final static int STATUTE_MILES = 1;
public final static int NAUTICAL_MILES = 2; /** Radius of the Earth in the units above */
private final static double EARTHS_RADIUS[] = { 6378.1, // Kilometers
3963.1676, // Statue miles
3443.89849 // Nautical miles
}; /** Conversion factor to convert from degrees to radians */
private static final double DEGREES_TO_RADIANS = (180 / Math.PI); /**
* Calculates the "length" of an arc between two points on a sphere given
* the latitude & longitude of those points.
*
* @param aLat
* Latitude of point A
* @param aLong
* Longitude of point A
* @param bLat
* Latitude of point B
* @param bLong
* Longitude of point B
* @return
*/
private static double calclateArc(double aLat, double aLong, double bLat,
double bLong) {
/*
* Convert location a and b's lattitude and longitude from degrees to
* radians
*/
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;
} /**
* Calculates the distance between two addresses
*
* @param pointA
* GeoPoint of point A
* @param pointB
* GeoPoint of point B
* @param units
* Desired units
* @return Distance between the two points in the desired units
*/
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];
} /**
* Calculates the distance between two locations
*
* @param pointA
* Location of point A
* @param pointB
* Location of point B
* @param units
* Desired units
* @return Distance between the two points in the desired units
*/
public static double calculateDistance(Location pointA, Location pointB,
int units) {
return calclateArc(pointA.getLatitude(), pointA.getLongitude(),
pointB.getLatitude(), pointB.getLongitude())
* EARTHS_RADIUS[units];
}
}

Android google map 两点之间的距离的更多相关文章

  1. sql server2008根据经纬度计算两点之间的距离

    --通过经纬度计算两点之间的距离 create FUNCTION [dbo].[fnGetDistanceNew] --LatBegin 开始经度 --LngBegin 开始维度 --29.49029 ...

  2. 2D和3D空间中计算两点之间的距离

    自己在做游戏的忘记了Unity帮我们提供计算两点之间的距离,在百度搜索了下. 原来有一个公式自己就写了一个方法O(∩_∩)O~,到僵尸到达某一个点之后就向另一个奔跑过去 /// <summary ...

  3. (6)Xamarin.android google map v2

    原文 Xamarin.android google map v2 Google Map v1已经在2013年的3月开始停止支持了,目前若要在你的Android手机上使用到Google Map,就必须要 ...

  4. Android Google Map v2具体解释:开发环境配置

    Android Google Map v2具体解释:开发环境配置                                       --转载请注明出处:coder-pig 说在前面: 说到地 ...

  5. arcgis for js 之 获取两点之间的距离

    换了新公司,接触新行业,半路出家,看着别人的代码,看着api慢慢理解. 需求如下:已知两点坐标求距离. 思路,没有,站在同事的肩膀上踩路子,给的这个链接 https://developers.arcg ...

  6. android google map v1 v2 v3 参考

    V1,V2已经不被推荐使用,谷歌强烈推荐使用V3. 本人在选择时着实纠结了良久,现在总结如下: 对于V1,现在已经申请不到API KEY了,所以不要使用这个版本.这个是网址:https://devel ...

  7. Android Google Map API使用的八个步骤

    本系列教程将分为两部分,第一部分是指导用户使用Mapview控件进行编程,其中包括了如何获得Google Map API,如何使用该API进行简单的开发,如何获得用户当前所在的位置.第二部分则包括如何 ...

  8. js通过经纬度计算两点之间的距离

    最近这几天在做地图的时候,获取到目的地经纬度和当前所在位置的经纬度,通过这几个参数,用js代码就能获取到这两点之间的直线距离: function (lat1, lng1, lat2, lng2) { ...

  9. 利用JS实现的根据经纬度计算地球上两点之间的距离

      最近用到了根据经纬度计算地球表面两点间距离的公式,然后就用JS实现了一下. 计算地球表面两点间的距离大概有两种办法. 第一种是默认地球是一个光滑的球面,然后计算任意两点间的距离,这个距离叫做大圆距 ...

随机推荐

  1. Android_进化史和平台架构介绍

    一.Android平台发展史  2008年9月,谷歌正式发布了Android 1.0系统,全球第一台Android设备HTC (G1)  2009年4月,谷歌正式推出了Android 1.5      ...

  2. SQL数据库基本操作语句

    一.数据库及数据库表定义 1.创建基本表 create table <表名> (<列名><数据类型>[列级完整性约束条件]                      ...

  3. zImage转换为uImage

    写个随笔,备忘! 拿到一个内核后,首先将其make为zImage,步骤: 1.将与要移植的系统的配置文件拷贝给.config cp xxx_xxx .config 2.修改Makefile,指定平台构 ...

  4. angularJS常见问题汇总

    问题描述 解决方案 当你简单的动态给页面插入html时, 此时html带有angular的语法不会执行的. var uploadInfo = '上传成功!<a ng-click="qu ...

  5. CSS3的颜色透明度 ajax请求时loading

    1.background-color:rgba(0,0,0,0.65)         这行代码让覆盖层变黑色,且有0.6的透明度 正常情况下遮盖层会把透明度遗传给弹出框(它的子元素),但是利用CSS ...

  6. Mongoose学习参考文档

    一.快速通道 1.1 名词解释 Schema : 一种以文件形式存储的数据库模型骨架,不具备数据库的操作能力 Model : 由Schema发布生成的模型,具有抽象属性和行为的数据库操作对 Entit ...

  7. Android利用V4包中的SwipeRefreshLayout实现上拉加载

    基本原理 上拉加载或者说滚动到底部时自动加载,都是通过判断是否滚动到了ListView或者其他View的底部,然后触发相应的操作,这里我们以 ListView来说明.因此我们需要在监听ListView ...

  8. win8 卸载IIS

    C:\Windows\System32\inetsrv C:\Windows\iis7.log C:\inetpub

  9. 【Java基础之Object类(一)】Java中Object类中的所有方法(toString、equals、hashCode、clone、finalize、wait和notify等)详解(转载)

    java中的hashcode.equals和toString方法都是基类Object的方法. 首先说说toString方法,简单的总结了下API说明就是:返回该对象的字符串表示,信息应该是简明但易于读 ...

  10. 初学Java之Pattern与Matcher类

    import java.util.regex.*; public class Gxjun{ public static void main(String args[]) { Pattern p; // ...