![](/img/trans.png)
[英]How to Integrate Map in flutter Application and get marker latitude and longitude which user set on map?
[英]How to get marker latitude and longitude on flutter map?
我正在使用 google_maps_flutter 包來顯示地圖,並放置了一個標記,以便用戶可以選擇一個位置(而不是用戶在地圖上的任何位置)。
像這樣現在用戶可以將標記拖到他想要的地方,我的問題是
用戶拖動標記后,我可以獲取標記的緯度和經度嗎?
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,
),
您可以使用google_maps_flutter插件中的標記來獲取標記坐標。 它有一個onDrag
屬性,該屬性返回一個包含坐標的LatLng
對象。 使用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.