Hassen KHELIFI
Hassen KHELIFI

Reputation: 21

how to get current position from mapbox in Flutter

As i am using MapBox in my application .i don't know how to get the current position to open Navigation_MapBox for start navigation with the current position as origin to the destination .thanks

Upvotes: 2

Views: 2768

Answers (2)

Mohi Dev
Mohi Dev

Reputation: 3406

To get user location you need to run this

    import 'package:geolocator/geolocator.dart';

/// Determine the current position of the device.
///
/// When the location services are not enabled or permissions
/// are denied the `Future` will return an error.
Future<Position> _determinePosition() async {
  bool serviceEnabled;
  LocationPermission permission;

  // Test if location services are enabled.
  serviceEnabled = await Geolocator.isLocationServiceEnabled();
  if (!serviceEnabled) {
    // Location services are not enabled don't continue
    // accessing the position and request users of the 
    // App to enable the location services.
    return Future.error('Location services are disabled.');
  }

  permission = await Geolocator.checkPermission();
  if (permission == LocationPermission.denied) {
    permission = await Geolocator.requestPermission();
    if (permission == LocationPermission.denied) {
      // Permissions are denied, next time you could try
      // requesting permissions again (this is also where
      // Android's shouldShowRequestPermissionRationale 
      // returned true. According to Android guidelines
      // your App should show an explanatory UI now.
      return Future.error('Location permissions are denied');
    }
  }
  
  if (permission == LocationPermission.deniedForever) {
    // Permissions are denied forever, handle appropriately. 
    return Future.error(
      'Location permissions are permanently denied, we cannot request permissions.');
  } 

  // When we reach here, permissions are granted and we can
  // continue accessing the position of the device.
  return await Geolocator.getCurrentPosition();
}

Upvotes: 3

kururu95
kururu95

Reputation: 29

Use geolcator library add it to pubspec yaml file

import it

import 'package:geolocator/geolocator.dart';

then

Position position = await Geolocator().getLastKnownPosition(desiredAccuracy: LocationAccuracy.high);
print("my location is  ${position.latitude}  ${position.longitude}      ");

Upvotes: 2

Related Questions