Java calculates post-order based on latitude and longitude for two locations

Source: Internet
Author: User
Tags cos openid sin

/*** 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.