我有一个问题。我想获得最短路线,并且我使用了这种算法。首先,我收集了所有路线的腿,然后选择了最短的腿。但是我不确定这是否是真的。这是找到最短路线的正确方法吗?因为在和,我手中没有最短的路线对象。我需要路由对象来获取其overview_polyline值。另外,如果我得到最短的路线,如何获得其距离?提前致谢!
DirectionsApi.getDirections(getGeoContext(), origin, destination)
.mode(TravelMode.DRIVING)
.alternatives(true)
.origin("place_id:"+idPlaceOrigem)
.destination("place_id:"+idPlaceDestino)
.units(Unit.METRIC)
.setCallback(new PendingResult.Callback<DirectionsResult>() {
@Override
public void onResult(DirectionsResult result) {
Set<Long> distances = new HashSet<>();
if (result != null && result.routes != null && result.routes.length > 0)
for (DirectionsRoute route : result.routes)
for (DirectionsLeg leg : route.legs)
if (leg.distance != null)
distances.add(leg.distance.inMeters);
if (!distances.isEmpty()) {
ArrayList<Long> sortedList = new ArrayList(distances);
distance = min(sortedList);
despesaRequest.setKm_distancia(distance/1000);
}
runOnUiThread(new Runnable() {
@Override
public void run() {
updateDistance();
Singleton.getInstance().dismissLoading();
}
});
}
@Override
public void onFailure(Throwable e) {
Timber.e(e);
runOnUiThread(() -> {
Toast.makeText(LancamentoODIIActivity.this, "Não foi possível traçar a rota, por favor tenta novamente mais tarde.", Toast.LENGTH_SHORT).show();
Singleton.getInstance().dismissLoading();
});
}
});
}