Flutter Google Map - How to Get Current Coordinates

7,022 views
Skip to first unread message

Black Clover

unread,
Nov 12, 2018, 7:39:32 AM11/12/18
to Flutter Dev
I use this plugin for my project,
just want to ask,
how to get current GoogleMap coordinates?

I Want to select location by move the map, but the marker is stay on the center.

Google Maps Flutter
https://github.com/flutter/plugins/tree/master/packages/google_maps_flutter


Andreas Sumerauer

unread,
Nov 14, 2018, 4:29:52 AM11/14/18
to Flutter Dev
The answer to your question is in the example app.
Build it, then go to the User Interface screen and look up the second line below the map display. It shows the coordinates.
The code that does it can be found in
google_maps_flutter/example/lib/map_ui.dart/
You can use that as a starting point for your own experiments.

Anand Gupta

unread,
Mar 16, 2019, 1:41:33 AM3/16/19
to Flutter Dev
add the dependency in pubspec.yaml
  geocoder: ^0.1.2
  google_maps_flutter: ^0.4.0
  location: ^1.3.4

import 'dart:async';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:location/location.dart';
import 'package:geocoder/geocoder.dart';
import 'package:flutter/services.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';

class _MyHomePageState extends State<MyHomePage> {

  Map<String, double> currentLocation = Map();
  StreamSubscription<Map<String, double>> streamSubscription;
  Location location = Location();
  
  Completer<GoogleMapController> _controller = Completer();
  GoogleMapController mapController;

  final GlobalKey<FormState> formKey = GlobalKey<FormState>();
  TextEditingController controllerAddress = TextEditingController();


  double zoomCamera;
  LatLng latLngCamera=new LatLng(0.0, 0.0);

  String cityText;
  String error;
  String addressLine = '';
  String countryCode = '';
  String countryName = '';
  String postalCode = '';
  String adminArea = '';
  String subLocality = '';
  String featureName = '';
  String locality = '';
  String latlng = '';
  String url = '';

  @override
  void initState() {
    super.initState();
    currentLocation['latitude'] = 0.0;
    currentLocation['longitude'] = 0.0;

    initPlatform().then((value) {
      if (value != null) {
        _goToTheLake(currentLocation['latitude'], currentLocation['longitude']).then((val) {});
      }
    });

    streamSubscription = location.onLocationChanged().listen((Map<String, double> result) {
      setState(() {
        currentLocation = result;
      });
    });
  }

  static final CameraPosition _kGooglePlex = CameraPosition(
    target: LatLng(37.42796133580664, -122.085749655962),
    zoom: 14.4746,
  );

  @override
  Widget build(BuildContext context) {
    return Scaffold(

      body: Stack(
        alignment: Alignment.topCenter,
        children: <Widget>[
          GoogleMap(
            mapType: MapType.normal,
            myLocationEnabled: true,
            zoomGesturesEnabled: true,
            scrollGesturesEnabled: true,
            rotateGesturesEnabled: true,
            tiltGesturesEnabled: true,
            markers: Set<Marker>.of(
              <Marker>[
                Marker(
                  onTap: (){
                  /* mapController.animateCamera(CameraUpdate.newCameraPosition(CameraPosition(target: LatLng(, ))))*/
                  },
                  visible: true,
                  draggable: true,
                  markerId: MarkerId("1"),
                  position: latLngCamera,
                  icon: BitmapDescriptor.defaultMarker,
                  infoWindow: const InfoWindow(
                    title: 'My Marker',
                  ),
                )
              ],
            ),
            initialCameraPosition: _kGooglePlex,
            onMapCreated: (GoogleMapController controller) {
              _controller.complete(controller);
              mapController=controller;
            },
            onCameraMove: (value){
              print("Camere Move: ${value.zoom}");
              setState(() {
                zoomCamera=value.zoom;
                latLngCamera=value.target;
              });
            },
          ),
          Positioned(
            top: 50.0,
            right: 15.0,
            left: 15.0,
            child: Container(
              child: Form(
                key: formKey,
                child: Card(
                  color: Colors.white,
                  elevation: 3.0,
                  child: TextFormField(
                    controller: controllerAddress,
                    style: TextStyle(fontSize: 20.0),
                    decoration: InputDecoration(
                      fillColor: Colors.white,
                      hintText: 'Serach',
                      border: OutlineInputBorder(),
                      labelStyle: TextStyle(
                        fontSize: 20.0,
                        fontWeight: FontWeight.bold,
                      ),
                      suffixIcon: IconButton(
                          tooltip: "Show Password",
                          icon: Icon(Icons.my_location, color: Colors.blue,),
                          onPressed: () {
                            getLatLong(controllerAddress.text);
                            print(controllerAddress.text);
                          }),
                    ),
                    onSaved: (val) {
                      controllerAddress.text = val;
                    },
                  ),
                ),
              ),
            ),
          ),

        ],
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () async {
          setState(() {
            _goToTheLake(latLngCamera.latitude, latLngCamera.longitude);
          });
        },
        tooltip: 'Increment',
        child: Icon(Icons.location_on),
      ),
    );
  }

  Future<String> initPlatform() async {
    Map<String, double> myCurrentLocation;
    try {
      myCurrentLocation = await location.getLocation();
    } on
    PlatformException catch (e) {
      if (e.code == "PERMISSION_DENIED") {
        error = "PERMISSION_DENIED";
      }
      else if (e.code == "PERMISSION_DENIED_NEVER_ASK") {
        error = "Never Ask";
      }
      myCurrentLocation = null;
    }
    setState(() {
      currentLocation = myCurrentLocation;
    });
    final coordinates = Coordinates(currentLocation['latitude'], currentLocation['longitude']);
    var addresses = await Geocoder.local.findAddressesFromCoordinates(coordinates);
    var first = addresses.first;

    cityText = first.adminArea;
    return cityText;
  }

  Future<void> _goToTheLake(double lat, double lng) async {
    final GoogleMapController controller = await _controller.future;
    controller.animateCamera(CameraUpdate.newCameraPosition(CameraPosition(
      target: LatLng(lat, lng),
      zoom: zoomCamera,)));

    setState(() {
      latlng = lat.toString() + ", " + lng.toString();
      url = "https://www.google.co.in/maps/@" + lat.toString() + "," + lng.toString() + ",19z";
      print(url);
    });

    final coordinates = Coordinates(latLngCamera.latitude, latLngCamera.longitude);
    var addresses = await Geocoder.local.findAddressesFromCoordinates(coordinates);
    var first = addresses.first;
    print("Address Line: " + first.addressLine);
    print("Country Code: " + first.countryCode);
    print("Country Name: " + first.countryName);

    print("Postal Code: " + first.postalCode);
    print("Admin Area: " + first.adminArea);

    print("Feature Name: " + first.featureName);
    print("Locality: " + first.locality);
    print("Sub Locality: " + first.subLocality);
    setState(() {
      addressLine = first.addressLine;
      countryCode = first.countryCode;
      countryName = first.countryName;
      postalCode = first.postalCode;
      adminArea = first.adminArea;
      featureName = first.featureName;
      locality = first.locality;
      subLocality = first.subLocality;
    });
    showDialog(
        context: context,
        builder: (BuildContext context) =>
            locationDialog(latLngCamera.latitude, latLngCamera.longitude));
  }

  void getLatLong(String address) async  {
    var addresses = await Geocoder.local.findAddressesFromQuery(address);
    var first = addresses.first;
    print("${first.featureName} : ${first.coordinates}");
    _goToTheLake(first.coordinates.latitude, first.coordinates.longitude);
    LatLng latLng = new LatLng(first.coordinates.latitude, first.coordinates.longitude);
    //_addMyMarker(latLng);
  }

  Widget locationDialog(double lat, double long) {
    return CupertinoAlertDialog(
      title: new Text("Current Location"),
      content: Column(
        children: [
          Text('latitude : $lat'),
          Text('longitude : $long'),
      Text("address line:- $addressLine \n country code:- $countryCode \n countryName:- $countryName \n postalCode:- $postalCode \n adminArea:- $adminArea"
          "\n locality:- $locality \n featureName:- $featureName \n subLocality:- $subLocality", style: TextStyle(fontSize: 18.0),)
        ],
      ),
      actions: <Widget>[
        new CupertinoDialogAction(
            child: const Text('Discard'),
            isDestructiveAction: true,
            onPressed: () {
              Navigator.pop(context, 'Discard');
            }),
        new CupertinoDialogAction(
            child: const Text('Cancel'),
            isDefaultAction: true,
            onPressed: () {
              Navigator.pop(context, 'Cancel');
            }),
      ],
    );
  }
}


This code will work for put the marker in center and also get the location of the mark when we move camera position.
It will work for me.

Gopenahalli Vaari S Prakash

unread,
Feb 19, 2020, 12:57:48 AM2/19/20
to Flutter Development (flutter-dev)
Hello Anand Gupta, I can see the "initPlatform()" is used. But when I used it  I am getting some error. As in getting suggestion as create a method like that. May I know how can I do this.

Thanks in advance :)

Gopenahalli Vaari S Prakash

unread,
Feb 19, 2020, 1:18:28 AM2/19/20
to Flutter Development (flutter-dev)
Hello Anand Gupta, The Problem has been solved. Sorry I didn't notice that there was a method. 

 Thanks :)
Reply all
Reply to author
Forward
0 new messages