177 lines
6.2 KiB
Dart
177 lines
6.2 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_new/api.dart';
|
|
import 'package:mymuseum_visitapp/Models/visitContext.dart';
|
|
import 'package:mymuseum_visitapp/Screens/Sections/Map/map_context.dart';
|
|
import 'package:mymuseum_visitapp/app_context.dart';
|
|
import 'package:provider/provider.dart';
|
|
import 'package:html/parser.dart' show parse;
|
|
|
|
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 = [];
|
|
bool init = false;
|
|
|
|
Set<Marker> getMarkers(language, mapContext) {
|
|
markers = {};
|
|
|
|
int i = 0;
|
|
pointsToShow!.forEach((point) {
|
|
if (point.title != null && point.title!.any((translation) => translation.language == language)) {
|
|
var translation = point.title!.firstWhere((translation) => translation.language == language);
|
|
var textSansHTML = parse(translation.value);
|
|
point.id = i;
|
|
var coords = _getPointLatLon(point);
|
|
if (coords != null) {
|
|
var icon = point.categorieId == null
|
|
? BitmapDescriptor.bytes(widget.icons.where((i) => i['id'] == null).first['icon'])
|
|
: widget.icons.any((i) => i['id'] == point.categorieId)
|
|
? BitmapDescriptor.bytes(widget.icons.where((i) => i['id'] == point.categorieId).first['icon'])
|
|
: BitmapDescriptor.bytes(widget.icons.where((i) => i['id'] == null).first['icon']);
|
|
|
|
markers.add(Marker(
|
|
draggable: false,
|
|
markerId: MarkerId(parse(textSansHTML.body!.text).documentElement!.text + coords.latitude.toString() + coords.longitude.toString()),
|
|
position: coords,
|
|
icon: icon,
|
|
onTap: () {
|
|
mapContext.setSelectedPoint(point);
|
|
(Provider.of<AppContext>(context, listen: false).getContext() as VisitAppContext)
|
|
.statisticsService?.track(
|
|
VisitEventType.mapPoiTap,
|
|
metadata: {
|
|
'geoPointId': point.id,
|
|
'geoPointTitle': parse(textSansHTML.body!.text).documentElement!.text,
|
|
},
|
|
);
|
|
},
|
|
infoWindow: InfoWindow.noText));
|
|
}
|
|
}
|
|
i++;
|
|
});
|
|
return markers;
|
|
}
|
|
|
|
LatLng? _getPointLatLon(GeoPointDTO point) {
|
|
if (point.geometry?.type == 'Point' && point.geometry?.coordinates != null && point.geometry!.coordinates! is List) {
|
|
final coords = point.geometry!.coordinates! as List;
|
|
if (coords.length >= 2) {
|
|
return LatLng(
|
|
(coords[1] as num).toDouble(),
|
|
(coords[0] as num).toDouble(),
|
|
);
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
super.dispose();
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final mapContext = Provider.of<MapContext>(context);
|
|
//final appContext = Provider.of<AppContext>(context);
|
|
|
|
pointsToShow = widget.geoPoints;
|
|
getMarkers(widget.language, mapContext);
|
|
|
|
MapType type = MapType.hybrid;
|
|
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;
|
|
}
|
|
}
|
|
|
|
return Stack(
|
|
children: [
|
|
Center(
|
|
child: GoogleMap(
|
|
mapType: type,
|
|
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),
|
|
zoom: widget.mapDTO.zoom != null ? widget.mapDTO.zoom!.toDouble() : 18,
|
|
),
|
|
onMapCreated: (GoogleMapController controller) {
|
|
if (!kIsWeb) {
|
|
_controller.complete(controller);
|
|
_GoogleMapcontroller = controller;
|
|
}
|
|
},
|
|
markers: markers,
|
|
onTap: (LatLng 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) {
|
|
_GoogleMapcontroller!.getZoomLevel().then((actualZoom) {
|
|
var zoomToNavigate = actualZoom <= 12.0 ? 15.0 : actualZoom;
|
|
var coords = _getPointLatLon(geopoint);
|
|
if (coords != null) {
|
|
_GoogleMapcontroller!.animateCamera(CameraUpdate.newCameraPosition(
|
|
CameraPosition(
|
|
target: coords,
|
|
tilt: 0.0,
|
|
bearing: 0.0,
|
|
zoom: zoomToNavigate
|
|
)
|
|
));
|
|
}
|
|
});
|
|
}
|
|
return SizedBox();
|
|
},
|
|
);
|
|
}
|
|
),
|
|
)
|
|
],
|
|
);
|
|
}
|
|
} |