[英]How to get marker latitude and longitude on flutter map?
I'm using google_maps_flutter package to display the map and I placed a marker so the user can select a location (not the user location any location on the map).我正在使用 google_maps_flutter 包来显示地图,并放置了一个标记,以便用户可以选择一个位置(而不是用户在地图上的任何位置)。
like this Now the user can drag the marker where he want , my question is 像这样现在用户可以将标记拖到他想要的地方,我的问题是
Can I get latitude and longitude for marker after user drag it?用户拖动标记后,我可以获取标记的纬度和经度吗?
class _BodyState extends State<Body> {
MapType mapType = MapType.normal;
Completer<GoogleMapController> _controller = Completer();
LocationServices _locationServices;
LocationData _locationData;
CameraPosition _cameraPosition;
Set<Marker> allMapMarkers;
Marker userPicker;
@override
Widget build(BuildContext context) {
_locationServices=Provider.of<LocationServices>(context);
return FutureBuilder(
future: _locationServices.getLocation().then((LocationData value) => _locationData=value),
builder:(context,snapshot){
if(_locationData==null){
return Center(child: CircularProgressIndicator());
}else{
_cameraPosition=CameraPosition(target:LatLng(_locationData.latitude,_locationData.longitude),zoom: 19);
setupMarkers(); // this set the Lat and Long for marker
return SafeArea(
child: Stack(
children: [
GoogleMap(
onMapCreated: (GoogleMapController controller) {
_controller.complete(controller);
},
initialCameraPosition: _cameraPosition,
mapType: mapType,
compassEnabled: false,
zoomControlsEnabled: false,
markers: allMapMarkers,
),
You can use the Marker from google_maps_flutter plugin to fetch marker coordinates.您可以使用google_maps_flutter插件中的标记来获取标记坐标。 It has an onDrag
property that returns a LatLng
object that contains coordinates.它有一个onDrag
属性,该属性返回一个包含坐标的LatLng
对象。 Use the onDragEnd property to fetch the coordinates on where the marker has been placed.使用onDragEnd属性获取放置标记的坐标。
Marker(
onTap: () {
debugPrint('Tapped');
},
draggable: true,
markerId: MarkerId('Marker'),
onDragEnd: ((LatLng newPosition) {
debugPrint(newPosition.latitude);
debugPrint(newPosition.longitude);
}),
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.