gpt4 book ai didi

google-maps - flutter 中更新Google Maps AlertDialog中的标记

转载 作者:行者123 更新时间:2023-12-03 04:54:07 25 4
gpt4 key购买 nike

我正在尝试加载googlemap(google_maps_flutter 0.5.25 + 1
库,使用以下方法在对话框窗口上

_loadMapDialog() {
try {
if (_currentPosition.latitude == null) {
Toast.show("Location not available. Please wait...", context,
duration: Toast.LENGTH_LONG, gravity: Toast.BOTTOM);
_getLocation(); //_getCurrentLocation();
return;
}
_controller = Completer();
_userpos = CameraPosition(
target: LatLng(latitude, longitude),
zoom: 14.4746,
);

markers.add(Marker(
markerId: markerId1,
position: LatLng(latitude, longitude),
infoWindow: InfoWindow(
title: 'New Location',
snippet: 'Delivery Location',
)));

showDialog(
context: context,
builder: (context) {
return StatefulBuilder(
builder: (BuildContext context, StateSetter setState) {
return AlertDialog(
title: Text("Select Your Location"),
titlePadding: EdgeInsets.all(5),
content: Text(curaddress),
actions: <Widget>[
Container(
height: screenHeight / 1.4 ?? 600,
width: screenWidth ?? 400,
child:
GoogleMap(
mapType: MapType.normal,
initialCameraPosition: _userpos,
markers: markers,
onMapCreated: (controller) {
_controller.complete(controller);
},
onTap: _loadLoc,
),
)
],
);
},
);
},
);
} catch (e) {
print(e);
return;
}


}

void _loadLoc(LatLng loc) async{
setState(() {
print("insetstate");
markers.clear();
latitude = loc.latitude;
longitude = loc.longitude;
label = latitude.toString();
_getLocationfromlatlng(latitude,longitude);
_home = CameraPosition(
target: loc,
zoom: 14,
);
markers.add(Marker(
markerId: markerId1,
position: LatLng(latitude, longitude),
infoWindow: InfoWindow(
title: 'New Location',
snippet: 'Delivery Location',
)));

});
_userpos = CameraPosition(
target: LatLng(latitude, longitude),
zoom: 14.4746,
);
_newhomeLocation();
}

Future<void> _newhomeLocation() async {
gmcontroller = await _controller.future;
gmcontroller.animateCamera(CameraUpdate.newCameraPosition(_home));
Navigator.of(context).pop(false);
_loadMapDialog();
}

我确实设法在我的AlertDialog中加载了 map 。问题是我需要能够在 map 上选择新位置,删除先前的标记并在 map 上显示新标记,但是除非应用执行热重装,否则标记不会显示在 map 上。现在,我使用一种愚蠢的方式弹出当前的alertdialog,并使用_loadMapDialog()方法再次显示它以重新加载小部件。我确实尝试使用flutter_places_dialog库,但似乎我在 Activity 返回结果错误中遇到了一些问题。

flutter 新手在这里..

最佳答案

问题是您正在使用StatefulWidget提供的setState更新标记。
但是Dialog正在使用StatefulBuilder提供的setState更新其状态。

解决方案是将StatefulBuilder的setState添加到onTap回调函数的参数中,并在_loadLoc函数中使用它,如我的代码。

List<Marker> markers = [];
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('GoogleMap'),
),
body: Center(
child: RaisedButton(
onPressed: () {
showDialog(
context: (context),
builder: (context) {
return StatefulBuilder(builder: (context, newSetState) {
return AlertDialog(
title: Text('Google Map'),
content: GoogleMap(
initialCameraPosition: CameraPosition(
target: LatLng(11.004556, 76.961632), zoom: 14),
markers: markers.toSet(),
onTap: (newLatLng) {
addMarker(newLatLng, newSetState);

},
),
);
});
});
},
),
),
);

addMarker(latLng, newSetState)
{
newSetState(() {
markers.clear();
markers.add(Marker(markerId: MarkerId('New'), position: latLng));
});
}

关于google-maps - flutter 中更新Google Maps AlertDialog中的标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60869149/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com