2017-04-07 53 views

回答

0

如果您知道您當前的LatLong或您的目標位置lat,則可以使用以下方法找到Distance。

public double findDistance(LatLng fromLatLan, LatLng toLatLan) { 
     int earthRadious = 6371;// radius of earth in Km 
     double latFrom = fromLatLan.latitude; 
     double latTo = toLatLan.latitude; 
     double lonFrom = fromLatLan.longitude; 
     double lonTo = toLatLan.longitude; 

     double diffLat = Math.toRadians(latTo - latFrom); 
     double diffLon = Math.toRadians(lonTo - lonFrom); 

     double aValue = Math.sin(diffLat/2) * Math.sin(diffLat/2) 
       + Math.cos(Math.toRadians(latFrom)) 
       * Math.cos(Math.toRadians(latTo)) * Math.sin(diffLon/2) 
       * Math.sin(diffLon/2); 

     double c = 2 * Math.asin(Math.sqrt(aValue)); 
     double results = earthRadious * c; 

     double kms = results/1; 
     DecimalFormat newFormat = new DecimalFormat("####"); 
     int killoMeter = Integer.valueOf(newFormat.format(kms)); 

     double meter = results % 1000; 
     int metersDistance = Integer.valueOf(newFormat.format(meter)); 


     Log.i("Value", "" + results + " KilloMeter " + killoMeter 
       + " Meter " + metersDistance); 

     return earthRadious * c; 
    } 
+0

謝謝,但是如何顯示KM在活動中的距離?以及我應該使用哪個按鈕?如何鏈接按鈕以直接從谷歌地圖獲取距離?我在Android新,所以請幫助我。 – Ankit

相關問題