Adithya Shetty
Adithya Shetty

Reputation: 1287

Updating marker position with change in Lat-Lng position of marker in Google-Maps Flutter Plugin?

I am making a vehicle tracking administration app using flutter. The part that I am stuck is only updating the marker position on the map when the location of the vehicle changes. For getting the location we are relying on the hardware and the data will be stored in the firestore database and with the use of the streambuilder, I am able to get the location from Firestone to the app.

While looking at plugin code I found a _updateMarkers() function but I have no idea how to implement the this in the application.

enter image description here

Upvotes: 1

Views: 5257

Answers (2)

Matt
Matt

Reputation: 803

Adding to Adithya's answer.

import 'package:google_maps_flutter_platform_interface/src/types/marker_updates.dart';

    var updates = MarkerUpdates.from(
            Set<Marker>.from(markers), Set<Marker>.from(updatedMarkers));

    GoogleMapsFlutterPlatform.instance.updateMarkers(updates, mapId: mapId);

Putting updateMarkers() inside a setState() is unnecessary.

Upvotes: 1

Adithya Shetty
Adithya Shetty

Reputation: 1287

I found the way after going through the documentation and plugin code it is as follows

Update the position of the markers using the MarkerUpdates class. The same class is mentioned in the documentation of the Google-Maps-Plugin. This class takes two Set<Marker> as inputs one the current Markers Set and another the new updated Marker Set. The documentation for this class is here: https://pub.dev/documentation/google_maps_flutter_platform_interface/latest/google_maps_flutter_platform_interface/MarkerUpdates-class.html

To use this class you will have to add this import statement : import 'package:google_maps_flutter_platform_interface/src/types/marker_updates.dart';

While performing this method my google-maps plugin version was google_maps_flutter: ^0.5.29+1

then make a function as follows :

List<Markers> markers; //This the list of markers is the old set of markers that were used in the onMapCreated function 

void upDateMarkers() {
  List<Markers> updatedMarkers =[]; //new markers with updated position go here 

  updatedMarkers =['updated the markers location here and also other properties you need.'];
  

  /// Then call the SetState function.
  /// I called the MarkersUpdate class inside the setState function.
  /// You can do it your way but remember to call the setState function so that the updated markers reflect on your Flutter app.
  /// Ps: I did not try the second way where the MarkerUpdate is called outside the setState buttechnically it should work.
  setState(() {
    MarkerUpdates.from(
        Set<Marker>.from(markers), Set<Marker>.from(updatedMarkers));
    markers = [];
    markers = updatedMarkers;
 //swap of markers so that on next marker update the previous marker would be the one which you updated now.
// And even on the next app startup, it takes the updated markers to show on the map.
  });
}

then call the function periodically like in my case or as you wish the markers will update.

While doing this a warning as I was promoted with a warning as : Don't import implementation files from another package.dartimplementation_imports

I don't know if it's a safe approach to do but it is doing the job. It would be great if someone could tell us more about the warning if it has the potential of creating a bug.

Note:

There is a similar class to update the circle, polygons and options (Map Options) the documentation has explained all and imports for those classes are similar in the same path as mentioned for the Updatemarkers class.

Upvotes: 1

Related Questions