如何在 google 地图上标记后实现 alertDialog?

How to implement alertDialog after marker on google maps in flutter?

我想在 google 地图上做标记后显示 alertDialog,因为我想询问用户 google 地图上的标记是否真实。当显示删除功能对话框时,它非常相似。当我试图在 google 地图上做标记时遇到了一些问题,google 地图上的标记和 alerDialog 没有显示。这是我的代码。

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:fyp/model/Location.dart';
import 'package:fyp/model/Task.dart';
import 'package:fyp/service/database.dart';
import 'package:geoflutterfire/geoflutterfire.dart';
import 'package:geolocator/geolocator.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:geocoder/geocoder.dart' as geoCo;





class Geolocation extends StatefulWidget {
  final Task task;

  const Geolocation({Key key, this.task}) : super(key: key);

  @override
  _GeolocationState createState() => _GeolocationState(task) ;
}

class _GeolocationState extends State<Geolocation> {
  Task task;
  _GeolocationState(Task task){
    this.task = task;
  }

  GoogleMapController googleMapController;
  Map<MarkerId, Marker> markers = <MarkerId, Marker>{};
  Position position;
  String addressLocation;
  LocationTask loc;



  void getMarkers(double lat, double long) {
    MarkerId markerId = MarkerId(lat.toString() + long.toString());
    Marker _marker = Marker(
        draggable: false,
        markerId: markerId,
        position: LatLng(lat, long),
        zIndex: 2,
        icon: BitmapDescriptor.defaultMarkerWithHue(BitmapDescriptor.hueCyan),
        infoWindow: InfoWindow(snippet: addressLocation));
    setState(() {
      markers[markerId] = _marker;
    });
  }

  getLocation(tapped) async{
    final coordinated = new geoCo.Coordinates(tapped.latitude, tapped.longitude);
    final FirebaseAuth auth = FirebaseAuth.instance;
    final FirebaseUser rd = await auth.currentUser();
    final String email = rd.email;
    var address = await geoCo.Geocoder.local.findAddressesFromCoordinates(coordinated);
    GeoFirePoint firePoint = new GeoFirePoint(tapped.latitude, tapped.longitude);
    var firstAddress = address.first;
    addressLocation = firstAddress.addressLine;
    DateTime _dateTime = DateTime.now();
    String id = Firestore.instance
        .collection("Location")
        .document()
        .documentID;
    loc = LocationTask(
        dateTime: _dateTime,
        email: email,
        docId: id,
        address: addressLocation,
        position: firePoint.geoPoint,
        noAduan: task.noAduan,
        kategori: task.kategori
    );
    await DatabaseService().addlocation(loc);
  }

  @override
  void dispose() {
    super.dispose();
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Google Maps"),
        backgroundColor: Colors.redAccent,
      ),
      body:  Container(
        child: GoogleMap(
            onTap: (tapped)  {
             getData(tapped);
            },
            mapType: MapType.normal,
            compassEnabled: true,
            myLocationButtonEnabled: true,
            onMapCreated: (GoogleMapController controller) {
              setState(() {
                googleMapController = controller;
              });
            },
            initialCameraPosition: CameraPosition(
                target: LatLng(3.12803968168713, 101.5910096431367),
                zoom: 15.0),
            markers: Set<Marker>.of(markers.values)
        ),
      ),
    );
  }
  getData(tapped){
        return AlertDialog(
          title: Text("Is there true your location"),
          content: Text("$addressLocation"),
          actions: [
            Row(
              children: [
                FlatButton(
                  child: Text('Yes'),
                  onPressed: () {
                    getLocation(tapped);
                  },
                ),
                FlatButton(
                  child: Text('No'),
                  onPressed: () {
                    Navigator.pop(context);
                  },
                ),
              ],
            )
          ],

        );
    }

  }

有没有因为我多次“点击”传递参数?还是因为我没有使用 showDialog?我曾尝试使用 showDialog 和 alertDialog 但它会显示一些错误。有什么我错过的吗?有人帮帮我吗?

你可以使用 Set<Marker> 而不是 Map<Marker,MarkerId>

声明标记集Set<Marker> markers = Set<Marker>();

显示标记和警告对话框将 getMarker 方法更改为:

   void getMarkers(double lat, double long) {
        MarkerId markerId = MarkerId(lat.toString() + long.toString());
        Marker _marker = Marker(
            draggable: false,
            markerId: markerId,
            position: LatLng(lat, long),
            zIndex: 2,
            icon: BitmapDescriptor.defaultMarkerWithHue(BitmapDescriptor.hueCyan),
            infoWindow: InfoWindow(snippet: addressLocation));
        setState(() {
          markers.add(_marker);
        });
getData(_marker.position);
      }

并将 getData 方法更改为:

getData(tapped){

        showDialog(
                context:context,
child:
     AlertDialog(
          title: Text("Is there true your location"),
          content: Text("$addressLocation"),
          actions: [
            Row(
              children: [
                FlatButton(
                  child: Text('Yes'),
                  onPressed: () {
                    getLocation(tapped);
                  },
                ),
                FlatButton(
                  child: Text('No'),
                  onPressed: () {
                    Navigator.pop(context);
                  },
                ),
              ],
            )
          ],

        ));
    }

最终在 GoogleMap 小部件中将 markers: Set<Marker>.of(markers.values) 更改为 markers: markers