我使用 GetX 的实现,但对所有人理解逻辑都很有用:
控制器:
class MapController extends GetxController {
Map<MarkerId, Marker> markers = <MarkerId, Marker>{}.obs;
@override
void onInit() {
super.onInit();
generateMarkers();
}
generateMarkers() {
int count = 0;
for (var item in _markers) {
count += 1;
final String markerIdVal = 'marker_id_' + count.toString();
final MarkerId markerId = MarkerId(markerIdVal);
final Marker marker = Marker(
markerId: markerId,
position: LatLng(
double.parse(item[1].toString()),
double.parse(item[2].toString()),
),
infoWindow:
InfoWindow(title: item[0].toString(), snippet: 'Content'),
);
markers[markerId] = marker;
}
}
final _markers = [
[
'Rektörlük',
40.972877,
29.152653,
'/assets/img/number_1_3.png',
'rektorluk'
],
[
'Mühendislik',
40.972443,
29.151457,
'/assets/img/number_2_3.png',
'muhendislik'
]
// Continues
];
}
看法:
class MapView extends GetView<MapController> {
final Completer<GoogleMapController> _controller = Completer();
static final CameraPosition _kYeditepe =
CameraPosition(target: LatLng(40.971991, 29.152793), zoom: 17, tilt: 17);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: backAppBar(context, 'MAP'.tr),
body: GoogleMap(
mapType: MapType.normal,
initialCameraPosition: _kYeditepe,
markers: Set<Marker>.of(controller.markers.values),
onMapCreated: (GoogleMapController mapController) {
rootBundle.loadString('assets/map_styles.txt').then((string) {
String _mapStyle = string;
mapController.setMapStyle(_mapStyle);
_controller.complete(mapController);
});
},
),
);
}
}