According to a recent query from the latitude and longitude, mysql query range near latitude and longitude

the Test class {public 

Private static List <LocalAddress> = new new IList the ArrayList <LocalAddress> ();
public static void main (String [] args) {
the Test = new new Test3 the Test ();
Double localDouble = 0.0;

// define a two-dimensional array to store latitude and longitude
Double [] [] Doubles = {
{22.6036906766, 113.8793209706} // Yong Huayuan
{22.5205569549, 113.9394272419} // West Bay gardens
{22.6494305358, 114.0411629507} // century Spring City 4
{22.5255080247,114.0384880750} / / Golden Bay garden
{22.5246432654,114.0720634923}, // Heights Golden Port
{22.5963291708,113.9689558477}, // proud Mingju
{22.5509638661,113.9358124450}, // A garden area Simon Kirin
22.5509638661,113.9358124450 {} // Kirin Garden Area A North Gate
{22.5254496086,114.0555439122} // Hiroyasu fashion Mingju
{22.7789489191,114.3063672776} // Sangtai Dan Garden
{22.5240537775,114.0641924822} // Wong Choi Ting Park
{22.5141408858,114.0624887496}}; // 6 cities 3 m

@ stores the latitude and longitude
Double [] = {22.539899298946577,113.95296375395431 initlocal};


for (int I = 0; I <doubles.length; I ++) {
the System.out. the 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);
// After acquiring the latitude and longitude and the shortest distance to the subject distance stored in
LocalAddress localAddress = new new LocalAddress (Doubles [I] [0], Doubles [I] [. 1], Z);
// the object with the list stored
IList.Add (localAddress);
}

List <localAddress> shotlocal getLocalList = ();
Double shotlocal.get LAT = (0) .getLat ();
Double LON = shotlocal.get (0) .getLon () ;
localDouble = shotlocal.get (0) .getDistance ();
System.err.println ( "closest distance:" + localDouble + ". corresponding to the latitude and longitude is:" + "(" + lat + "," + lon + ")");
}

/ * Find the first shortest distance ========================================= ========== * /
/ **
* Gets the shortest distance
* @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) {
Double1 = arg0.getDistance double ();
double double2 arg1.getDistance = ();
IF (Double1> double2) {
return. 1;
} the else IF (Double1 == double2) {
return 0;
} the else {
return -1;
}
}
});
return IList;
}

/ * find second closest distance =================================== * =============================== /
private static Double Final EARTH_RADIUS = 6378.137; // Earth radius in one thousand meters
private Double RAD static (Double D)
{
return Math.PI D * / 180.0;
}

/ **
*
* @param LAT1 first latitude
* @param lng1 first longitude
* @param lat2 second latitude
* @param lng2 second longitude
from latitude and longitude of two * @return
* /
public static Double getDistance (Double LAT1, Double LNG 1, Double LAT2, Double lng2)
{
Double radLat1 = RAD (LAT1);
Double radLat2 = RAD (LAT2);
Double A = radLat1 - radLat2;
Double B = RAD (LNG 1) - RAD (lng2) ;

Double Math.asin S = 2 * (the Math.sqrt (Math.pow (Math.sin, (A / 2), 2) +
Math.cos (radLat1) * Math.cos (radLat2) * Math.pow (the 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>

Guess you like

Origin www.cnblogs.com/tomingto/p/11102240.html