Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Own photo map marker clustering

Neeraj Gupta5cf2f708b964
diff --git a/mobile/apps/photos/lib/ui/map/map_gallery_tile.dart b/mobile/apps/photos/lib/ui/map/map_gallery_tile.dart
deleted file mode 100644
index fdb774b5f0..0000000000
--- a/mobile/apps/photos/lib/ui/map/map_gallery_tile.dart
+++ /dev/null
@@ -1,20 +0,0 @@
-import "package:flutter/material.dart";
-import "package:photos/ui/map/image_marker.dart";
-import "package:photos/ui/map/marker_image.dart";
-
-class MapGalleryTile extends StatelessWidget {
- final ImageMarker imageMarker;
-
- const MapGalleryTile({super.key, required this.imageMarker});
- @override
- Widget build(BuildContext context) {
- return Container(
- margin: const EdgeInsets.all(8),
- child: MarkerImage(
- key: super.key,
- file: imageMarker.imageFile,
- seperator: 69,
- ),
- );
- }
-}
diff --git a/mobile/apps/photos/lib/ui/map/map_isolate.dart b/mobile/apps/photos/lib/ui/map/map_isolate.dart
index ba6c7edf20..b0fce0af0e 100644
--- a/mobile/apps/photos/lib/ui/map/map_isolate.dart
+++ b/mobile/apps/photos/lib/ui/map/map_isolate.dart
@@ -28,15 +28,30 @@ class MapMarkerGroup {
required this.longitude,
required this.imageCount,
});
+}
- MapMarkerGroup withIncrementedCount() {
- return MapMarkerGroup(
- imageIndex: imageIndex,
- latitude: latitude,
- longitude: longitude,
- imageCount: imageCount + 1,
- );
- }
+class _ProjectedMarkerGroup {
+ final int imageIndex;
+ final double latitude;
+ final double longitude;
+ final double x;
+ final double y;
+ int imageCount = 1;
+
+ _ProjectedMarkerGroup({
+ required this.imageIndex,
+ required this.latitude,
+ required this.longitude,
+ required this.x,
+ required this.y,
+ });
+
+ MapMarkerGroup toMarkerGroup() => MapMarkerGroup(
+ imageIndex: imageIndex,
+ latitude: latitude,
+ longitude: longitude,
+ imageCount: imageCount,
+ );
}
class MapWorkerInit {
@@ -70,7 +85,7 @@ class MapViewportResult {
});
}
-const double _markerBucketSizePixels = 64;
+const double _minimumMarkerDistancePixels = 100;
const double _webMercatorTileSize = 256;
const double _mercatorLatitudeLimit = 85.05112878;
@@ -83,8 +98,9 @@ void mapWorker(MapWorkerInit init) {
}
final visibleIndexes = <int>[];
- final groups = <(int, int), MapMarkerGroup>{};
- final scale = _webMercatorTileSize * pow(2, message.zoom);
+ final groupsByCell = <(int, int), List<_ProjectedMarkerGroup>>{};
+ final groups = <_ProjectedMarkerGroup>[];
+ final scale = _webMercatorTileSize * pow(2, message.zoom).toDouble();
for (final point in init.points) {
final latLng = LatLng(point.latitude, point.longitude);
@@ -101,26 +117,60 @@ void mapWorker(MapWorkerInit init) {
final x = (point.longitude + 180) / 360 * scale;
final y =
(0.5 - log((1 + sinLatitude) / (1 - sinLatitude)) / (4 * pi)) * scale;
- final key = (
- (x / _markerBucketSizePixels).floor(),
- (y / _markerBucketSizePixels).floor(),
+ final cell = (
+ (x / _minimumMarkerDistancePixels).floor(),
+ (y / _minimumMarkerDistancePixels).floor(),
+ );
+ _ProjectedMarkerGroup? closestGroup;
+ var closestDistanceSquared = double.infinity;
+
+ for (var xOffset = -1; xOffset <= 1; xOffset++) {
+ for (var yOffset = -1; yOffset <= 1; yOffset++) {
+ final nearbyGroups =
+ groupsByCell[(cell.$1 + xOffset, cell.$2 + yOffset)];
+ if (nearbyGroups == null) {
+ continue;
+ }
+
+ for (final group in nearbyGroups) {
+ final xDistance = x - group.x;
+ final yDistance = y - group.y;
+ final distanceSquared =
+ xDistance * xDistance + yDistance * yDistance;
+ if (distanceSquared <=
+ _minimumMarkerDistancePixels *
+ _minimumMarkerDistancePixels &&
+ distanceSquared < closestDistanceSquared) {
+ closestGroup = group;
+ closestDistanceSquared = distanceSquared;
+ }
+ }
+ }
+ }
+
+ if (closestGroup != null) {
+ closestGroup.imageCount++;
+ continue;
+ }
+
+ final group = _ProjectedMarkerGroup(
+ imageIndex: point.imageIndex,
+ latitude: point.latitude,
+ longitude: point.longitude,
+ x: x,
+ y: y,
);
- final group = groups[key];
- groups[key] = group == null
- ? MapMarkerGroup(
- imageIndex: point.imageIndex,
- latitude: point.latitude,
- longitude: point.longitude,
- imageCount: 1,
- )
- : group.withIncrementedCount();
+ groups.add(group);
+ groupsByCell.putIfAbsent(cell, () => []).add(group);
}
init.sendPort.send(
MapViewportResult(
id: message.id,
visibleImageIndexes: visibleIndexes,
- markerGroups: groups.values.toList(growable: false),
+ markerGroups: groups
+ .map((group) => group.toMarkerGroup())
+ .toList(growable: false),
),
);
});
diff --git a/mobile/apps/photos/lib/ui/map/map_screen.dart b/mobile/apps/photos/lib/ui/map/map_screen.dart
index a12e40c243..da83149c92 100644
--- a/mobile/apps/photos/lib/ui/map/map_screen.dart
+++ b/mobile/apps/photos/lib/ui/map/map_screen.dart
@@ -137,11 +137,10 @@ class _MapScreenState extends State<MapScreen> {
}
Future<void> _startMapWorker(List<MapPoint> mapPoints) async {
- _mapWorkerReceivePort = ReceivePort();
+ final receivePort = ReceivePort();
final sendPortCompleter = Completer<SendPort>();
- _mapWorkerSubscription = _mapWorkerReceivePort!.listen((message) {
+ final subscription = receivePort.listen((message) {
if (message is SendPort) {
- _mapWorkerSendPort = message;
if (!sendPortCompleter.isCompleted) {
sendPortCompleter.complete(message);
}
@@ -149,14 +148,23 @@ class _MapScreenState extends State<MapScreen> {
_applyViewportResult(message);
}
});
- _mapWorkerIsolate = await Isolate.spawn(
+ final isolate = await Isolate.spawn(
mapWorker,
- MapWorkerInit(
- points: mapPoints,
- sendPort: _mapWorkerReceivePort!.sendPort,
- ),
+ MapWorkerInit(points: mapPoints, sendPort: receivePort.sendPort),
);
- await sendPortCompleter.future;
+ final sendPort = await sendPortCompleter.future;
+
+ if (!mounted) {
+ await subscription.cancel();
+ receivePort.close();
+ isolate.kill();
+ return;
+ }
+
+ _mapWorkerReceivePort = receivePort;
+ _mapWorkerSubscription = subscription;
+ _mapWorkerSendPort = sendPort;
+ _mapWorkerIsolate = isolate;
}
void _requestViewport(LatLngBounds bounds, double zoom) {
diff --git a/mobile/apps/photos/lib/ui/map/map_view.dart b/mobile/apps/photos/lib/ui/map/map_view.dart
index b5dcf8059d..e2ca1c85c3 100644
--- a/mobile/apps/photos/lib/ui/map/map_view.dart
+++ b/mobile/apps/photos/lib/ui/map/map_view.dart
@@ -3,12 +3,9 @@ import "dart:io" show Platform;
import "package:ente_pure_utils/ente_pure_utils.dart";
import "package:flutter/material.dart";
import "package:flutter_map/flutter_map.dart";
-import "package:flutter_map_marker_cluster/flutter_map_marker_cluster.dart";
import "package:latlong2/latlong.dart";
import "package:photos/ui/map/image_marker.dart";
import "package:photos/ui/map/map_button.dart";
-import "package:photos/ui/map/map_gallery_tile.dart";
-import "package:photos/ui/map/map_gallery_tile_badge.dart";
import "package:photos/ui/map/map_marker.dart";
import "package:photos/ui/map/tile/layers.dart";
import "package:url_launcher/url_launcher.dart";
@@ -84,14 +81,14 @@ class _MapViewState extends State<MapView> {
@override
void initState() {
super.initState();
- _markers = _buildMakers();
+ _markers = _buildMarkers();
}
@override
void didUpdateWidget(MapView oldWidget) {
super.didUpdateWidget(oldWidget);
if (!identical(oldWidget.imageMarkers, widget.imageMarkers)) {
- _markers = _buildMakers();
+ _markers = _buildMarkers();
}
}
@@ -134,44 +131,7 @@ class _MapViewState extends State<MapView> {
),
children: [
const OSMTileLayer(),
- MarkerClusterLayerWidget(
- options: MarkerClusterLayerOptions(
- alignment: Alignment.topCenter,
- maxClusterRadius: 100,
- showPolygon: false,
- spiderfyCluster: false,
- size: widget.markerSize,
- padding: const EdgeInsets.all(80),
- markers: _markers,
- onClusterTap: (_) {
- onChange(
- widget.controller.camera.visibleBounds,
- widget.controller.camera.zoom,
- );
- },
- builder: (context, List<Marker> markers) {
- final valueKey = markers.first.key as ValueKey;
- final index = valueKey.value as int;
-
- final imageCount = markers.fold<int>(0, (count, marker) {
- final markerIndex = (marker.key! as ValueKey<int>).value;
- return count + widget.imageMarkers[markerIndex].imageCount;
- });
- final clusterKey = 'map-badge-$index-len-$imageCount';
-
- return Stack(
- key: ValueKey(clusterKey),
- children: [
- MapGalleryTile(
- key: Key(markers.first.key.toString()),
- imageMarker: widget.imageMarkers[index],
- ),
- MapGalleryTileBadge(size: imageCount),
- ],
- );
- },
- ),
- ),
+ MarkerLayer(markers: _markers),
Padding(
padding: EdgeInsets.only(
bottom: widget.bottomSheetDraggableAreaHeight,
@@ -247,7 +207,7 @@ class _MapViewState extends State<MapView> {
);
}
- List<Marker> _buildMakers() {
+ List<Marker> _buildMarkers() {
return List<Marker>.generate(widget.imageMarkers.length, (index) {
final imageMarker = widget.imageMarkers[index];
return mapMarker(
diff --git a/mobile/apps/photos/pubspec.yaml b/mobile/apps/photos/pubspec.yaml
index 93c4ab5047..9ad2a54ca4 100644
--- a/mobile/apps/photos/pubspec.yaml
+++ b/mobile/apps/photos/pubspec.yaml
@@ -108,7 +108,6 @@ dependencies:
flutter_localizations:
sdk: flutter
flutter_map: 7.0.2
- flutter_map_marker_cluster: 1.4.0
flutter_rust_bridge: 2.12.0
# Version 9.2.4 leaves lock-screen keys behind after an app reinstall.
# Upgrade only after this is fixed or with a migration.
diff --git a/mobile/pubspec.lock b/mobile/pubspec.lock
index d791bdc3ab..48d1d49fab 100644
--- a/mobile/pubspec.lock
+++ b/mobile/pubspec.lock
@@ -41,14 +41,6 @@ packages:
url: "https://pub.dev"
source: hosted
version: "5.3.0"
- animated_stack_widget:
- dependency: transitive
- description:
- name: animated_stack_widget
- sha256: ce4788dd158768c9d4388354b6fb72600b78e041a37afc4c279c63ecafcb9408
- url: "https://pub.dev"
- source: hosted
- version: "0.0.4"
ansicolor:
dependency: transitive
description:
@@ -1036,22 +1028,6 @@ packages:
url: "https://pub.dev"
source: hosted
version: "7.0.2"
- flutter_map_marker_cluster:
- dependency: transitive
- description:
- name: flutter_map_marker_cluster
- sha256: "2c1fb4d7a2105c4bbeb89be215320507f4b71b2036df4341fab9d2aa677d3ae9"
- url: "https://pub.dev"
- source: hosted
- version: "1.4.0"
- flutter_map_marker_popup:
- dependency: transitive
- description:
- name: flutter_map_marker_popup
- sha256: a7540538114b5d1627ab67b498273d66bc36090385412ae49ef215af4a2861c5
- url: "https://pub.dev"
- source: hosted
- version: "7.0.0"
flutter_native_splash:
dependency: transitive
description: