/*** Check Shipping address List * *@paramRequest *@paramWechatid *@return */@RequestMapping ("/weixin/address/queryrecentlyaddress") PublicADDRESSVO queryrecentlyaddress (httpservletrequest request, Integer Wechatid, Addressvo vo) {String openId =oauthutil.getinstance (). Getopenid (Request, string.valueof (Wechatid)); //Check the shipping addressmap<string, object> map =NewHashmap<string, object>(); String NOWLNG= (String) request.getattribute ("Lng"); String Nowlat= (String) request.getattribute ("Lat"); Map.put ("OpenId", openId); List<AddressVo> alist =addressservice.querylist (map);
//treemap has a more key to do automatic sorting function, the default is the positive sequence is small to large Map<Double,AddressVo> addressmap=New TreeMap<Double,AddressVo>(); for(Addressvo address:alist) {String latitude=Address.getlatitude (); String Longitude=Address.getlongitude (); DoubleDistance = Distance(nowlng,nowlat,latitude,longitude); Addressmap.put (distance,address); } ADDRESSVO ADDRESSVO= Addressmap.get (0); returnADDRESSVO; }
Calculates the distance between two points based on the latitude and longitude of the two points
// calculates two distances based on latitude and longitude of two locations
Private static final double Earth_radius = 6378.137;
Private static double rad (double D) {
return d * math.pi/180.0;
}
Public DoubleDistance (String nowlng, String Nowlat, string latitude, string longitude) {Doublelong1; DoubleLAT1; Doublelong2; DoubleLat2; DoubleA, B, sa2, sb2,d = 0; Try{long1=double.valueof (NOWLNG); LAT1=double.valueof (Nowlat); Long2=double.valueof (latitude); LAT2=double.valueof (longitude); LAT1=Rad (LAT1); LAT2=Rad (LAT2); A= LAT1-Lat2; b= Rad (Long1-long2); SA2= Math.sin (a/2.0); SB2= Math.sin (b/2.0); D= 2 *Earth_radius* Math.asin (MATH.SQRT (SA2 * SA2 +Math.Cos (LAT1)* MATH.COS (LAT2) * SB2 *SB2)); } Catch(Exception e) {e.printstacktrace (); } returnD; }
Java calculates post-order based on latitude and longitude for two locations