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. 迟来的Json反序列化

    源码发布 搞了一个下午,终于搞定了这个号称中国的github...以后源码直接在这里发布了(github实在用不来,英文实在太烂了) https://code.csdn.net/jy02305022/ ...

  2. 实现ABP中Person类的权限功能

    菜单项的显示功能已经完全OK了.那么我们就开始制作视图功能吧. 首先测试接口是否正常 我们通过代码生成器将权限和application中大部分功能已经实现了.那么我们来测试下这些接口ok不. 浏览/a ...

  3. 不使用Ajax,如何实现表单提交不刷新页面

    不使用Ajax,如何实现表单提交不刷新页面? 目前,我想到的是使用<iframe>,如果有其他的方式,后续再补. 举个栗子: 在表单上传文件的时候必须设置enctype="mul ...

  4. AngularJs之八

    ***今天讲一下angularJs的路由功能: 一:angularJs路由. 1.AngularJS 路由允许我们通过不同的 URL 访问不同的内容. 2.通过 AngularJS 可以实现多视图的单 ...

  5. dom addeventlistener与id 绑定事件的区别

    文档中有写. //addEventListener() 方法用于向指定元素添加事件句柄. //提示: 使用 removeEventListener() 方法来移除 addEventListener() ...

  6. ASP.NET MVC5+EF6+EasyUI 后台管理系统(42)-工作流设计-表建立

    系列目录 工作流在实际应用中还是比较广泛,网络中存在很多工作流的图形化插件,可以做到拉拽的工作流设计,非常简便,再配合第三方编辑器,可以直接生成表单,我没有刻意的浏览很多工作流的实际设计,我认为工作流 ...

  7. Angular Service入门

    1.Angular内置service Angular为了方便开发者开发,本身提供了非常多的内置服务.可以通过https://docs.angularjs.org/api/ng/service查看Ang ...

  8. 日常css技巧小结(2)-- inline-block带来的迷惑

    一.问题描述 在平时布局中,inline-block使用的频率比很高,主要是因为可以让行标签设置宽高.我在布局过程中,发现了两个“问题”, 1行标签.display:inline-block之后的行标 ...

  9. J2EE 邮件发送那些事儿

    距离自己写的关于java邮件发送的第一篇博客已经有很长一段时间了,现在回过头看看.虽然代码质量方面有待提高,整体结构也不怎样,但是基本思路和过程还是比较纯的.现在有空写写J2EE中邮件发送的开发,实际 ...

  10. 作为一个程序猿,是不是经常会用到.chm文档,但是我们可能会遇到这样那样的问题,比如.chm文档打不开

    .chm文档不能正常打开,一般有两种情形下会造成文档打不开, 1.系统语言栏的语言和文档的语言类别不一同,也就是说比如你的文档是中文版的,但是系统设置的语言是其他国家的.不过一般这种情况很少出现 ,谁 ...