Customize Consent Preferences

We use cookies to help you navigate efficiently and perform certain functions. You will find detailed information about all cookies under each consent category below.

The cookies that are categorized as "Necessary" are stored on your browser as they are essential for enabling the basic functionalities of the site. ... 

Always Active

Necessary cookies are required to enable the basic features of this site, such as providing secure log-in or adjusting your consent preferences. These cookies do not store any personally identifiable data.

No cookies to display.

Functional cookies help perform certain functionalities like sharing the content of the website on social media platforms, collecting feedback, and other third-party features.

No cookies to display.

Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics such as the number of visitors, bounce rate, traffic source, etc.

No cookies to display.

Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.

No cookies to display.

Advertisement cookies are used to provide visitors with customized advertisements based on the pages you visited previously and to analyze the effectiveness of the ad campaigns.

No cookies to display.

How to Get Current Location (Latitude and Longitude) in Flutter

Tiempo de lectura: < 1 minuto

To quickly and easily get the current location, follow these steps:

First, make sure you have added the geolocator dependency in the pubspec.yaml file:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
geolocator: ^7.0.3
geolocator: ^7.0.3
geolocator: ^7.0.3

Then, run flutter pub get to install the dependency.

Next, add the necessary permissions in the AndroidManifest.xml file:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission></uses-permission>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission></uses-permission>
  

Then implement the following code, which will request location permissions, get the user’s current position, and display the latitude and longitude coordinates on the screen.

To test the following code, run the application on a physical device or emulator that supports location.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
import 'package:flutter/material.dart';import 'package:geolocator/geolocator.dart';void main() {runApp(MyApp());}class MyApp extends StatelessWidget {@overrideWidget build(BuildContext context) {return MaterialApp(home: MyHomePage(),);}}class MyHomePage extends StatefulWidget {@override_MyHomePageState createState() => _MyHomePageState();}class _MyHomePageState extends State {String _locationMessage = '';@overridevoid initState() {super.initState();_getLocation();}Future _getLocation() async {try {// Requests location permissionsLocationPermission permission = await Geolocator.requestPermission();if (permission == LocationPermission.denied) {setState(() {_locationMessage = 'Permission denied';});return;}scssCopy code // Gets the current position Position position = await Geolocator.getCurrentPosition( desiredAccuracy: LocationAccuracy.high, ); setState(() { _locationMessage = 'Lat: ${position.latitude}, Long: ${position.longitude}'; });} catch (e) { setState(() { _locationMessage = 'Error: $e'; });}}@overrideWidget build(BuildContext context) {return Scaffold(appBar: AppBar(title: Text('Get Location'),),body: Center(child: Text(_locationMessage),),);}}
import 'package:flutter/material.dart';import 'package:geolocator/geolocator.dart';void main() {runApp(MyApp());}class MyApp extends StatelessWidget {@overrideWidget build(BuildContext context) {return MaterialApp(home: MyHomePage(),);}}class MyHomePage extends StatefulWidget {@override_MyHomePageState createState() => _MyHomePageState();}class _MyHomePageState extends State {String _locationMessage = '';@overridevoid initState() {super.initState();_getLocation();}Future _getLocation() async {try {// Requests location permissionsLocationPermission permission = await Geolocator.requestPermission();if (permission == LocationPermission.denied) {setState(() {_locationMessage = 'Permission denied';});return;}scssCopy code // Gets the current position Position position = await Geolocator.getCurrentPosition( desiredAccuracy: LocationAccuracy.high, ); setState(() { _locationMessage = 'Lat: ${position.latitude}, Long: ${position.longitude}'; });} catch (e) { setState(() { _locationMessage = 'Error: $e'; });}}@overrideWidget build(BuildContext context) {return Scaffold(appBar: AppBar(title: Text('Get Location'),),body: Center(child: Text(_locationMessage),),);}}
import 'package:flutter/material.dart';import 'package:geolocator/geolocator.dart';void main() {runApp(MyApp());}class MyApp extends StatelessWidget {@overrideWidget build(BuildContext context) {return MaterialApp(home: MyHomePage(),);}}class MyHomePage extends StatefulWidget {@override_MyHomePageState createState() => _MyHomePageState();}class _MyHomePageState extends State {String _locationMessage = '';@overridevoid initState() {super.initState();_getLocation();}Future _getLocation() async {try {// Requests location permissionsLocationPermission permission = await Geolocator.requestPermission();if (permission == LocationPermission.denied) {setState(() {_locationMessage = 'Permission denied';});return;}scssCopy code // Gets the current position Position position = await Geolocator.getCurrentPosition( desiredAccuracy: LocationAccuracy.high, ); setState(() { _locationMessage = 'Lat: ${position.latitude}, Long: ${position.longitude}'; });} catch (e) { setState(() { _locationMessage = 'Error: $e'; });}}@overrideWidget build(BuildContext context) {return Scaffold(appBar: AppBar(title: Text('Get Location'),),body: Center(child: Text(_locationMessage),),);}}

I hope this helps. Have a great day!

0

Leave a Comment