-2

我想在谷歌地图上做标记后显示 alertDialog 因为我想问用户谷歌地图上的标记是否真实。何时显示删除功能的对话框非常相似。当我试图在谷歌地图上做标记时,我遇到了一些问题,谷歌地图和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 但它会显示一些错误。有什么我错过的吗?有人帮我吗?

4

1 回答 1

0

你可以使用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);
                  },
                ),
              ],
            )
          ],

        ));
    }

最后更改markers: Set<Marker>.of(markers.values)markers: markersGoogleMap 小部件

于 2020-11-22T18:42:15.193 回答