Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Maps caching #695

Merged
merged 2 commits into from
Feb 24, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 66 additions & 34 deletions uni/lib/view/locations/widgets/map.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import 'package:flutter/material.dart';
import 'package:flutter_map/flutter_map.dart';
import 'package:flutter_map_marker_popup/flutter_map_marker_popup.dart';
import 'package:latlong2/latlong.dart';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:url_launcher/url_launcher.dart';

import 'package:uni/model/entities/location_group.dart';
import 'package:uni/view/locations/widgets/floorless_marker_popup.dart';
Expand All @@ -26,42 +28,61 @@ class LocationsMap extends StatelessWidget {
@override
Widget build(BuildContext context) {
return FlutterMap(
options: MapOptions(
minZoom: 17,
maxZoom: 18,
nePanBoundary: northEastBoundary,
swPanBoundary: southWestBoundary,
center: center,
zoom: 17.5,
rotation: 0,
interactiveFlags: InteractiveFlag.all - InteractiveFlag.rotate,
onTap: (tapPosition, latlng) => _popupLayerController.hideAllPopups(),
),
children: <Widget>[
TileLayerWidget(
options: TileLayerOptions(
urlTemplate: 'https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png',
subdomains: <String>['a', 'b', 'c'],
),
options: MapOptions(
minZoom: 17,
maxZoom: 18,
nePanBoundary: northEastBoundary,
swPanBoundary: southWestBoundary,
center: center,
zoom: 17.5,
rotation: 0,
interactiveFlags: InteractiveFlag.all - InteractiveFlag.rotate,
onTap: (tapPosition, latlng) => _popupLayerController.hideAllPopups(),
),
PopupMarkerLayerWidget(
options: PopupMarkerLayerOptions(
markers: _getMarkers(),
popupController: _popupLayerController,
popupAnimation: const PopupAnimation.fade(
duration: Duration(milliseconds: 400)),
popupBuilder: (_, Marker marker) {
if (marker is LocationMarker) {
return marker.locationGroup.isFloorless
? FloorlessLocationMarkerPopup(marker.locationGroup)
: LocationMarkerPopup(marker.locationGroup);
}
return const Card(child: Text('undefined'));
},
nonRotatedChildren: [
Align(
alignment: Alignment.bottomRight,
child: ColoredBox(
color: Theme.of(context).colorScheme.onPrimary.withOpacity(0.8),
child: GestureDetector(
onTap: () => launchUrl(
Uri(host: 'openstreetmap.org', path: '/copyright')),
child: const Padding(
padding: EdgeInsets.symmetric(vertical: 5, horizontal: 8),
child: MouseRegion(
cursor: SystemMouseCursors.click,
child: Text("© OpenStreetMap"),
),
),
),
),
)
],
children: <Widget>[
TileLayerWidget(
options: TileLayerOptions(
urlTemplate: 'https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png',
subdomains: <String>['a', 'b', 'c'],
tileProvider: CachedTileProvider(),
),
),
),
],
);
PopupMarkerLayerWidget(
options: PopupMarkerLayerOptions(
markers: _getMarkers(),
popupController: _popupLayerController,
popupAnimation: const PopupAnimation.fade(
duration: Duration(milliseconds: 400)),
popupBuilder: (_, Marker marker) {
if (marker is LocationMarker) {
return marker.locationGroup.isFloorless
? FloorlessLocationMarkerPopup(marker.locationGroup)
: LocationMarkerPopup(marker.locationGroup);
}
return const Card(child: Text('undefined'));
},
),
),
]);
}

List<Marker> _getMarkers() {
Expand All @@ -70,3 +91,14 @@ class LocationsMap extends StatelessWidget {
}).toList();
}
}

class CachedTileProvider extends TileProvider {
CachedTileProvider();

@override
ImageProvider getImage(Coords<num> coords, TileLayerOptions options) {
return CachedNetworkImageProvider(
getTileUrl(coords, options),
);
}
}