π Get the user's real-time location (latitude, longitude, city, and country) using Flutter and the Geolocator package!
- β Fetch real-time latitude & longitude of the user.
- β Convert coordinates to city, country, and full address.
- β Auto-refresh location on startup & via button press.
- β Cupertino-style loading indicator for a smooth UI experience.
- β Works on Android, iOS, and Web.
1οΈβ£ Clone the repository
git clone https://github.com/your-username/flutter-user-location.git
cd flutter-user-location
2οΈβ£ Install dependencies
flutter pub get
3οΈβ£ Run the app
flutter run
π Android Add the following permissions to your AndroidManifest.xml:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
π iOS Update your Info.plist:
<key>NSLocationWhenInUseUsageDescription</key>
<string>We need your location to show relevant data</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>We need your location even when the app is in the background</string>
π lib/
β£ π main.dart // Entry point of the app
β£ π final_view.dart // Main screen UI & logic
β£ π location_helper.dart // Handles location fetching & processing
πΉ Fetch User Location
Position position = await Geolocator.getCurrentPosition(
desiredAccuracy: LocationAccuracy.high,
);
πΉ Convert Coordinates to City & Country
List<Placemark> placemarks = await placemarkFromCoordinates(
position.latitude, position.longitude,
);
πΉ Display Location in UI
setState(() {
userLocation = 'City: ${place.locality}, Country: ${place.country}';
});
Want to contribute? Feel free to fork this repo and submit a PR! π
β Star this repo if you like it!π¦ Follow me on Twitter for more cool projects!
This project is licensed under the MIT License.