-1

How can I get the LatLng of user's current location when using google_maps_flutter plugin?

I wish there had been something like:

GoogleMap(
  onLocationChanged: (latLng) {
    // Something like this callback ...
  }
)

PS: I don't wish to use any other plugin.

iDecode
  • 22,623
  • 19
  • 99
  • 186
  • and what is wrong with `GoogleMap.onCameraMove` / `onCameraMoveStarted`? – pskink Dec 05 '20 at 08:56
  • @pskink When user starts moving, I'm not sure any of them will get called on their own. Will they? – iDecode Dec 05 '20 at 09:06
  • ahh you want user's physical position change... no, they are called after changing position on map, most likely you need https://pub.dev/packages/geolocator – pskink Dec 05 '20 at 09:13
  • @pskink Yeah, that's what I want, just like how Navigation works on Google Map. Yes sir, I'm aware of that. I thought this could be done by google_maps_flutter itself and I might not be able to find how. – iDecode Dec 05 '20 at 09:15
  • Features Get the last known location; Get the current location of the device; Get continuous location updates; Check if location services are enabled on the device; Calculate the distance (in meters) between two geocoordinates; Calculate the bearing between two geocoordinates; – pskink Dec 05 '20 at 09:16

3 Answers3

2

Flutter is a framework which allows you to create UI easily. But behind this technology actually we don't have any native functionality. To bring these functionalities to flutter we have "Method Channel" or "Dart:FFI".

If you look at the source code of plugin which brings native functions to flutter they mostly use "Method Channel".

Google Maps plugin allows you to use google maps api, but however if you want to use or get client location, you must have some native code.

Some 3rd party apps already offer this functionality for you, but if you want to develop it for yourself, you have to write your own code for Android & iOS. Then you need to link it with the flutter. To do this you need to use method channel.

Vijay
  • 575
  • 4
  • 14
Emre Tufekci
  • 147
  • 2
  • 11
1

There is no way to get current location from google_maps_flutter plugin, but you can get displaying location either by

GoogleMap(
   onCameraMove: (LatLng displayingLocation){
      // do operations here
   }
)

or by using map controller

mapController.getLatLng(ScreenCoordinate(
        x: MediaQuery.of(context).size.width, 
        y: MediaQuery.of(context).size.height))
Mohamed Al Ameen
  • 355
  • 1
  • 3
  • 8
0

Maybe what you need is the location package from Flutter dev.

With that you can just listen for location changes, and maybe update your current location variable.

Something like:

location.onLocationChanged().listen((LocationData newLoc) {
  currentLocation = newLoc;
});
iDecode
  • 22,623
  • 19
  • 99
  • 186
codeKiller
  • 5,493
  • 17
  • 60
  • 115
  • Hi, thanks for your answer, although I clearly mentioned that I don't want to use any other plugin. I know it can easily be done using 3rd party packages. – iDecode Dec 11 '20 at 10:27
  • aha ok sorry! but I do not think you can get the current location from the google_maps package, it is just that the API does not provide this option. Another option without 3rd party packages (more complicated though) is to get the current location natively (Android and iOS) and pass it up to the flutter layer using flutter EventChannels. – codeKiller Dec 11 '20 at 10:41
  • No worries. I thought there's some way to get the `LatLng` in `google_maps_flutter` plugin but I'm just not able to figure that out yet, that's why the question was asked. – iDecode Dec 11 '20 at 11:08