tablet-app/lib/Screens/Map/google_map_view.dart

234 lines
8.9 KiB
Dart

import 'dart:async';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:manager_api/api.dart';
import 'package:provider/provider.dart';
import 'package:tablet_app/Models/tabletContext.dart';
import 'package:tablet_app/Screens/Map/map_context.dart';
import 'package:html/parser.dart' show parse;
import 'package:tablet_app/app_context.dart';
class GoogleMapView extends StatefulWidget {
final MapDTO mapDTO;
final List<GeoPointDTO> geoPoints;
final List<Map<String, dynamic>> icons;
final String? language;
const GoogleMapView({
Key? key,
required this.mapDTO,
required this.geoPoints,
required this.icons,
this.language,
}) : super(key: key);
@override
_GoogleMapViewState createState() => _GoogleMapViewState();
}
class _GoogleMapViewState extends State<GoogleMapView> {
ConfigurationDTO? configurationDTO;
Completer<GoogleMapController> _controller = Completer();
GoogleMapController? _GoogleMapcontroller;
Set<Marker> markers = {};
List<GeoPointDTO>? pointsToShow = [];
//List<String>? selectedCategories = [];
bool init = false;
Set<Marker> getMarkers(language, mapContext) {
markers = {};
int i = 0;
pointsToShow!.forEach((point) {
var textSansHTML = parse(point.title!.firstWhere((translation) => translation.language == language).value);
point.id = i;
/*var mapMarker = new MapMarker(
id: point.id,
title: parse(textSansHTML.body!.text).documentElement!.text,
description: point.description!.firstWhere((translation) => translation.language == language).value,
longitude: point.longitude,
latitude: point.latitude,
contents: point.contents
);*/
if (point.latitude != null && point.longitude != null) {
var icon = point.categorie == null ? BitmapDescriptor.fromBytes(widget.icons.where((i) => i['id'] == null).first['icon']) : widget.icons.any((i) => i['id'] == point.categorieId) ? BitmapDescriptor.fromBytes(widget.icons.where((i) => i['id'] == point.categorieId).first['icon']) : BitmapDescriptor.fromBytes(widget.icons.where((i) => i['id'] == null).first['icon']); //widget.selectedMarkerIcon,;
markers.add(Marker(
draggable: false,
markerId: MarkerId(parse(textSansHTML.body!.text).documentElement!.text + point.latitude! + point.longitude!),
position: LatLng(
double.tryParse(point.latitude!)!,
double.tryParse(point.longitude!)!,
),
icon: icon, //widget.selectedMarkerIcon,
//widget.selectedMarkerIcon != null ? BitmapDescriptor.fromBytes(widget.selectedMarkerIcon!) : BitmapDescriptor.defaultMarker,
/*icon: BitmapDescriptor.defaultMarkerWithHue(
BitmapDescriptor.hueYellow,
),*/
onTap: () {
//setState(() {
mapContext.setSelectedPoint(point);
mapContext.setSelectedPointForNavigate(point);
//});
},
infoWindow: InfoWindow.noText));
}
i++;
});
return markers;
}
@override
void initState() {
//selectedCategories = widget.mapDTO.categories!.map((cat) => cat.label!.firstWhere((element) => element.language == widget.language).value!).toList();
super.initState();
}
@override
void dispose() {
// TODO: implement dispose
super.dispose();
}
@override
Widget build(BuildContext context) {
final mapContext = Provider.of<MapContext>(context);
final appContext = Provider.of<AppContext>(context);
TabletAppContext tabletAppContext = appContext.getContext() as TabletAppContext;
Size size = MediaQuery.of(context).size;
pointsToShow = widget.geoPoints;
/*if(!init) {
print("getmarkers in build");*/
getMarkers(widget.language, mapContext);
/* init = true;
}*/
//MapTypeApp? mapTypeApp = MapTypeApp.fromJson(widget.mapDTO!.mapType!.value);
//print(mapTypeApp.toString());
MapType type = MapType.hybrid;
//if(kIsWeb) {
if(widget.mapDTO.mapType != null) {
switch(widget.mapDTO.mapType!.value) {
case 0:
type = MapType.none;
break;
case 1:
type = MapType.normal;
break;
case 2:
type = MapType.satellite;
break;
case 3:
type = MapType.terrain;
break;
case 4:
type = MapType.hybrid;
break;
}
}
/*} else {
print("is OTHEER");
type = EnumToString.fromString(MapType.values, widget.mapDTO!.mapType.toString()) != null ? EnumToString.fromString(MapType.values, widget.mapDTO!.mapType.toString())! : MapType.hybrid;
}*/
//MapType type = EnumToString.fromString(MapType.values, widget.mapDTO!.mapType.toString()) != null ? EnumToString.fromString(MapType.values, widget.mapDTO!.mapType.toString())! : MapType.hybrid;
return Stack(
children: [
Center(
child: GoogleMap(
mapType: type, // widget.mapDTO!.mapType != null ? EnumToString.fromString(MapType.values, widget.mapDTO!.mapType.toString())!: MapType.hybrid,
mapToolbarEnabled: false,
indoorViewEnabled: false,
initialCameraPosition: CameraPosition(
target: widget.mapDTO.longitude != null && widget.mapDTO.latitude != null ? LatLng(double.tryParse(widget.mapDTO.latitude!)!, double.tryParse(widget.mapDTO.longitude!)!) : LatLng(50.465503, 4.865105) , // MDLF 50.416639, 4.879169 / Namur 50.465503, 4.865105
zoom: widget.mapDTO.zoom != null ? widget.mapDTO.zoom!.toDouble() : 18,
),
onMapCreated: (GoogleMapController controller) {
if(kIsWeb) {
//_controllerWeb.complete(controller);
} else {
_controller.complete(controller);
_GoogleMapcontroller = controller;
}
},
markers: markers,
onTap: (LatLng location) {
print(location);
mapContext.setSelectedPoint(null);
mapContext.setSelectedPointForNavigate(null);
},
),
),
Container(
child: Builder(
builder: (context) {
return Consumer<MapContext>(
builder: (context, mapContext, _) {
var geopoint = mapContext.getSelectedPointForNavigate() as GeoPointDTO?;
if (geopoint != null && _GoogleMapcontroller != null) {
print("COUCOU IL FAUT NAVUGATE");
_GoogleMapcontroller!.animateCamera(CameraUpdate.newCameraPosition(
CameraPosition(
target: LatLng(double.tryParse(geopoint.latitude!)!, double.tryParse(geopoint.longitude!)!),
tilt: 0.0,
bearing: 0.0,
zoom: 16.0
)
));
}
return SizedBox();
},
);
}
),
)
/*Positioned(
left: 5,
top: 35,
child: SizedBox(
width: size.width * 0.3,
height: size.height * 0.76,
child: GeoPointFilter(
language: tabletAppContext.language!,
geoPoints: widget.mapDTO!.points!,
categories: widget.mapDTO!.categories!,
filteredPoints: (filteredPoints) {
print("COUCOU FILTERED POINTS");
print(filteredPoints);
}),
),
),
Positioned(
bottom: 35,
left: 10,
child: SizedBox(
width: size.width * 0.75,
child: MultiSelectContainer(
label: null,
color: kBackgroundGrey,
initialValue: selectedCategories!,
isMultiple: true,
values: widget.mapDTO!.categories!.map((categorie) => categorie.label!.firstWhere((element) => element.language == widget.language).value!).toList(),
onChanged: (value) {
var tempOutput = new List<String>.from(value);
print(tempOutput);
if(init) {
selectedCategories = tempOutput;
pointsToShow = widget.mapDTO!.points!.where((point) => tempOutput.any((tps) => point.categorie?.label?.firstWhere((lab) => lab.language == widget.language).value == tps) || point.categorie == null).toList();
setState(() {
markers = getMarkers(widget.language, mapContext);
mapContext.notifyListeners();
});
}
},
),
),
),*/
],
);
}
}