package xxx.driver.business.utils;

/**
* <p>Represents a point on the surface of a sphere. (The Earth is almost
* spherical.)</p>
*
* <p>To create an instance, call one of the static methods fromDegrees() or
* fromRadians().</p>
*
* <p>This code was originally published at
* <a href="http://JanMatuschek.de/LatitudeLongitudeBoundingCoordinates#Java">
* http://JanMatuschek.de/LatitudeLongitudeBoundingCoordinates#Java</a>.</p>
*
* @author Jan Philip Matuschek
* @version 22 September 2010
*/
public class GeoLocation {
private double radLat; // latitude in radians
private double radLon; // longitude in radians private double degLat; // latitude in degrees
private double degLon; // longitude in degrees private static final double EARTHS_RADIUS_KM = 6371.01;
private static final double EARTHS_RADIUS_M = 3958.762079; public static final int UNIT_KM = 0;
public static final int UNIT_M = 1; private static final double MIN_LAT = Math.toRadians(-90d); // -PI/2
private static final double MAX_LAT = Math.toRadians(90d); // PI/2
private static final double MIN_LON = Math.toRadians(-180d); // -PI
private static final double MAX_LON = Math.toRadians(180d); // PI private GeoLocation () {
} /**
* @param latitude the latitude, in degrees.
* @param longitude the longitude, in degrees.
*/
public static GeoLocation fromDegrees(double latitude, double longitude) {
GeoLocation result = new GeoLocation();
result.radLat = Math.toRadians(latitude);
result.radLon = Math.toRadians(longitude);
result.degLat = latitude;
result.degLon = longitude;
result.checkBounds();
return result;
} /**
*
* @param unit 0 km || kilometers; 1 m || miles
* @return
*/
protected double getEarthsRadius(int unit) {
return unit == UNIT_KM?EARTHS_RADIUS_KM:EARTHS_RADIUS_M;
} /**
* @param latitude the latitude, in radians.
* @param longitude the longitude, in radians.
*/
public static GeoLocation fromRadians(double latitude, double longitude) {
GeoLocation result = new GeoLocation();
result.radLat = latitude;
result.radLon = longitude;
result.degLat = Math.toDegrees(latitude);
result.degLon = Math.toDegrees(longitude);
result.checkBounds();
return result;
} private void checkBounds() {
if (radLat < MIN_LAT || radLat > MAX_LAT ||
radLon < MIN_LON || radLon > MAX_LON)
throw new IllegalArgumentException();
} /**
* @return the latitude, in degrees.
*/
public double getLatitudeInDegrees() {
return degLat;
} /**
* @return the longitude, in degrees.
*/
public double getLongitudeInDegrees() {
return degLon;
} /**
* @return the latitude, in radians.
*/
public double getLatitudeInRadians() {
return radLat;
} /**
* @return the longitude, in radians.
*/
public double getLongitudeInRadians() {
return radLon;
} @Override
public String toString() {
return "(" + degLat + "\u00B0, " + degLon + "\u00B0) = (" +
radLat + " rad, " + radLon + " rad)";
} /**
* Computes the great circle distance between this GeoLocation instance
* and the location argument.
* @param radius the radius of the sphere, e.g. the average radius for a
* spherical approximation of the figure of the Earth is approximately
* 6371.01 kilometers.
* @return the distance, measured in the same unit as the radius
* argument.
*/
public double distanceTo(GeoLocation location, double radius) {
return Math.acos(Math.sin(radLat) * Math.sin(location.radLat) +
Math.cos(radLat) * Math.cos(location.radLat) *
Math.cos(radLon - location.radLon)) * radius;
} /**
*
* @param location
* @param unit
* @return
*/
public double distanceToByUnit(GeoLocation location, int unit) {
return distanceTo(location, getEarthsRadius(unit));
} /**
* <p>Computes the bounding coordinates of all points on the surface
* of a sphere that have a great circle distance to the point represented
* by this GeoLocation instance that is less or equal to the distance
* argument.</p>
* <p>For more information about the formulae used in this method visit
* <a href="http://JanMatuschek.de/LatitudeLongitudeBoundingCoordinates">
* http://JanMatuschek.de/LatitudeLongitudeBoundingCoordinates</a>.</p>
* @param distance the distance from the point represented by this
* GeoLocation instance. Must me measured in the same unit as the radius
* argument.
* @param radius the radius of the sphere, e.g. the average radius for a
* spherical approximation of the figure of the Earth is approximately
* 6371.01 kilometers.
* @return an array of two GeoLocation objects such that:<ul>
* <li>The latitude of any point within the specified distance is greater
* or equal to the latitude of the first array element and smaller or
* equal to the latitude of the second array element.</li>
* <li>If the longitude of the first array element is smaller or equal to
* the longitude of the second element, then
* the longitude of any point within the specified distance is greater
* or equal to the longitude of the first array element and smaller or
* equal to the longitude of the second array element.</li>
* <li>If the longitude of the first array element is greater than the
* longitude of the second element (this is the case if the 180th
* meridian is within the distance), then
* the longitude of any point within the specified distance is greater
* or equal to the longitude of the first array element
* <strong>or</strong> smaller or equal to the longitude of the second
* array element.</li>
* </ul>
*/
public GeoLocation[] boundingCoordinates(double distance, double radius) { if (radius < 0d || distance < 0d)
throw new IllegalArgumentException(); // angular distance in radians on a great circle
double radDist = distance / radius; double minLat = radLat - radDist;
double maxLat = radLat + radDist; double minLon, maxLon;
if (minLat > MIN_LAT && maxLat < MAX_LAT) {
double deltaLon = Math.asin(Math.sin(radDist) /
Math.cos(radLat));
minLon = radLon - deltaLon;
if (minLon < MIN_LON) minLon += 2d * Math.PI;
maxLon = radLon + deltaLon;
if (maxLon > MAX_LON) maxLon -= 2d * Math.PI;
} else {
// a pole is within the distance
minLat = Math.max(minLat, MIN_LAT);
maxLat = Math.min(maxLat, MAX_LAT);
minLon = MIN_LON;
maxLon = MAX_LON;
} return new GeoLocation[]{fromRadians(minLat, minLon),
fromRadians(maxLat, maxLon)};
}
}

Java 根据经纬度计算两点之间的距离的更多相关文章

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

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

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

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

  3. JAVA通过经纬度获取两点之间的距离

    private static double EARTH_RADIUS = 6378.137; private static double rad(double d) { return d * Math ...

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

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

  5. TSQL 根据经纬度计算两点间的距离;返回米(m)

    -- ============================================= -- Author:Forrest -- Create date: 2013-07-16 -- Des ...

  6. php根据地球上任意两点的经纬度计算两点间的距离 原理

    地球是一个近乎标准的椭球体,它的赤道半径为6378.140千米,极半径为6356.755千米,平均半径6371.004千米.如果我们假设地球是一个完美的球体,那么它的半径就是地球的平均半径,记为R.如 ...

  7. 经纬度计算两点间的距离,根据距离排序SQL

    #java的Utilspublic class DistanceUtil { // 地球平均半径 private static final double EARTH_RADIUS = 6378137; ...

  8. IOS计算两点之间的距离

    //广州经纬度 CLLocationCoordinate2D guangZhouLocation; guangZhouLocation.latitude = 23.20; guangZhouLocat ...

  9. mysql oracle计算两点之间的距离

    mysql函数: SET FOREIGN_KEY_CHECKS=0; DROP FUNCTION IF EXISTS `getDistance`;DELIMITER ;;CREATE DEFINER= ...

随机推荐

  1. myeclipse学习总结二(myeclipse安装svn插件)

    1.在myeclipse中要安装svn插件,首先是下载svn插件. 下载地址:http://subclipse.tigris.org/servlets/ProjectDocumentList?fold ...

  2. SpringMVC+MyBatis整合——事务管理

    项目一直没有做事务管理,这几天一直在想着解决这事,今天早上终于解决了.接下来直接上配置步骤. 我们项目采用的基本搭建环境:SpringMVC.MyBatis.Oracle11g.WebLogic10. ...

  3. mysql大小写敏感与校对规则

    大家在使用mysql过程中,可能会遇到类似一下的问题: root@chuck 07:42:00>select * from test where c1 like 'ab%';  +-----+  ...

  4. C# 设置Excel打印选项及打印excel文档

    C# 设置Excel打印选项及打印excel文档 打印Excel文档是一个很常见的操作,但有时候我们会碰到各种不同的打印需求,例如只打印一个Excel工作表的其中一部分,或打印时每页都有表头,或把工作 ...

  5. React.js深入学习详细解析

    今天,继续深入学习react.js. 目录: 一.JSX介绍 二.React组件生命周期详解 三.属性.状态的含义和用法 四.React中事件的用法 五.组件的协同使用 六.React中的双向绑定   ...

  6. Android面试经验 -- 乐视

    此次投的是三年经验的Android开发,最后反而因为自己的失误,没有准备充分而导致结果很悲剧,以此告诫自己千万不能疏忽大意. 面试过程 第一次去大公司面试,心里不是一般的激动和紧张,来到乐视大厦门口, ...

  7. Python爬虫小白入门(二)requests库

    一.前言 为什么要先说Requests库呢,因为这是个功能很强大的网络请求库,可以实现跟浏览器一样发送各种HTTP请求来获取网站的数据.网络上的模块.库.包指的都是同一种东西,所以后文中可能会在不同地 ...

  8. WPF 虚拟化 VirtualizingWrapPanel 和 VirtualLizingTilePanel

    一. UI  上两个扩展 public class VirtualizingWrapPanel : VirtualizingPanel, IScrollInfo { #region Fields UI ...

  9. 异步编程系列第01章 Async异步编程简介

    p { display: block; margin: 3px 0 0 0; } --> 2016.10.11补充 三个月过去了,回头来看,我不得不承认这是一系列失败的翻译.过段时间,我将重新翻 ...

  10. easyUI属性汇总

    CSS类定义: 1.div easyui-window 生成一个window窗口样式. 属性如下: 1)modal:是否生成模态窗口.true[是] false[否] 2)shadow:是否显示窗口阴 ...