在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. Gitweb 安装与配置

    gitweb 可以通过搭建git服务器将代码保存在git服务器上,多个开发者可以从服务器上clone代码,也可以各自维护一份本地代码,在本地更新之后可以提交到git服务器上,提高开发效率.     g ...

  2. python操作mongodb之四cp数据库

    from pymongo import MongoClient #连接数据库 client=MongoClient('192.168.30.252',27017) #获取现有数据库的名称 client ...

  3. BOM头的来源

    类似WINDOWS自带的记事本等软件,在保存一个以UTF-8编码的文件时,会在文件开始的地方插入三个不可见的字符(0xEF 0xBB 0xBF,即BOM).它是一串隐藏的字符,用于让记事本等编辑器识别 ...

  4. phalcon: eventManager事件管理(结合dispatcher调度控制器)制作简单的acl

    制作简单的acl, dispatcher(专门用来加载或调度或跳转到相应的url地址即XXXcontroller的调度器或控制器,能够在controller执行前对controller进行停止跳转等) ...

  5. wampserver2.5 apache2.4.9:forbidden,本机可以访问,局域网内部能访问。

    wampserver2.5 apache2.4.9:forbidden,本机可以访问,局域网内部能访问. 因为做项目,多人分工,需要局域网内访问各自的项目. 然后安装了wampserver2.5,Ap ...

  6. VB6 GDI+ 入门教程[5] 基础绘图小结

    http://vistaswx.com/blog/article/category/tutorial/page/2 VB6 GDI+ 入门教程[5] 基础绘图小结 2009 年 6 月 18 日 4条 ...

  7. VC++编译libpng

    目录 第1章简介    1 第2章 Visual C++6.0    2 2.1 打开项目    2 2.2 编译宏    3 2.2.1 小结    5 第3章 Visual C++2010     ...

  8. astyle 使用说明

    欢迎关注我的社交账号: 博客园地址: http://www.cnblogs.com/jiangxinnju GitHub地址: https://github.com/jiangxincode 知乎地址 ...

  9. 5.3.1 新建Java工程和类

    1.新建工程 (1)打开Eclipse,选择工作区域 (2)选择“File”->“New”->“Java Project”命令,新建工程. (3)在弹出的新建工程对话框中,输入Projec ...

  10. JAVA中线程同步的方法(7种)汇总

    同步的方法: 一.同步方法 即有synchronized关键字修饰的方法. 由于java的每个对象都有一个内置锁,当用此关键字修饰方法时, 内置锁会保护整个方法.在调用该方法前,需要获得内置锁,否则就 ...