2

How to integrate live tracking with flutter_map. Does anyone know which library is integrated into it? or How to implement it?

paul
  • 519
  • 6
  • 13

3 Answers3

8

In order to do this, we need to understand what live location tracking is. Mainly it consists of these:

  • The location being broadcasted (the one being tracked) => 1st party.
  • The listener for these location updates (observer) => 2nd party.
  • A place to store these changes, which acts as a middle ground between the two parties. This is your database. Could be FireStore, or a traditional database.

Your 1st party will have to be aware or location changes, and when it's coordinates change, it will post the new coordinates to your database. This can be done using your location package, either Location or Geolocator or other dependancies. You listen to onLocationChanged.

Your 2nd party will be monitoring your database, for any new coordinates being posted. Ideally, it would be by utilizing a streambuilder in Flutter, which listen to a stream of events coming from your database via your server, or more practically, via FirebaseFirestore streams.

When your 2nd party receives these new coordinates, it will updated the map with the newly received data. You implement this by using two things:

  • Add a marker to the map, the marker takes a required LatLng argument. This is the LatLng you just received.
  • You also have to animate the camera, to point at the newly received LatLng.

Now, you have 1st party uploading it's data whenever location changes, and 2nd party getting the data as soon as it is being uploaded, and you have live tracking. Implementing it isn't as hard as you imagine.

  1. Use Flutter google maps.
  2. Enable google maps API in google cloud console for your platform(s), Android, iOS, JavaScript if you want web.
  3. Install your location dependency.
  4. Update your database with the new coords when they change.
  5. Build your map widget on your 2nd party's device, as per the documentation, which is crystal clear,and put it in a streambuilder.
  6. Update this map with the new coords.

You get a location tracking system.

Huthaifa Muayyad
  • 11,321
  • 3
  • 17
  • 49
1

It uses user_location_plugin

I don't think you want to implement it yourself.

Pablito
  • 521
  • 4
  • 15
1

Live location tracking of the user's own device can be achieved with the flutter_map_location_marker plugin, as listed on the flutter_map docs. It superseeds 'user_location_plugin' as suggested by Pablito, and another (now archived) plugin, and uses the device's location metric to move a Marker around the map.

Huthaifa Muayyad's answer may be appropriate if you are trying to track another device - in this case you will need the complex intermediate database setup. Note that their answer uses 'flutter_google_maps' instead of 'flutter_map'.

JaffaKetchup
  • 1,151
  • 10
  • 26