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

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

问题描述

在Flutter中,我有一张地图,该地图借助位置来确定设备位置。然后,在 Google Maps Flutter 插件的帮助下,我使用该位置在Google地图中显示标记。 / p>

代码类似于

 子级:GoogleMap(
mapType:MapType.hybrid,
initialCameraPosition:CameraPosition(
目标:LatLng(lat,lng),
zoom:5,
),
标记:Set< Marker> .of(
< Marker> [
Marker(
draggable:true,
markerId:MarkerId( 1),
位置:LatLng(lat ,lng),
图标:BitmapDescriptor.defaultMarker,
infoWindow:const InfoWindow(
title:'Ustedestáaquí',
),

],
),
onMapCreated :( GoogleMapController控制器){
mapController = contr oller;
},
))

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



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

解决方案

标记具有 onDragEnd 属性。使用 onDragEnd 属性赋予新的纬度经度

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


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.

The code is similar to

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

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 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天全站免登陆