Flutter - 拖动标记并获得新位置
Flutter - Drag marker and get new position
在 Flutter 中,我有一个在 location. Then i use this position to show marker in Google map with help of Google Maps Flutter 插件的帮助下获取设备位置的地图。
代码类似
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 来拖动标记,但我需要使用新的纬度和经度将其保存在数据库中。
所以问题是:使用 draggable 我怎样才能得到新的纬度和经度?
如果您允许,我会建议使用不同的方法来解决这个问题。我认为在当前版本的 google maps for Flutter (0.4.0) 中,您没有办法在拖动标记后获得新位置,所以我的解决方案是使用相机位置来移动标记周围,然后使用当前相机位置获取新坐标。
所以在你的代码中,你可以这样做:
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í',
),
)
],
),
onCameraMove: ((_position) => _updatePosition(_position)),
onMapCreated: (GoogleMapController controller) {
mapController = controller;
},
))
然后您可以在每次用户移动相机时设置标记的新状态,并将此坐标用于您需要的任何其他目的:
void _updatePosition(CameraPosition _position) {
Position newMarkerPosition = Position(
latitude: _position.target.latitude,
longitude: _position.target.longitude);
Marker marker = markers["1"];
setState(() {
markers["1"] = marker.copyWith(
positionParam: LatLng(newMarkerPosition.latitude, newMarkerPosition.longitude));
});
}
希望对您有所帮助!
标记有 onDragEnd 属性。使用 onDragEnd 属性 给出新的 latitude 和 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 中,我有一个在 location. Then i use this position to show marker in Google map with help of Google Maps Flutter 插件的帮助下获取设备位置的地图。
代码类似
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 来拖动标记,但我需要使用新的纬度和经度将其保存在数据库中。
所以问题是:使用 draggable 我怎样才能得到新的纬度和经度?
如果您允许,我会建议使用不同的方法来解决这个问题。我认为在当前版本的 google maps for Flutter (0.4.0) 中,您没有办法在拖动标记后获得新位置,所以我的解决方案是使用相机位置来移动标记周围,然后使用当前相机位置获取新坐标。 所以在你的代码中,你可以这样做:
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í',
),
)
],
),
onCameraMove: ((_position) => _updatePosition(_position)),
onMapCreated: (GoogleMapController controller) {
mapController = controller;
},
))
然后您可以在每次用户移动相机时设置标记的新状态,并将此坐标用于您需要的任何其他目的:
void _updatePosition(CameraPosition _position) {
Position newMarkerPosition = Position(
latitude: _position.target.latitude,
longitude: _position.target.longitude);
Marker marker = markers["1"];
setState(() {
markers["1"] = marker.copyWith(
positionParam: LatLng(newMarkerPosition.latitude, newMarkerPosition.longitude));
});
}
希望对您有所帮助!
标记有 onDragEnd 属性。使用 onDragEnd 属性 给出新的 latitude 和 longitude.
Marker(
onTap: () {
print('Tapped');
},
draggable: true,
markerId: MarkerId('Marker'),
position: LatLng(value.latitude, value.longitude),
onDragEnd: ((newPosition) {
print(newPosition.latitude);
print(newPosition.longitude);
}))