Java對關於兩個地點的根據經緯度算出後排序

來源:互聯網
上載者:User

標籤:根據   eof   功能   res   地址   hash   收貨   div   之間   

/**     * 查詢收貨地址清單     *      * @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; }

 

Java對關於兩個地點的根據經緯度算出後排序

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.