Flutter - 拖动标记并获得新位置 [英] Flutter - Drag marker and get new position

查看:22
本文介绍了Flutter - 拖动标记并获得新位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Flutter 中,我有一张借助 location 获取设备位置的地图.然后我在 Google Maps Flutter 插件的帮助下使用这个位置在 Google 地图中显示标记.

In Flutter I have a map that take the device position with help of location. Then i use this position to show marker in Google map with help of Google Maps Flutter plugin.

代码类似

 child: GoogleMap(
        mapType: MapType.hybrid,
        initialCameraPosition: CameraPosition(
          target: LatLng(lat, lng),
          zoom: 5,
        ),
        markers: Set<Marker>.of(
          <Marker>[
            Marker(
              draggable: true,
              markerId: MarkerId("1"),
              position: LatLng(lat, lng),
              icon: BitmapDescriptor.defaultMarker,
              infoWindow: const InfoWindow(
                title: 'Usted está aquí',
              ),
            )
          ],
        ),
        onMapCreated: (GoogleMapController controller) {
          mapController = controller;
        },
      ))

如果定位插件失败或获取错误数据,我需要重新定位此标记的选项.我使用 draggable: true 来拖动标记,但我需要将新的纬度和经度保存在数据库中.

If location plugin fails or take wrong data i need to have the option to reposition this marker. I use draggable: true to drag marker, but i need to take the new latitude and longitude for save it in database.

所以问题是:使用可拖动的如何获得新的经纬度?

So the question is: Using the draggable how can i get the new latitude and longitude?

推荐答案

Marker 有 onDragEnd 属性.使用 onDragEnd 属性给出新的 latitudelongitude.

Marker have onDragEnd property. using onDragEnd property give new latitude and longitude.

Marker(
          onTap: () {
            print('Tapped');
          },
          draggable: true,
          markerId: MarkerId('Marker'),
          position: LatLng(value.latitude, value.longitude),
          onDragEnd: ((newPosition) {
            print(newPosition.latitude);
            print(newPosition.longitude);
          }))

这篇关于Flutter - 拖动标记并获得新位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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