2016-09-24 21 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来获取距离和持续时间以便达到它们。

根据持续时间,使用任何排序技术对数组列表进行排序。

如果你正在飞行,寻找两个经纬度之间的距离直接起作用。

相关问题