簡體   English   中英

在顫動中更新 Google 地圖警報對話框中的標記

[英]Update marker in Google maps alertdialog in flutter

我正在嘗試使用以下方法在對話窗口上加載 googlemap(google_maps_flutter 0.5.25+1 library)

_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 中加載了地圖。 問題是我需要能夠在地圖上選擇新位置刪除以前的標記並在地圖上顯示新標記,但是除非應用程序執行熱重新加載,否則標記不會顯示在地圖上。 現在我正在使用一種愚蠢的方法來彈出當前的警報對話框並使用 _loadMapDialog() 方法重新加載小部件再次顯示它。 我確實嘗試使用 flutter_places_dialog 庫,但似乎我在活動返回結果錯誤方面遇到了一些問題。

Flutter newb 在這里請客氣點..

問題是您正在使用 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));
  });
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM