searchLocations method

Future<void> searchLocations(
  1. String locationName,
  2. Set<Marker> markers,
  3. LatLng? currentPosition,
  4. GoogleMapController mapController,
  5. dynamic updateStraightDistance(
    1. double
    ),
  6. dynamic updateCenterPosition(
    1. 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');
  }
}