diff --git a/lib/src/screens/service.dart b/lib/src/screens/service.dart index 642c6f9..6ec2e74 100644 --- a/lib/src/screens/service.dart +++ b/lib/src/screens/service.dart @@ -110,11 +110,6 @@ class _ServiceScreenState extends State { late GoogleMapController googleMapController; - static CameraPosition initialCameraPosition = const CameraPosition( - target: LatLng(7.8939100, -72.5078200), - zoom: 14.4746, - ); - Set markers = {}; Future _determinePosition() async { @@ -180,12 +175,22 @@ class _ServiceScreenState extends State { } } - Future>>> - getUsersWithActiveStatus() async { - var querySnapshot = await FirebaseFirestore.instance - .collection('users') - .where('estado', isEqualTo: 'activo') - .get(); + Future>>> getUsersWithActiveStatus( + String _serviceType) async { + var querySnapshot; + + if (_serviceType != "Servicio") { + querySnapshot = await FirebaseFirestore.instance + .collection('users') + .where('estado', isEqualTo: 'activo') + .where('profesion', isEqualTo: _serviceType) + .get(); + } else { + querySnapshot = await FirebaseFirestore.instance + .collection('users') + .where('estado', isEqualTo: 'activo') + .get(); + } return querySnapshot.docs; } @@ -199,6 +204,43 @@ class _ServiceScreenState extends State { String _ciudad = '...'; + void _setInitialCameraPosition(String coordsOfCity) { + List coords = coordsOfCity.split(','); + double lat = double.parse(coords[0]); + double lng = double.parse(coords[1]); + + googleMapController.moveCamera( + CameraUpdate.newLatLngZoom( + LatLng(lat, lng), + 14.4746, + ), + ); + } + + void updateMarkersForServiceType(String serviceType) { + getUsersWithActiveStatus(serviceType).then((value) { + markers.clear(); + for (var doc in value) { + final element = doc.data()!; + + if (element['latitude'] != null && element['longitude'] != null) { + markers.add( + Marker( + icon: _markerIcon!, + markerId: MarkerId(doc.id), + position: LatLng( + element['latitude'], + element['longitude'], + ), + ), + ); + } + } + }).catchError((e) { + print('Error al actualizar los marcadores: $e'); + }); + } + @override void initState() { super.initState(); @@ -229,21 +271,8 @@ class _ServiceScreenState extends State { .then((String s) { setState(() { _coordsOfCity = s; + _setInitialCameraPosition(_coordsOfCity); }); - - if (_coordsOfCity != '0.0,0.0') { - List coords = _coordsOfCity.split(','); - double lat = double.parse(coords[0]); - double lng = double.parse(coords[1]); - - // Asegúrate de que googleMapController esté disponible y no sea nulo - googleMapController.moveCamera( - CameraUpdate.newLatLngZoom( - LatLng(lat, lng), - 14.4746, // Puedes ajustar el nivel de zoom según tus necesidades - ), - ); - } }); } @@ -263,27 +292,25 @@ class _ServiceScreenState extends State { _markerIcon = icon; }); }); - try { - getUsersWithActiveStatus().then((value) { - for (var doc in value) { - final element = doc.data()!; - if (element['latitude'] != null && element['longitude'] != null) { - markers.add( - Marker( - icon: _markerIcon!, - markerId: MarkerId(doc.id), - position: LatLng( - element['latitude'], - element['longitude'], - ), - ), - ); - } - } - }); - } catch (e) { - print('error coordenadas $e'); + updateMarkersForServiceType(_serviceType); + } + + static CameraPosition initialCameraPosition = const CameraPosition( + target: LatLng(7.8939100, -72.5078200), + zoom: 14.4746, + ); + + void setInitialCameraPosition(String coordsOfCity) { + if (coordsOfCity != '0.0,0.0') { + List coords = coordsOfCity.split(','); + double lat = double.parse(coords[0]); + double lng = double.parse(coords[1]); + + initialCameraPosition = CameraPosition( + target: LatLng(lat, lng), + zoom: 14.4746, + ); } } @@ -758,6 +785,8 @@ class _ServiceScreenState extends State { _serviceType = serviceType; _serviceTypeController.text = _serviceType; + + updateMarkersForServiceType(_serviceType); }); } },