Java根据两点经纬度计算距离

0
0
0
1. 云栖社区>
2. 博客>
3. 正文

## Java根据两点经纬度计算距离

180°经线。

0°纬线，北纬度的最大值为90°，即北极点；南纬度的最大值为90°，即南极点。

C = sin(MLatA)sin(MLatB)cos(MLonA-MLonB) + cos(MLatA)*cos(MLatB)
Distance = RArccos(C)Pi/180

C = sin(LatA)sin(LatB) + cos(LatA)cos(LatB)*cos(MLonA-MLonB)
Distance = RArccos(C)Pi/180

C = sin(LatAPi/180)sin(LatBPi/180) + cos(LatAPi/180)cos(LatBPi/180)cos((MLonA-MLonB)Pi/180)
Distance = RArccos(C)Pi/180

C = sin(LatA/57.2958)sin(LatB/57.2958) + cos(LatA/57.2958)cos(LatB/57.2958)*cos((MLonA-MLonB)/57.2958)
Distance = RArccos(C) = 6371.004Arccos(C)
kilometer = 0.6213711926371.004Arccos(C)
mile = 3958.758349716768*Arccos(C) mile

/**

• Copyright (C), 2015-2018, 亚信（中国）科技有限公司
• FileName: a
• Author: zhoumoxuan
• Date: 10/8/18 10:35 AM
• Description: e
• History:
• 作者姓名 修改时间 版本号 描述
*/

package com.asiainfo.utils;

/**

• 〈一句话功能简述〉
• 〈e〉
*
• @author zhoumoxuan
• @create 10/8/18
• @since 1.0.0
*/

public class a {

private static final  double EARTH_RADIUS = 6378137;//赤道半径(单位m)

/**
* 转化为弧度(rad)
* */
private static double rad(double d)
{
return d * Math.PI / 180.0;
}

/**
* 基于余弦定理求两经纬度距离
* @param lon1 第一点的精度
* @param lat1 第一点的纬度
* @param lon2 第二点的精度
* @param lat3 第二点的纬度
* @return 返回的距离，单位km
* */
public static double LantitudeLongitudeDist(double lon1, double lat1,double lon2, double lat2) {
double radLat1 = rad(lat1);
double radLat2 = rad(lat2);

double radLon1 = rad(lon1);
double radLon2 = rad(lon2);

if (radLat1 < 0)
radLat1 = Math.PI / 2 + Math.abs(radLat1);// south
if (radLat1 > 0)
radLat1 = Math.PI / 2 - Math.abs(radLat1);// north
if (radLon1 < 0)
radLon1 = Math.PI * 2 - Math.abs(radLon1);// west
if (radLat2 < 0)
radLat2 = Math.PI / 2 + Math.abs(radLat2);// south
if (radLat2 > 0)
radLat2 = Math.PI / 2 - Math.abs(radLat2);// north
if (radLon2 < 0)
radLon2 = Math.PI * 2 - Math.abs(radLon2);// west
double x1 = EARTH_RADIUS * Math.cos(radLon1) * Math.sin(radLat1);
double y1 = EARTH_RADIUS * Math.sin(radLon1) * Math.sin(radLat1);
double z1 = EARTH_RADIUS * Math.cos(radLat1);

double x2 = EARTH_RADIUS * Math.cos(radLon2) * Math.sin(radLat2);
double y2 = EARTH_RADIUS * Math.sin(radLon2) * Math.sin(radLat2);
double z2 = EARTH_RADIUS * Math.cos(radLat2);

double d = Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)+ (z1 - z2) * (z1 - z2));
//余弦定理求夹角
double theta = Math.acos((EARTH_RADIUS * EARTH_RADIUS + EARTH_RADIUS * EARTH_RADIUS - d * d) / (2 * EARTH_RADIUS * EARTH_RADIUS));
double dist = theta * EARTH_RADIUS;
return dist;
}

}

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigDecimal;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

public class LatitudeUtil {

public static final String KEY_1 = "ChwEVlrmoje34iED20piImPc";

/**
* 根据地址查坐标
*
* @param address 地址,格式:深圳市罗湖区火车站
* @return
*/

// @param key   申请ak（即获取密钥），若无百度账号则首先需要注册百度账号。
public static Map<String, String> getGeocoderLatitude(String address) {
BufferedReader in = null;
if (CommonUtil.NotEmpty(key)) { //
return null;
}
try {
address = URLEncoder.encode(address, "UTF-8");
URL tirc = new URL("http://api.map.baidu.com/geocoder?address=" + address + "&output=json&key=" + KEY_1);
in = new BufferedReader(new InputStreamReader(tirc.openStream(), "UTF-8"));
String res;
StringBuilder sb = new StringBuilder("");
while ((res = in.readLine()) != null) {
sb.append(res.trim());
}
String str = sb.toString();
Map<String, String> map = new HashMap<String, String>();
if (StringUtils.isNotEmpty(str)) {
int lngStart = str.indexOf("lng\":");
int lngEnd = str.indexOf(",\"lat");

int latEnd = str.indexOf("},\"precise");
if (lngStart > 0 && lngEnd > 0 && latEnd > 0)

{
String lng = str.substring(lngStart + 5, lngEnd);
String lat = str.substring(lngEnd + 7, latEnd);
map.put("lng", lng);
map.put("lat", lat);
return map;
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}

/**
* 计算地球上任意两点(经纬度)距离
* *
* * @param lon1
* *            第一点经度
* * @param lat1
* *            第一点纬度
* * @param lon2
* *            第二点经度
* * @param lat2
* *            第二点纬度
* * @return 返回距离 单位：千米
*/
public static double getDistatce(
double lon1, double lat1, double lon2, double lat2) {
double R = 6371;
double distance = 0.0;
double dLat = (lat2 - lat1) * Math.PI / 180;
double dLon = (lon2 - lon1) * Math.PI / 180;
double a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + Math.cos(lat1 * Math.PI / 180) * Math.cos(lat2 * Math.PI / 180) * Math.sin(dLon / 2) * Math.sin(dLon / 2);
distance = (2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a))) * R;
return distance;
}

/**
* 计算地球上任意两点(经纬度)距离      *       * @param long1       *            第一点经度      * @param lat1       *            第一点纬度      * @param long2       *            第二点经度      * @param lat2       *            第二点纬度      * @return 返回距离 单位：米
*/
public static double Distance(double long1, double lat1, double long2, double lat2) {
double a, b, R;
R = 6378137; // 地球半径
lat1 = lat1 * Math.PI / 180.0;
lat2 = lat2 * Math.PI / 180.0;
a = lat1 - lat2;
b = (long1 - long2) * Math.PI / 180.0;
double d;
double sa2, sb2;
sa2 = Math.sin(a / 2.0);
sb2 = Math.sin(b / 2.0);
d = 2 * R * Math.asin(Math.sqrt(sa2 * sa2 + Math.cos(lat1) * Math.cos(lat2) * sb2 * sb2));
return d;
}

/**
* 查找一定范围内的经纬度值   * 传入值： 经度 纬度   查找半径(m)   * 返回值：最小经度、纬度，最大经度、纬度   113.957541,22.549392 朗峰大厦
*/
public static Map<String, Double> getAround(Double lon, Double lat, Double raidus) {
Double PI = 3.14159265;    // 圆周率
Double EARTH_RADIUS = 6378137d;
// 地球半径
Double RAD = Math.PI / 180.0;
// 弧度
Double longitude = lon;
//经度
Double latitude = lat;
//纬度
Double degree = (24901 * 1609) / 360.0;
Double raidusMile = raidus;
//距离
Double dpmLat = 1 / degree;
Double radiusLat = dpmLat * raidusMile;
Double minLat = latitude - radiusLat;
//最小纬度
Double maxLat = latitude + radiusLat;
//最大纬度
Double mpdLng = degree * Math.cos(latitude * (PI / 180));
Double dpmLng = 1 / mpdLng;
Double radiusLng = dpmLng * raidusMile;
Double minLng = longitude - radiusLng;
//最小经度
Double maxLng = longitude + radiusLng;   ]
//最大经度
Map<String, Double> m = new HashMap<String, Double>();
m.put("minLng", minLng);
//最小经度
m.put("minLat", minLat);
//最小纬度
m.put("maxLng", maxLng);
//最大经度
m.put("maxLat", maxLat);
//最大纬度
System.err.println("最小经度:" + minLng);
System.err.println("最小纬度:" + minLat);
System.err.println("最大经度:" + maxLng);
System.err.println("最大纬度:" + maxLat);
return m;
}

public static void main(String args[]) {
Map<String, String> json = LatitudeUtil.getGeocoderLatitude("深圳罗湖火车站");
Map<String, String> json = LatitudeUtil.getGeocoderLatitude("tl");
System.out.println("lng : " + json.get("lng"));
System.out.println("lat : " + json.get("lat"));
double d1 = getDistatce(113.993941, 22.596919, 114.156395, 22.581934);
double d2 = Distance(113.993941, 22.596919, 114.156395, 22.581934);
System.out.println("d1 -> " + d1 + ", d2 -> " + d2);
double f = 111.234;
BigDecimal bg = new BigDecimal(f);
double f1 = bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
System.out.println(f1);
}

}

/**

• 经纬距离换算
*/

public class MapDistance {

private static final double PI = 3.14159265;
private static final double EARTH_RADIUS = 6378137;
private static final double RAD = Math.PI / 180.0;
private static Double a;

private static MapDistance mapDistance=null;

public MapDistance(){

}

public static MapDistance getInstance(){
if(mapDistance==null){
mapDistance=new MapDistance();
}

return mapDistance;
}

// @see of-a-Latitude-and-Longitude--65095/
// The circumference of the earth is 24,901 miles.
// 24,901/360 = 69.17 miles / degree
/**
* 根据提供的经度和纬度、以及半径，取得此半径内的最大最小经纬度
*  @param raidus 单位米 *
*  @return minLat,minLng,maxLat,maxLng
*
*/
public static Double[] getAround(Double lat, Double lon, int raidus) {
Double latitude = lat;//纬度
Double longitude = lon;//经度
Double degree = (24901 * 1609) / 360.0;//度
double raidusMile = raidus;//半径
Double dpmLat = 1 / degree;
Double radiusLat = dpmLat * raidusMile;
Double minLat = latitude - radiusLat;
Double maxLat = latitude + radiusLat;
Double mpdLng = degree * Math.cos(latitude * (PI / 180));
Double dpmLng = 1 / mpdLng;
Double radiusLng = dpmLng * raidusMile;
Double minLng = longitude - radiusLng;
Double maxLng = longitude + radiusLng;
System.out.println(minLat+"   "+maxLat+"   "+minLng+"  "+maxLng);
// System.out.println(&quot;[&quot;+minLat+&quot;,&quot;+minLng+&quot;,&quot;+maxLat+&quot;,&quot;+maxLng+&quot;]&quot;);
return new Double[] { minLat,maxLat,minLng,maxLng };
}

/**
* 根据两点间经纬度坐标（double值），计算两点间距离，单位为米 *
* @param lng1 *
* @param lat1
* @param lng2
* @param lat2
* @return
*/
public static Double getDistance(Double lng1, Double lat1, Double lng2, Double lat2) {
double radLat1 = lat1 * RAD;
double radLat2 = lat2 * RAD;
double a = radLat1 - radLat2;
Double b = (lng1 - lng2) * RAD;
Double s = 2 * Math.asin(Math.sqrt(
Math.pow(Math.sin(a / 2), 2) + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
s = s * EARTH_RADIUS;
s = Math.round(s * 10000) / 10000.0;
return s;
}

public static void main(String[] args) {

// Double lat1 = 34.264648;
// Double lon1 = 108.952736;
// int radius = 1000; // [34.25566276027792,108.94186385411045,34.27363323972208,108.96360814588955]
// getAround(lat1, lon1, radius); // 911717.0
//// // 34.264648,108.952736,39.904549,116.407288
//// Double dis = getDistance(108.952736, 34.264648, 116.407288, 39.904549);
//// System.out.println(dis);

System.out.println((0!=0.0));
}

}

+ 关注

corcosa 14024人浏览