更新标记位置,并更改Google-Maps Flutter插件中标记的Lat-Lng位置? [英] Updating marker position with change in Lat-Lng position of marker in Google-Maps Flutter Plugin?

查看:304
本文介绍了更新标记位置,并更改Google-Maps Flutter插件中标记的Lat-Lng位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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

推荐答案

在完成文档和插件代码之后,方法如下

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

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

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

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

To use this class you will have to add this import statement : 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.
  });
}

然后像我这样或您希望标记会定期调用该函数

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

在执行此警告时,我被警告为:
不要从另一个包中导入实现文件。dartimplementation_imports

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.

注意:

有一个类似的类来更新圆形,多边形和选项(地图选项),文档已解释了所有内容,并且这些类的导入在与 Updatemarkers 类。

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.

这篇关于更新标记位置,并更改Google-Maps Flutter插件中标记的Lat-Lng位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆