計算兩個經緯度之間的距離

這個算是自己保存的一個小demo吧,方便以後使用,不用再度娘上到處亂找。

先從方法上接收經緯度的參數:latitude和longitude,然後直接套用下面計算。s就是距離,單位是(米),若要單位修改爲公里,將s*1000修改爲s*10000即可


			Map map2 = new HashMap();
                    	 //將傳入進來的經緯度參數,轉換爲Double類型,方便計算。
                    	 Double double_latitude = new Double(latitude);
                    	 Double double_longitude = new Double(longitude);
                    	 
                    	 double radLat1  = double_latitude * Math.PI / 180.0;
                    	 double radLat2  = (store_list.get(i).getStore_lat()).doubleValue()* Math.PI / 180.0;
                    	 double a = radLat1 - radLat2;
                    	 double b = double_longitude* Math.PI / 180.0 - (store_list.get(i).getStore_lng()).doubleValue()* Math.PI / 180.0;
                    	 double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2)  + Math.cos(radLat1) * Math.cos(radLat2)  
                    			    * Math.pow(Math.sin(b / 2), 2)));
                    	 
                    	 s = s * r;  
                    	 s = Math.round(s * 1000);  
                         map2.put("store_farAway", s);


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章