Add distance to map lat lang

dev
suresh 2 years ago
parent a189751141
commit c6a021cc98

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1009 B

After

Width:  |  Height:  |  Size: 28 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.9 KiB

After

Width:  |  Height:  |  Size: 42 KiB

@ -68,7 +68,6 @@ class _DashboardState extends State<Dashboard> {
Widget _dashBoard() { Widget _dashBoard() {
return Container( return Container(
color: screenBackgroundColor, color: screenBackgroundColor,
child: Column( child: Column(

@ -27,6 +27,8 @@ class OrderTrackingPageState extends State<OrderTrackingPage> {
double latitude=0; double latitude=0;
double longitude=0; double longitude=0;
LocationData? currentLocation; LocationData? currentLocation;
Location _location = Location();
String _reachTime = '';
String googleAPiKey ="AIzaSyDJpK9RVhlBejtJu9xSGfneuTN6HOfJgSM"; String googleAPiKey ="AIzaSyDJpK9RVhlBejtJu9xSGfneuTN6HOfJgSM";
@ -40,6 +42,7 @@ class OrderTrackingPageState extends State<OrderTrackingPage> {
@override @override
void initState() { void initState() {
super.initState(); super.initState();
_getLocation();
latitude=widget.lat; latitude=widget.lat;
longitude=widget.lng; longitude=widget.lng;
LatLng endLocation = LatLng(widget.lat,widget.lng); LatLng endLocation = LatLng(widget.lat,widget.lng);
@ -56,6 +59,50 @@ class OrderTrackingPageState extends State<OrderTrackingPage> {
getDirections(endLocation); //fetch direction polylines from Google API getDirections(endLocation); //fetch direction polylines from Google API
} }
Future<void> _getLocation() async {
try {
currentLocation = await _location.getLocation();
} catch (e) {
currentLocation = null;
}
// Calculate ETA using current location and destination coordinates
double eta = _calculateETA( startLocation.latitude, startLocation.longitude,latitude, longitude,);
// Convert ETA to readable format
String hours = eta.floor().toString();
String minutes = ((eta - eta.floor()) * 60).round().toString();
_reachTime = minutes;
}
// Function to calculate ETA
double _calculateETA(double currentLat, double currentLng, double destinationLat, double destinationLng) {
// Algorithm to calculate ETA based on current location, destination coordinates, and traffic conditions
// You can use any algorithm or API to calculate ETA based on your requirements
// Here's an example algorithm:
double distance = _calculateDistance(currentLat, currentLng, destinationLat, destinationLng); // Calculate distance between current location and destination
double speed = 30.0; // Assuming an average speed of 30 km/h
double time = distance / speed; // Calculate time in hours
return time;
}
// Function to calculate distance between two coordinates
double _calculateDistance(double lat1, double lng1, double lat2, double lng2) {
double rad = 0.017453292519943295; // Math.PI / 180
double cosLat1 = cos(lat1 * rad);
double cosLat2 = cos(lat2 * rad);
double sinLat1 = sin(lat1 * rad);
double sinLat2 = sin(lat2 * rad);
double cosLng1 = cos(lng1 * rad);
double cosLng2 = cos(lng2 * rad);
double sinLng1 = sin(lng1 * rad);
double sinLng2 = sin(lng2 * rad);
double c = cosLat1 * cosLat2 * cosLng2 * sinLng1 + cosLat1 * cosLat2 * sinLng2 * cosLng1 + sinLat1 * sinLat2;
double distance = 6371 * acos(c); // 6371 is the radius of the Earth in kilometers
return distance;
}
getDirections(endLocation) async { getDirections(endLocation) async {
markers.clear(); markers.clear();
@ -97,20 +144,6 @@ class OrderTrackingPageState extends State<OrderTrackingPage> {
} }
/* if (result.status == "OK") {
final route = result.routes.first;
final leg = route.legs.first;
final distance = leg.distance?.text ?? '';
final duration = leg.duration?.text ?? '';
print('Distance: $distance');
print('Duration: $duration');
} else {
print('Error: ${result.errorMessage}');
}*/
//polulineCoordinates is the List of longitute and latidtude. //polulineCoordinates is the List of longitute and latidtude.
@ -191,7 +224,6 @@ class OrderTrackingPageState extends State<OrderTrackingPage> {
mapController.complete(controller); mapController.complete(controller);
} }
}, },
), ),
const SizedBox( const SizedBox(
@ -210,6 +242,29 @@ class OrderTrackingPageState extends State<OrderTrackingPage> {
), ),
) )
)), )),
/* Positioned(
bottom: 150,
left: 50,
child: Container(
child: Card(
child: Container(
padding: EdgeInsets.all(20),
child: Column(
children: [
currentLocation != null
? Text('Current location: (${latitude}, ${longitude})')
: Text('Location not available'),
SizedBox(height: 16),
Text('Estimated time of arrival: $_reachTime'),
],
)
),
)
)),*/
], ],
) )

Loading…
Cancel
Save