在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. JavaScript的事件对象_概述/this

    JavaScript 事件的一个重要方面是它们拥有一些相对一致的特点,可以给你的开发提供更多的强大功能. 最方便和强大的就是事件对象,他们可以帮你处理鼠标事件和键盘敲击方面的情况,此外还可以修改一般事 ...

  2. 选择排序算法Java与Python实现

    Java 实现 package common; public class SimpleArithmetic { /** * 选择排序 * 输入整形数组:a[n] [4.5.3.7] * 1. 取数组编 ...

  3. idea14教程

    破解:http://blog.csdn.net/guofeng526/article/details/47043457 使用教程: http://www.phperz.com/article/15/0 ...

  4. C\C++头文件说明

    C\C++编程时候经常会遇到头文件问题而出现一系列的调试错误,下面我就简要的举例介绍一下头文件的作用,我们知道一个C\C++ 程序中开头一般都为: #include<iostream.h> ...

  5. Machine Learning – 第2周(Linear Regression with Multiple Variables、Octave/Matlab Tutorial)

    Machine Learning – Coursera Octave for Microsoft Windows GNU Octave官网 GNU Octave帮助文档 (有900页的pdf版本) O ...

  6. js文字上下滚动代码

    <div id="dome"> <div id="dome1"> <ul class="express"> ...

  7. 如何增加Asp.Net Core生成的模板网站中用户信息表中的列(AspNetUsers)

    环境: 1.VS2015 Community 14.0.25431.01 Update 3; 2.其他环境(具体哪一个影响不太清楚,都列在这儿) 使用的系统模板 利用系统提供的模板,并选择个人身份验证 ...

  8. jQuery clone()方法绑定事件

    先看如下代码: (function ($) { var div = $("<div></div>").css({width: "100px&quo ...

  9. h5新增标签兼容性

    <address> 标签定义文档或文章的作者/拥有者的联系信息.  兼容所有浏览器 <area> 标签定义图像映射中的区域(注:图像映射指得是带有可点击区域的图像).兼容所有浏 ...

  10. [Java] Java 获取数据库所有表基本信息和表中的所有列基本信息代码

    废话不多说.上代码 import java.io.IOException; import java.io.PrintWriter; import java.sql.Connection; import ...