Chris
Chris

Reputation: 2034

How to make Flutter GoogleMap initialCameraPosition show user's current location?

Ok, so I have a map screen where a user can select a location and place a marker, but the problem is I cannot figure out how to set the GoogleMap initialCameraPosition to the user's current location.

I have real trouble understand how to implement the instructions from the flutter Location package (https://pub.dev/packages/location).

I feel so stupid I wish I could just figure it out :'(

import 'package:location/location.dart';

class MapScreen extends StatefulWidget {

@override
  _MapScreenState createState() => _MapScreenState();
}

class _MapScreenState extends State<MapScreen> {
  LatLng _pickedLocation;

  void _selectLocation(LatLng position) {
    setState(
      () {
        _pickedLocation = position;
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
body: GoogleMap(
        compassEnabled: true,
        myLocationButtonEnabled: true,
        myLocationEnabled: true,
        initialCameraPosition: CameraPosition(
          target: LatLng(0, 0),
          zoom: 16,
        ),
        onTap: widget.isSelecting ? _selectLocation : null,
        markers: (_pickedLocation == null && widget.isSelecting)
            ? null
            : {
                Marker(
                  markerId: MarkerId('m1'),
                  position: _pickedLocation ??
                      LatLng(widget.initialLocation.latitude,
                          widget.initialLocation.longitude),
                ),
              },
      ),
      floatingActionButton: widget.isSelecting
          ? FloatingActionButton(
              disabledElevation: 0,
              child: Icon(Icons.check),
              onPressed: _pickedLocation == null
                  ? null
                  : () {
                      Navigator.of(context).pop(_pickedLocation);
                    },
            )
          : null,
    );
  }
}

I've tried to figure this out myself but I dont know how to do it properly and need someone to explain what I need to do and WHY.

Upvotes: 0

Views: 362

Answers (1)

Bruno Sponsorship
Bruno Sponsorship

Reputation: 148

Add the Location lib

import 'package:location/location.dart';

var location = new Location();

And add OnMapCreated

GoogleMap(
        compassEnabled: true,
        myLocationButtonEnabled: true,
        myLocationEnabled: true,
        initialCameraPosition: CameraPosition(
          target: LatLng(0, 0),
          zoom: 16,
        ),
        onTap: widget.isSelecting ? _selectLocation : null,
        markers: (_pickedLocation == null && widget.isSelecting)
            ? null
            : {
                Marker(
                  markerId: MarkerId('m1'),
                  position: _pickedLocation ??
                      LatLng(widget.initialLocation.latitude,
                          widget.initialLocation.longitude),
                ),
              },

    onMapCreated: (GoogleMapController controller) {
     var here = await location.getLocation();
    controller.animateCamera(
              CameraUpdate.newCameraPosition(
                CameraPosition(
                    target: LatLng(here.latitude, here.longitude)),
              ),
            );
    }),

Upvotes: 1

Related Questions