通过 Google-Maps Flutter 插件中标记的 Lat-Lng 位置的变化来更新标记位置?

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

我正在使用 flutter 制作车辆跟踪管理应用程序。我卡住的部分只是在车辆位置发生变化时更新地图上的标记位置。为了获取我们依赖于硬件的位置,数据将存储在 firestore 数据库中,并使用 streambuilder,我能够将位置从 Firestone 获取到应用程序。

在查看插件代码时,我发现了一个 _updateMarkers() 函数,但我不知道如何在应用程序中实现它。

我通过文档和插件代码找到了如下方法

使用 MarkerUpdates class 更新标记的位置。在 Google-Maps-Plugin 的文档中提到了相同的 class。此 class 将两个 Set<Marker> 作为输入,一个是当前标记集,另一个是新更新的标记集。 class 的文档在这里:https://pub.dev/documentation/google_maps_flutter_platform_interface/latest/google_maps_flutter_platform_interface/MarkerUpdates-class.html

要使用此 class,您必须添加以下导入语句: import 'package:google_maps_flutter_platform_interface/src/types/marker_updates.dart';

执行此方法时,我的 google-maps 插件版本是 google_maps_flutter: ^0.5.29+1

然后做一个函数如下:

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.
  });
}

然后像我的情况一样定期调用该函数,或者您希望标记更新。

在执行此操作时发出警告,因为我被警告提升为: Don't import implementation files from another package.dartimplementation_imports

我不知道这样做是否安全,但它正在完成工作。 如果有人可以告诉我们更多关于警告的信息,如果它有可能产生错误,那就太好了。

注:

有一个类似的 class 来更新圆、多边形和选项(地图选项)文档已经解释了所有这些 classes 的导入与提到的相同路径相似Updatemarkers class.

添加到 Adithya 的回答中。

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);

不需要将 updateMarkers() 放在 setState() 中。