/** * 查询收货地址列表 * * @param request * @param wechatId * @return */ @RequestMapping("/weixin/address/queryRecentlyAddress") public AddressVo queryRecentlyAddress(HttpServletRequest request, Integer wechatId, AddressVo vo) { String openId = OauthUtil.getInstance().getOpenId(request, String.valueOf(wechatId)); // 查询收货地址 Map<String, Object> map = new HashMap<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具有更具key做自动排序的功能,默认为正序即从小到大 Map<Double,AddressVo> addressMap=new TreeMap<Double,AddressVo>(); for(AddressVo address:aList){ String latitude = address.getLatitude(); String longitude = address.getLongitude(); double distance = Distance(nowLng,nowLat,latitude,longitude); addressMap.put(distance,address); } AddressVo addressVo = addressMap.get(0); return addressVo; }
根据两个点的经纬度算出两个点之间的距离
//根据两个地点的经纬度,计算两的距离
private static final double EARTH_RADIUS = 6378.137;
private static double rad(double d){
return d * Math.PI / 180.0;
}
public double Distance(String nowLng, String nowLat, String latitude, String longitude) { double long1; double lat1; double long2; double lat2; double a, 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(); } return d; }