Skip to content

Instantly share code, notes, and snippets.

@Emeka-Chukwu
Created September 13, 2021 07:11
Show Gist options
  • Save Emeka-Chukwu/39f04b14fe548e1a7022342a108d0c66 to your computer and use it in GitHub Desktop.
Save Emeka-Chukwu/39f04b14fe548e1a7022342a108d0c66 to your computer and use it in GitHub Desktop.
getAllTheDistanceCover(String addressOne, String addressTwo) async {
isCalculating = true;
update();
List<Location> startAddress =
await locationFromAddress(addressOne, localeIdentifier: "en");
List<Location> destinationAddress =
await locationFromAddress(addressTwo, localeIdentifier: "en");
await _createPolylines(startAddress[0], destinationAddress[0]);
double sum = totalDistanceCovered();
isCalculating = false;
distanceCal = sum;
totalDistance = sum.ceil();
logisticsTotal(sum);
update();
}
// Sets the delivery type and update the UI
void selectDeliveryType(bool value) {
isRegular = value;
if (isRegular) {
service.deliveryType = "Regular";
} else {
service.deliveryType = "Premium";
}
logisticsType();
update();
}
_createPolylines(Location start, Location destination) async {
// Initializing PolylinePoints
polylinePoints = PolylinePoints();
// Generating the list of coordinates to be used for
// drawing the polylines
PolylineResult result = await polylinePoints.getRouteBetweenCoordinates(
ApiKeys.MapsKey, // Google Maps API Key
PointLatLng(start.latitude, start.longitude),
PointLatLng(destination.latitude, destination.longitude),
travelMode: TravelMode.driving,
);
if (result.points.isNotEmpty) {
polylineCoordinates = [];
result.points.forEach((PointLatLng point) {
polylineCoordinates.add(LatLng(point.latitude, point.longitude));
});
update();
}
}
double _coordinateDistance(lat1, lon1, lat2, lon2) {
var p = 0.017453292519943295;
var c = cos;
var a = 0.5 -
c((lat2 - lat1) * p) / 2 +
c(lat1 * p) * c(lat2 * p) * (1 - c((lon2 - lon1) * p)) / 2;
return 12742 * asin(sqrt(a));
}
double totalDistanceCovered() {
double totalDistance = 0;
for (int i = 0; i < polylineCoordinates.length - 1; i++) {
totalDistance += _coordinateDistance(
polylineCoordinates[i].latitude,
polylineCoordinates[i].longitude,
polylineCoordinates[i + 1].latitude,
polylineCoordinates[i + 1].longitude,
);
}
return totalDistance;
}
void logisticsTotal(double distance) {
totalPrice = (flatRateValue * logBaseValue).toDouble();
print("totalPrice");
if (distance > logBaseValue) {
totalPrice = (flatRateValue * logBaseValue).toDouble() +
((distance.ceil().toInt() - logBaseValue) * logNextPrice);
}
if (!isRegular) {
totalPrice = totalPrice + premiumCharge;
}
update();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment