diff options
| author | Rishi-k-s <rishikrishna.sr@gmail.com> | 2025-09-19 16:27:54 +0530 |
|---|---|---|
| committer | Rishi-k-s <rishikrishna.sr@gmail.com> | 2025-09-19 16:27:54 +0530 |
| commit | 2651c6833c35e2e3d1b13d67c60de0cbc6d61d44 (patch) | |
| tree | 3024fbdccca3f0d39e73f6a07b2e2503fbb01076 | |
| parent | 80a101fa7daa9dac11df0cbb749fa839d8f653c1 (diff) | |
added the Location thing
- made to use the users location as location bais and set the map to that location
- else use bangalore as the default location
| -rw-r--r-- | lib/main.dart | 25 | ||||
| -rw-r--r-- | lib/services/location_service.dart | 49 |
2 files changed, 72 insertions, 2 deletions
diff --git a/lib/main.dart b/lib/main.dart index d579ff9..3a56154 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,6 +1,7 @@ import 'package:flutter/material.dart'; import 'package:google_maps_flutter/google_maps_flutter.dart'; import 'services/places_service.dart'; +import 'services/location_service.dart'; import 'models/place_prediction.dart'; void main() { @@ -31,18 +32,23 @@ class MapSample extends StatefulWidget { class _MapSampleState extends State<MapSample> { late GoogleMapController mapController; - final LatLng _center = const LatLng(-33.86, 151.20); + late LatLng _center; final TextEditingController _fromController = TextEditingController(); final TextEditingController _toController = TextEditingController(); final PlacesService _placesService = PlacesService(); + final LocationService _locationService = LocationService(); Future<Iterable<PlacePrediction>> _getLocationSuggestions(String query) async { if (query.isEmpty) { return const Iterable<PlacePrediction>.empty(); } try { - final predictions = await _placesService.getPlacePredictions(query); + final predictions = await _placesService.getPlacePredictions( + query, + latitude: _center.latitude, + longitude: _center.longitude, + ); return predictions; } catch (e) { print('Error getting predictions: $e'); @@ -51,6 +57,21 @@ class _MapSampleState extends State<MapSample> { } @override + void initState() { + super.initState(); + _center = LocationService.defaultLocation; + _initializeLocation(); + } + + Future<void> _initializeLocation() async { + final location = await _locationService.getCurrentLocation(); + setState(() { + _center = location; + }); + mapController.animateCamera(CameraUpdate.newLatLng(_center)); + } + + @override void dispose() { _fromController.dispose(); _toController.dispose(); diff --git a/lib/services/location_service.dart b/lib/services/location_service.dart new file mode 100644 index 0000000..2747ed5 --- /dev/null +++ b/lib/services/location_service.dart @@ -0,0 +1,49 @@ +import 'package:geolocator/geolocator.dart'; +import 'package:google_maps_flutter/google_maps_flutter.dart'; + +class LocationService { + static const LatLng _defaultLocation = LatLng(12.8539832, 77.7786213); // Bangalore + + Future<LatLng> getCurrentLocation() async { + bool serviceEnabled; + LocationPermission permission; + + // Test if location services are enabled. + serviceEnabled = await Geolocator.isLocationServiceEnabled(); + if (!serviceEnabled) { + return _defaultLocation; + } + + permission = await Geolocator.checkPermission(); + if (permission == LocationPermission.denied) { + permission = await Geolocator.requestPermission(); + if (permission == LocationPermission.denied) { + return _defaultLocation; + } + } + + if (permission == LocationPermission.deniedForever) { + return _defaultLocation; + } + + try { + final position = await Geolocator.getCurrentPosition(); + return LatLng(position.latitude, position.longitude); + } catch (e) { + return _defaultLocation; + } + } + + Future<bool> requestLocationPermission() async { + LocationPermission permission = await Geolocator.checkPermission(); + + if (permission == LocationPermission.denied) { + permission = await Geolocator.requestPermission(); + } + + return permission != LocationPermission.denied && + permission != LocationPermission.deniedForever; + } + + static LatLng get defaultLocation => _defaultLocation; +}
\ No newline at end of file |
