I am using flutter application Currently this code is drawing a path that prioritizes the fastest time, but the distance will be longer. What do I need to do to make this code draw the shortest path from point A to point B?
getPoly(change, lat, lng) async {
fmpoly.clear();
if ((userRequestData.isEmpty ||
userRequestData['accepted_at'] == null ||
userRequestData['is_driver_arrived'] == 1) &&
lat == '') {
for (var i = 1; i < addressList.length; i++) {
var api = await http.get(Uri.parse(
'https://routing.openstreetmap.de/routed-car/route/v1/driving/${addressList[i - 1].latlng.longitude},${addressList[i - 1].latlng.latitude};${addressList[i].latlng.longitude},${addressList[i].latlng.latitude}?overview=false&geometries=polyline&steps=true'));
if (api.statusCode == 200) {
// ignore: no_leading_underscores_for_local_identifiers
List _poly = jsonDecode(api.body)['routes'][0]['legs'][0]['steps'];
// String polystring = _poly[5]['geometry'];
polyline.clear();
for (var e in _poly) {
decodeEncodedPolyline(e['geometry']);
// polystring = polystring + _poly[i]['geometry'];
}
double lat = (addressList[0].latlng.latitude +
addressList[addressList.length - 1].latlng.latitude) /
2;
double lon = (addressList[0].latlng.longitude +
addressList[addressList.length - 1].latlng.longitude) /
2;
var val = LatLng(lat, lon);
// if(change == true){
_fmController.move(fmlt.LatLng(val.latitude, val.longitude), 13);
setState(() {});
}
}
} else {
var api = await http.get(Uri.parse(
'https://routing.openstreetmap.de/routed-car/route/v1/driving/$lng,$lat;${addressList[0].latlng.longitude},${addressList[0].latlng.latitude}?overview=false&geometries=polyline&steps=true'));
if (api.statusCode == 200) {
// ignore: no_leading_underscores_for_local_identifiers
List _poly = jsonDecode(api.body)['routes'][0]['legs'][0]['steps'];
polyline.clear();
for (var e in _poly) {
decodeEncodedPolyline(e['geometry']);
}
// ignore: no_leading_underscores_for_local_identifiers
double _lat = (addressList[0].latlng.latitude + lat) / 2;
// ignore: no_leading_underscores_for_local_identifiers
double _lon = (addressList[0].latlng.longitude + lng) / 2;
var val = LatLng(_lat, _lon);
// if(change == true){
_fmController.move(fmlt.LatLng(val.latitude, val.longitude), 15);
// }
setState(() {});
} else {}
}
fmPolyGot = false;
}
New contributor
TU TAM is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.