我想在谷歌地图上做标记后显示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,但它会显示一些错误。我遗漏了什么吗?有人帮我吗?
您可以使用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