在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. python网络编程socket之多线程

    #coding:utf-8 __author__ = 'similarface' import os,socket,threading,SocketServer SERVER_HOST='localh ...

  2. PCA in Bioinformatics

    光看原理和代码是没用的,得看看具体算法在实际中的用途,多看看文献. Principal component analysis for clustering gene expression data

  3. 在Ubuntu 14 上安装 Nginx-RTMP 流媒体服务器

    一:RTMP RTMP流媒体协议是 一套 Adobe 开发的音频视频实时传输协议: 二:Nginx-rtmp nginx-rtmp 是一个基于nginx的 RTMP服务模块,开源,免费 https:/ ...

  4. 【Unity Shaders】初探Surface Shader背后的机制

    转载请注明出处:http://blog.csdn.net/candycat1992/article/details/39994049 写在前面 一直以来,Unity Surface Shader背后的 ...

  5. jq slideUp slideDown

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  6. Java开发Maven环境配置和介绍

    最近很火热的12306的订票软件go-home,我也下载了一份下来了,使用了一下,也从svn中把代码down下来了,但是在eclipse中竟然出错了,依赖的jar包都没有找到,后来才知道人家是用mav ...

  7. edm注意细节

    Email Direct Marketing不要有js,css也放在html里面不能有热区,所有的border要设置为0在浏览器里面邮件发送可能会歪掉,可以测试接受后是否歪掉 有点时候表格会有细缝等等 ...

  8. HDU----(4549)M斐波那契数列(小费马引理+快速矩阵幂)

    M斐波那契数列 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Sub ...

  9. Android存储数据方式

    可以查看Android开发文档中的:/docs/guide/topics/data/data-storage.html Android provides several options for you ...

  10. 教你如何用PS制作多款按钮UI设计教程

    教你如何用PS制作多款按钮UI设计教程 本文教大家制作按钮的方法 LV. ★ 初入设计,学做按钮.只会套个底色,加个阴影,字体纯白,小聪明的弄个圆角. LV. ★★(描边.字体.内阴影) 看了很多案例 ...