Google Maps Current User Location - Flutter

4,282

This is async programming. currentLatLng is null until getCurrentPosition calls it's callback, so you can't just do this:

initialCameraPosition: CameraPosition(target: currentLatLng)

because, as you error shows, currentLatLng is null.

Your two options are:

  1. Set the map to a default position that you define and then update the position using the mapcontroller when getCurrentPosition completes.

  2. Show a loader while currentLatLng is null, and when it's no longer null, show your map.

Here's an example for 2

    return MaterialApp(
      home: new Scaffold(
        body: currentLatLng == null ? Center(child:CircularProgressIndicator()) : GoogleMap(
          mapType: MapType.normal,
          initialCameraPosition: CameraPosition(target: currentLatLng),
          onMapCreated: (GoogleMapController controller) {
            _controller.complete(controller);
          },
        ),
      ),
    );
Share:
4,282
Haseeb
Author by

Haseeb

Updated on December 29, 2022

Comments

  • Haseeb
    Haseeb over 1 year

    I am trying to get the user's current location from Google Maps but I am getting an exception in return which I can't seem to understand why it's happening. The app is simple - get the user's current location, and then zoom the camera onto that location. That's it.

    I am going to attach my code and the exception message. The strange thing is that I am getting my LatLng correctly as you can see in the exception message, so why is the camera not pointing to that location?

    class Map extends StatefulWidget {
      @override
      _MapState createState() => _MapState();
    }
    
    class _MapState extends State<Map> {
    
      LatLng currentLatLng;
      Completer<GoogleMapController> _controller = Completer();
    
    
      @override
      void initState(){
        super.initState();
        Geolocator.getCurrentPosition().then((currLocation){
          setState((){
            currentLatLng = new LatLng(currLocation.latitude, currLocation.longitude);
          });
        });
      }
    
      @override
      Widget build(BuildContext context) {
        print("Current Location --------> " + currentLatLng.latitude.toString() + " " + currentLatLng.longitude.toString());
        return MaterialApp(
          home: new Scaffold(
            body: GoogleMap(
              mapType: MapType.normal,
              initialCameraPosition: CameraPosition(target: currentLatLng),
              onMapCreated: (GoogleMapController controller) {
                _controller.complete(controller);
              },
            ),
          ),
        );
      }
    }
    

    Exception Message

    note - I am using 'geolocator' and 'google_maps_flutter' from pub.dev - all latest versions.

    • Jagraj Singh
      Jagraj Singh about 3 years
      You have to pass the target parameter. You must be setting it null or not passing anything to it.
    • Milvintsiss
      Milvintsiss about 3 years
      Please upload your log and not a screenshot of your log.
  • Haseeb
    Haseeb about 3 years
    working perfectly. Option 2 works best. THANK YOU!