动态地将标记添加到Google Map的颤动中 [英] Adding markers dynamically to flutter google map

查看:99
本文介绍了动态地将标记添加到Google Map的颤动中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Flutter制作使用地图的移动应用程序.我们决定使用Google地图,而我们用于抖动的插件是:

I'm using Flutter to make a mobile application that uses a map. We decided to use Google map and the plugin for flutter we use is:

google_maps_flutter:^ 0.5.7

google_maps_flutter: ^0.5.7

我知道在地图上添加标记的方式如下:

I understand that adding markers to the map works like this:

Map<MarkerId, Marker> markers = <MarkerId, Marker>{};
Marker _marker = new Marker(
  icon: BitmapDescriptor.defaultMarker,
  markerId: _markerId,
  position: LatLng(34.024441,-5.0310968),
  infoWindow: InfoWindow(title: "userMarker", snippet: '*'),
);
GoogleMap _map;

@override
void initState(
   markers[_markerId] = _marker;
   _map = new GoogleMap(
    /* other properties irrelevant to this prob */
    markers: Set<Marker>.of(_markers.values),
  );
);

上面的代码片段确实有效,我可以在地图上看到标记.但是修改标记或尝试添加其他标记(如下面的代码片段一样)不起作用.

The above snippet does work, I get to see the marker on the map. But modifying the marker or trying to add another marker like in the snippet below does not work.

FloatingActionButton(
    onPressed: () {
      setState(() {
        _marker = new Marker(
            icon: BitmapDescriptor.defaultMarker,
            markerId: _markerId,
            position: LatLng(currentLocation.latitude, currentLocation.longitude),
            infoWindow: InfoWindow(title: "userMarker", snippet: '*'),
            onTap: () {
              debugPrint("Marker Tapped");
            },
          );
        markers[_markerId] = _marker; // What I do here is modify the only marker in the Map.
      });
      markers.forEach((id,marker) { // This is used to see if the marker properties did change, and they did.
        debugPrint("MarkerId: $id");
        debugPrint("Marker: [${marker.position.latitude},${marker.position.longitude}]");
      });
    });
)

我在这里的意图是使用另一个插件(geoLocator)来获取用户的位置数据并更改我拥有的唯一标记,以便它可以跟踪他的运动. debugPrint显示数据确实在变化,但是我在地图上看不到变化(测试时,我更改的初始标记使用的位置与我自己的位置不同).

My intention here is using another plugin (geoLocator) to get location data of the user and change the only marker I have so it can track his movements. The debugPrint shows that the data is indeed changing, but I see no change in the map (the initial marker that I change uses a different location than my own when I test).

推荐答案

如果您没有使用Map数据结构的特定原因,这就是我过去所做的事情.

If there's no specific reason for you to use Map data structure, here's what I've done in the past.

我的State

Set<Marker> markers = Set();

将其交给地图小部件.标记:标记

Give it to the map widget. markers: markers

GoogleMap(
  onMapCreated: _onMapCreated,
  myLocationEnabled: true,
  initialCameraPosition:
    CameraPosition(target: LatLng(0.0, 0.0)),
  markers: markers,
)

然后在setState方法中将我正在使用搜索结果构建的,将要使用用户位置构建的Marker添加到Set of Marker中.

And then adding the Marker, which I'm building with search result and which you'll be building with your user's location, to Set of Marker in setState method.

// Create a new marker
Marker resultMarker = Marker(
  markerId: MarkerId(responseResult.name),
  infoWindow: InfoWindow(
  title: "${responseResult.name}",
  snippet: "${responseResult.types?.first}"),
  position: LatLng(responseResult.geometry.location.lat,
  responseResult.geometry.location.lng),
);
// Add it to Set
markers.add(resultMarker);

编辑:我刚刚注意到您在initState中使用的是GoogleMap小部件,如果您想每次使用新版本重新构建它,都需要将其移至build方法中状态值.

Edit: I've just noticed you're using GoogleMap widget in your initState, you need to move it to the build method if you want to rebuild it everytime with the new state values.

这篇关于动态地将标记添加到Google Map的颤动中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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