DEV Community

Cover image for Flutter Location Tracker - Track Current Location
rrtutors
rrtutors

Posted on • Originally published at rrtutors.com

Flutter Location Tracker - Track Current Location

Tutorial we will learn how to use Google Maps in Flutter and track location by retrive current location from the device using Flutter Location package and display it on Google Maps using Marker Icon. This example track the liva location of the user and display on device on every 1 second. For this example we will use two packages Google Maps and Location packages. Check my previous example Integrate Google Maps in Flutter

What we will cover

Load Google Maps
Disply Marker on Maps
Fetch Current Location
Track Location and Update on Maps
Enter fullscreen mode Exit fullscreen mode

Add required packages in pubspec.yaml file and run flutter pub add

dev_dependencies:
flutter_test:
sdk: flutter
location: ^4.3.0
google_maps_flutter: ^2.0.6
Enter fullscreen mode Exit fullscreen mode

Now lets open Android directory if you are going to be implementing for Android and if you are going to implement ios relevant iOS side and read package description how to setup native side. Here we will be implementing Android side in this tutorial

Now add required permissions in the manifest file

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
Enter fullscreen mode Exit fullscreen mode

Add Maps Api Key in meta data

<meta-data android:name="com.google.android.geo.API_KEY"
android:value=""/>
Enter fullscreen mode Exit fullscreen mode

Refer my previous post about How to fetch Maps API key

Load Google Maps

Let's add below code to load Google Maps

GoogleMap(

  initialCameraPosition: _kGooglePlex,
  onMapCreated: (GoogleMapController controller) {
    _controller.complete(controller);
  },

),
Enter fullscreen mode Exit fullscreen mode

Fetch Current Location
Now we are able to load Google Maps on device. Now we need to fetch the current location of the device. To fetch current location we need to take location permissions and check the Enable Location services in the device.

Check the Location services enabled by

initPlatformState() async {
  await _locationService.changeSettings(accuracy: LocationAccuracy.high, interval: 1000);

  LocationData? locationData;
  try {
    bool serviceEnabled = await _locationService.serviceEnabled();
    if (serviceEnabled) {
      PermissionStatus p;

      _permissionGranted = (await _locationService.requestPermission());
      if (_permissionGranted!.index==0) {
        locationData = await _locationService.getLocation();

      }
    }
    else {
      bool serviceRequestGranted = await _locationService.requestService();
      if(serviceRequestGranted) {
        initPlatformState();
      }
    }
  }
  on PlatformException catch (e) {
    print(e);
    if (e.code == 'PERMISSION_DENIED') {
      error = e.message!;
    }
    else if (e.code == 'SERVICE_STATUS_ERROR') {
      error = e.message!;
    }

  }


}
Enter fullscreen mode Exit fullscreen mode

Download Complete code for Flutter Track Current Location of the Device

Flutter Track Current Location of the Device

Discussion (0)