searchLocations method
Future<void>
searchLocations(
- String locationName,
- Set<Marker> markers,
- LatLng? currentPosition,
- GoogleMapController mapController,
- dynamic updateStraightDistance(
- double
),
- dynamic updateCenterPosition(
- LatLng
),
)
Implementation
Future<void> searchLocations(
String locationName,
Set<Marker> markers,
LatLng? currentPosition,
GoogleMapController mapController,
Function(double) updateStraightDistance,
Function(LatLng) updateCenterPosition,
) async {
try {
List<Location> locations = await searchLocation(locationName);
markers.clear(); // Clear existing markers
if (locations.isNotEmpty) {
for (Location location in locations) {
LatLng searchedLocation =
LatLng(location.latitude, location.longitude);
MapPageController.addMarker(markers, searchedLocation);
// Calculate straight-line distance
if (currentPosition != null) {
double straightLineDistance =
_distanceBetweenLatLng(currentPosition, searchedLocation);
print(
'Straight-line distance to $locationName: ${straightLineDistance.toStringAsFixed(2)} meters');
updateStraightDistance(straightLineDistance);
}
}
Location firstLocation = locations.first;
LatLng searchedLocation =
LatLng(firstLocation.latitude, firstLocation.longitude);
updateCenterPosition(searchedLocation);
mapController
.animateCamera(CameraUpdate.newLatLngZoom(searchedLocation, 15));
MapPageController.addMarker(markers, searchedLocation);
}
} catch (e) {
print('Error searching location: $e');
}
}