title: How to activate location when clicking on button using google maps flutter?
body: <p>I am new using Flutter and i am making a app. In that i need to use google maps and show my location only if activated by a button.</p>
  
  <p>I am using the example in: </p>
  
  <p><a href="https://pub.dartlang.org/packages/google_maps_flutter" rel="noreferrer">https://pub.dartlang.org/packages/google_maps_flutter</a></p>
  
  <p>That give you a button that redirect the camera to a certain point, but how can i turn that button to be the trigger of using my location or not?</p>
  
  <p>I made something like this but i get an error:</p>
  
  <pre><code>import 'dart:async';
  
  import 'package:flutter/material.dart';
  import 'package:google_maps_flutter/google_maps_flutter.dart';
  import 'package:location/location.dart' as LocationManager;
  
  void main() =&gt; runApp(Lugares());
  
  class Lugares extends StatelessWidget {
    @override
    Widget build(BuildContext context) {
      return MaterialApp(
        title: 'Lugares',
        home: MapSample(),
        debugShowCheckedModeBanner: false,
      );
    }
  }
  
  class MapSample extends StatefulWidget {
    @override
    State&lt;MapSample&gt; createState() =&gt; MapSampleState();
  }
  
  class MapSampleState extends State&lt;MapSample&gt; {
    Completer&lt;GoogleMapController&gt; _controller = Completer();
  
    static final CameraPosition _kGooglePlex = CameraPosition(
      target: LatLng(37.42796133580664, -122.085749655962),
      zoom: 14.4746,
    );
  
    static final CameraPosition _kLake = CameraPosition(
        bearing: 192.8334901395799,
        target: LatLng(37.43296265331129, -122.08832357078792),
        tilt: 59.440717697143555,
        zoom: 19.151926040649414);
  
    @override
    Widget build(BuildContext context) {
      return new Scaffold(
        body: GoogleMap(
          mapType: MapType.hybrid,
          initialCameraPosition: _kGooglePlex,
          onMapCreated: (GoogleMapController controller) {
            _controller.complete(controller);
          },
          myLocationEnabled: true,
        ),
        floatingActionButton: FloatingActionButton.extended(
          onPressed: _goToTheLake,
          label: Text('To the lake!'),
          icon: Icon(Icons.directions_boat),
        ),
      );
    }
  
    Future&lt;void&gt; _goToTheLake() async {
      final GoogleMapController controller = await _controller.future;
      controller.animateCamera(CameraUpdate.newCameraPosition(CameraPosition(
          target: getUserLocation() == null ? LatLng(0, 0) : getUserLocation(), zoom: 15.0)));
    }
  
    Future&lt;LatLng&gt; getUserLocation() async {
      var currentLocation = &lt;String, double&gt;{};
      final location = LocationManager.Location();
      try {
        currentLocation = (await location.getLocation()) as Map&lt;String, double&gt;;
        final lat = currentLocation["latitude"];
        final lng = currentLocation["longitude"];
        final center = LatLng(lat, lng);
        return center;
      } on Exception {
        currentLocation = null;
        return null;
      }
    }
  
  }
  </code></pre>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>Thanks to @Vishal G. Gohel that share his code i could acomplish what i want, this is the code:</p>
  
  <pre><code>import 'dart:async';
  import 'package:flutter/material.dart';
  import 'package:google_maps_flutter/google_maps_flutter.dart';
  import 'package:location/location.dart';
  
  void main() =&gt; runApp(Lugares());
  
  class Lugares extends StatelessWidget {
    @override
    Widget build(BuildContext context) {
      return MaterialApp(
        title: 'Lugares',
        home: MapSample(),
        debugShowCheckedModeBanner: false,
      );
    }
  }
  
  class MapSample extends StatefulWidget {
    @override
    State&lt;MapSample&gt; createState() =&gt; MapSampleState();
  }
  
  class MapSampleState extends State&lt;MapSample&gt; {
    Completer&lt;GoogleMapController&gt; _controller = Completer();
  
    static final CameraPosition _kGooglePlex = CameraPosition(
      target: LatLng(37.42796133580664, -122.085749655962),
      zoom: 14.4746,
    );
  
    @override
    Widget build(BuildContext context) {
      return new Scaffold(
        body: GoogleMap(
          mapType: MapType.hybrid,
          initialCameraPosition: _kGooglePlex,
          onMapCreated: (GoogleMapController controller) {
            _controller.complete(controller);
          },
          myLocationEnabled: true,
        ),
        floatingActionButton: FloatingActionButton.extended(
          onPressed: _currentLocation,
          label: Text('Ir a mi Ubicacion!'),
          icon: Icon(Icons.location_on),
        ),
      );
    }
  
  
  
  void _currentLocation() async {
     final GoogleMapController controller = await _controller.future;
     LocationData currentLocation;
     var location = new Location();
     try {
       currentLocation = await location.getLocation();
       } on Exception {
         currentLocation = null;
         }
  
      controller.animateCamera(CameraUpdate.newCameraPosition(
        CameraPosition(
          bearing: 0,
          target: LatLng(currentLocation.latitude, currentLocation.longitude),
          zoom: 17.0,
        ),
      ));
    }
  
  }
  </code></pre>