0

Blockquote

I have a block of code in my flutter google map implementation like this:

Location _location = Location();
 void _onMapCreated(GoogleMapController _cntrl) {
    _controller = _cntrl;
    _location.onLocationChanged.listen((l) {
      print(l.latitude);
      print(l.longitude);
      _controller.animateCamera(
        CameraUpdate.newCameraPosition(
          CameraPosition(
            target: LatLng(l.latitude, l.longitude),
            zoom: 15
          ),
        ),
      );
    });
  }

By this I am getting user's current Latitude and Longitude .

I want to store those longitude and latitude inside another 2 variables outside the function for further use. How would I do that??

My requirement is I want user's current longitude & latitude as the source point and another 2 hard coded longitude & latitude as destination point on flutter google map.

I just want to access those l.latitude & l.longitude outside the _onMapCreated(). Please help me out!

  • *"I want to store those longitude and latitude inside another 2 variables outside the function for further use. How would I do that??"* - `currentLocation = l;` - where `currentLocation` is a field / global var / whatever – pskink Jan 27 '21 at 05:46
  • @pskink , you mean currentLat = l.latitude ; and currentLng = l.longitude ; ??? – sushree soma mohanty Jan 27 '21 at 05:50
  • you dont need those two `double` vars - just use one `LocationData currentLocation` variable – pskink Jan 27 '21 at 05:58
  • @pskink, I did like that "LocationData currentLocation = l ;" after the _onMapCreated() {} ends. But it's showing undefined name 'l' – sushree soma mohanty Jan 27 '21 at 06:04
  • how can it be undefined if you use `print(l.latitude);` ? – pskink Jan 27 '21 at 06:14
  • @pskink I used "print(l.latitude);" inside the void _onMapCreated(GoogleMapController _cntrl) {} . Now I want to access that outside the void _onMapCreated(GoogleMapController _cntrl) {} – sushree soma mohanty Jan 27 '21 at 06:21
  • so do `currentLocation = l;` inside that `void _onMapCreated(GoogleMapController _cntrl) {}` – pskink Jan 27 '21 at 06:22
  • but I want to store it inside another variable so that I can use it further (like to draw routes from source to destination) – sushree soma mohanty Jan 27 '21 at 06:32

1 Answers1

0

Create 2 methods (1 for asking for permissions, the other one for track the current location) and call them in didChangeDependencies

void _permissions() {
// First, it is needed to ask for permissions
  try {
    location.requestPermission();
    location.getLocation().then((location) {
       // You can get the first user location
       print(location.latitude);
    });
  } on PlatformException catch (e) {
    if (e.code == "PERMISSION_DENIED") {
      print("Permission denied.");
    }
    print(e);
  }
}

void _getCurrentLocation() {
  location.onLocationChanged.listen((LocationData currentLocation) {
    // Use current location
    print(currentLocation.latitude);
  });
}

@override
void didChangeDependencies() {
  super.didChangeDependencies();
  _permissions();
  _getCurrentLocation();
}

Néstor
  • 570
  • 2
  • 8
  • 22