2016-09-24 23 views

回答

0

從下面的代碼片段創建Java比較器,然後使用相同的方法對ArrayList進行排序。

示例代碼拿到2個地理位置

private double distanceBetween(final GeoLocation from, final GeoLocation to) { 
    // double earthRadius = 3958.75; // in miles, change to 6371 for 
    // kilometer output 
    final double earthRadius = 6371; 

    final double dLat = Math.toRadians(to.lattitude - from.lattitude); 
    final double dLng = Math.toRadians(to.longitude - from.longitude); 

    final double sindLat = Math.sin(dLat/2); 
    final double sindLng = Math.sin(dLng/2); 

    final double a = Math.pow(sindLat, 2) + Math.pow(sindLng, 2) * Math.cos(Math.toRadians(from.lattitude)) 
     * Math.cos(Math.toRadians(to.lattitude)); 

    final double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)); 

    final double dist = earthRadius * c; 
    return dist; 
} 
0

你需要你的位置之間來計算所有的地理距離點之間的距離。使用Location.distanceTo() API計算如下的距離。

Location loc1 = new Location(""); 
loc1.setLatitude(lat1); 
loc1.setLongitude(lon1); 

Location loc2 = new Location(""); 
loc2.setLatitude(lat2); 
loc2.setLongitude(lon2); 

float distanceInMeters = loc1.distanceTo(loc2); 

然後按距離排序。

0

使用谷歌的Distance Matrix API爲您的目的。

在一個循環內部,根據運輸模式使用距離矩陣API來獲取距離和持續時間以便達到它們。

根據持續時間,使用任何排序技術對數組列表進行排序。

如果你正在飛行,尋找兩個經緯度之間的距離直接起作用。

相關問題