public class Test{
private static List<LocalAddress> ilist = new ArrayList<LocalAddress>();
public static void main(String[] args) {
Test test3 = new Test();
Double localDouble = 0.0;
//定義一個二維數組存放經緯度
Double[][] doubles = {
{ 22.6036906766, 113.8793209706 }, //雍華源
{ 22.5205569549, 113.9394272419 }, //西海灣花園
{ 22.6494305358, 114.0411629507 }, //世紀春城4期
{ 22.5255080247,114.0384880750 }, //金海灣花園
{ 22.5246432654,114.0720634923 }, //金港豪庭
{ 22.5963291708,113.9689558477 }, //得意名居
{ 22.5509638661,113.9358124450 }, //麒麟花園A區西門
{ 22.5509638661,113.9358124450 }, //麒麟花園A區北門
{ 22.5254496086,114.0555439122 }, //裕康時尚名居
{ 22.7789489191,114.3063672776 }, //桑泰丹華園
{ 22.5240537775,114.0641924822 }, //皇庭彩園
{ 22.5141408858,114.0624887496 } }; //城市3米6
//門店的經緯度
Double[] initlocal = {22.539899298946577,113.95296375395431 };
for (int i = 0; i < doubles.length; i++) {
System.out.println("doubles.length==============="+doubles.length);
System.out.println("(" + doubles[i][0] + "," + doubles[i][1] + ")");
Double z = test3.getDistance(doubles[i][0], doubles[i][1],initlocal[0], initlocal[1]);
System.out.println(z);
//獲取最短距離后把經緯度和距離存放到對象中
LocalAddress localaddress = new LocalAddress(doubles[i][0], doubles[i][1], z);
//將對象用list保存
ilist.add(localaddress);
}
List<LocalAddress> shotlocal = getLocalList();
Double lat=shotlocal.get(0).getLat();
Double lon= shotlocal.get(0).getLon();
localDouble = shotlocal.get(0).getDistance();
System.err.println("最近的距離:" + localDouble + "。對應的經緯是:" +"(" + lat + "," + lon + ")");
}
/*第一種求最近距離 ===================================================*/
/**
* 獲取最短的距離
* @param lat
* @param lon
* @param initlat
* @param initlon
* @return
*/
public Double GetShotLocal(Double lat, Double lon, Double initlat,Double initlon) {
Double x = (initlat - lat) * (initlat - lat);
Double y = (initlon - lon) * (initlon - lon);
Double z = Math.sqrt(x + y);
return z;
}
/**
* 對List<LocalAddress> 進行排序
* @return
*/
public static List<LocalAddress> getLocalList() {
Collections.sort(ilist, new Comparator<LocalAddress>() {
@Override
public int compare(LocalAddress arg0, LocalAddress arg1) {
Double double1 = arg0.getDistance();
Double double2 = arg1.getDistance();
if(double1>double2){
return 1;
}else if (double1 == double2) {
return 0;
}else {
return -1;
}
}
});
return ilist;
}
/* 第二種求最近距離 ==================================================================*/
private static final double EARTH_RADIUS = 6378.137;//地球半徑,單位千米
private static double rad(double d)
{
return d * Math.PI / 180.0;
}
/**
*
* @param lat1 第一個緯度
* @param lng1 第一個經度
* @param lat2 第二個緯度
* @param lng2 第二個經度
* @return 兩個經緯度的距離
*/
public static double getDistance(double lat1,double lng1,double lat2,double lng2)
{
double radLat1 = rad(lat1);
double radLat2 = rad(lat2);
double a = radLat1 - radLat2;
double b = rad(lng1) - rad(lng2);
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;
return s;
}
}
entity
public class LocalAddress {
private Double lat; //緯度
private Double lon; //經度
private Double distance; //最近距離
public LocalAddress() {
// TODO Auto-generated constructor stub
}
public LocalAddress(Double lat, Double lon, Double distance) {
super();
this.lat = lat;
this.lon = lon;
this.distance = distance;
}
}
<!--根據經緯度查詢最近距離范圍-->
<select id="SelectDistanceByLat" parameterType="map" resultType="map">
SELECT
*,(
6371 * acos (
cos ( radians(#{lat}) )
* cos( radians( lat ) )
* cos( radians( lng ) - radians(#{lng}) )
+ sin ( radians(#{lat}) )
* sin( radians( lat ) )
)
) AS distance
FROM machine
HAVING 3 > distance
ORDER BY distance
LIMIT 0 , 20;
</select>