From 39578bcff3deb2847b06cef309e074e1c3f9ca62 Mon Sep 17 00:00:00 2001 From: "mr.mojtaba" Date: Sat, 12 Apr 2025 11:07:52 +0330 Subject: [PATCH] feat : map and generate fake location --- .../lib/data/utils/cluster_generator.dart | 109 ------------------ .../lib/data/utils/marker_generator.dart | 34 ++++++ .../lib/presentation/routes/app_pages.dart | 2 +- .../lib/presentation/routes/app_routes.dart | 6 +- 4 files changed, 38 insertions(+), 113 deletions(-) delete mode 100644 features/supervision/lib/data/utils/cluster_generator.dart diff --git a/features/supervision/lib/data/utils/cluster_generator.dart b/features/supervision/lib/data/utils/cluster_generator.dart deleted file mode 100644 index 18c5bcc..0000000 --- a/features/supervision/lib/data/utils/cluster_generator.dart +++ /dev/null @@ -1,109 +0,0 @@ -import 'dart:math'; -import 'package:flutter/foundation.dart'; -import 'package:latlong2/latlong.dart'; - -class ClusterParams { - final List points; - final double clusterRadiusMeters; - - ClusterParams({ - required this.points, - required this.clusterRadiusMeters, - }); -} - -class Cluster { - final LatLng center; - final List members; - - Cluster(this.center, this.members); -} -// Use a more efficient quadtree-based clustering algorithm -Future> clusterMarkersQuadtreeIsolate(ClusterParams params) async { - return compute(_clusterMarkersQuadtree, params); -} - -List _clusterMarkersQuadtree(ClusterParams params) { - final points = params.points; - final radius = params.clusterRadiusMeters; - final distance = const Distance(); - final List clusters = []; - - // Skip clustering if we have a small number of points - if (points.length < 100) { - return points.map((p) => Cluster(p, [p])).toList(); - } - - // Find bounds - double minLat = points[0].latitude; - double maxLat = points[0].latitude; - double minLng = points[0].longitude; - double maxLng = points[0].longitude; - - for (final point in points) { - minLat = min(minLat, point.latitude); - maxLat = max(maxLat, point.latitude); - minLng = min(minLng, point.longitude); - maxLng = max(maxLng, point.longitude); - } - - // Build spatial grid for faster lookups (simple spatial index) - // Convert geographic distance to approximate degrees - final double radiusDegLat = radius / 111000; // ~111km per degree latitude - final double radiusDegLng = radius / (111000 * cos(minLat * pi / 180)); // Adjust for longitude - - final int gridLatSize = ((maxLat - minLat) / radiusDegLat).ceil(); - final int gridLngSize = ((maxLng - minLng) / radiusDegLng).ceil(); - - // Create spatial grid - final List>> grid = List.generate( - gridLatSize + 1, - (_) => List.generate(gridLngSize + 1, (_) => []) - ); - - // Add points to grid cells - for (final point in points) { - final int latIdx = ((point.latitude - minLat) / radiusDegLat).floor(); - final int lngIdx = ((point.longitude - minLng) / radiusDegLng).floor(); - grid[latIdx][lngIdx].add(point); - } - - // Process grid cells in batches - final Set processed = {}; - - for (int latIdx = 0; latIdx < gridLatSize; latIdx++) { - for (int lngIdx = 0; lngIdx < gridLngSize; lngIdx++) { - final cellPoints = grid[latIdx][lngIdx]; - - for (final point in cellPoints) { - if (processed.contains(point)) continue; - - // Find nearby points - final List neighbors = []; - neighbors.add(point); - processed.add(point); - - // Check current and adjacent cells for neighbors - for (int adjLat = max(0, latIdx - 1); adjLat <= min(gridLatSize - 1, latIdx + 1); adjLat++) { - for (int adjLng = max(0, lngIdx - 1); adjLng <= min(gridLngSize - 1, lngIdx + 1); adjLng++) { - for (final neighbor in grid[adjLat][adjLng]) { - if (!processed.contains(neighbor) && distance(point, neighbor) <= radius) { - neighbors.add(neighbor); - processed.add(neighbor); - } - } - } - } - - // Calculate cluster center - if (neighbors.isNotEmpty) { - final avgLat = neighbors.map((p) => p.latitude).reduce((a, b) => a + b) / neighbors.length; - final avgLng = neighbors.map((p) => p.longitude).reduce((a, b) => a + b) / neighbors.length; - clusters.add(Cluster(LatLng(avgLat, avgLng), neighbors)); - } - } - } - } - - return clusters; -} \ No newline at end of file diff --git a/features/supervision/lib/data/utils/marker_generator.dart b/features/supervision/lib/data/utils/marker_generator.dart index c01e898..af3ad4a 100644 --- a/features/supervision/lib/data/utils/marker_generator.dart +++ b/features/supervision/lib/data/utils/marker_generator.dart @@ -1,7 +1,12 @@ +import 'dart:async'; +import 'dart:convert'; import 'dart:math'; + import 'package:flutter/foundation.dart'; import 'package:latlong2/latlong.dart'; + +/* class GridGenParams { final LatLng center; final int count; @@ -55,3 +60,32 @@ List _generateGridMarkersOptimized(GridGenParams params) { return result.sublist(0, min(result.length, params.count)); } +*/ +class LatLngSimple { + final double lat; + final double lng; + + LatLngSimple(this.lat, this.lng); + + Map toJson() => {'lat': lat, 'lng': lng}; +} + + +List generateLatLngInIsolate(int count) { + final Random random = Random(); + const double minLat = 35.610; + const double maxLat = 36.120; + const double minLng = 50.190; + const double maxLng = 51.100; + + return List.generate(count, (_) { + double lat = minLat + random.nextDouble() * (maxLat - minLat); + double lng = minLng + random.nextDouble() * (maxLng - minLng); + return LatLngSimple(lat, lng); + }); +} + +Future> generateLocationsUsingCompute(int count) async { + final result = await compute(generateLatLngInIsolate, count); + return result.map((e) => LatLng(e.lat, e.lng)).toList(); +} \ No newline at end of file diff --git a/features/supervision/lib/presentation/routes/app_pages.dart b/features/supervision/lib/presentation/routes/app_pages.dart index 12c2320..46dad2e 100644 --- a/features/supervision/lib/presentation/routes/app_pages.dart +++ b/features/supervision/lib/presentation/routes/app_pages.dart @@ -10,7 +10,7 @@ sealed class SupervisionPages { GetPage( name: SupervisionRoutes.supervision, page: () => SupervisionFilterPage(), - binding: BindingsBuilder.put(() => BazresiLogic()), + binding: BindingsBuilder.put(() => SupervisionLogic()), ), ]; } diff --git a/features/supervision/lib/presentation/routes/app_routes.dart b/features/supervision/lib/presentation/routes/app_routes.dart index bafe72c..37df78b 100644 --- a/features/supervision/lib/presentation/routes/app_routes.dart +++ b/features/supervision/lib/presentation/routes/app_routes.dart @@ -1,7 +1,7 @@ sealed class SupervisionRoutes { SupervisionRoutes._(); - static const supervision = '/supervision'; - -} \ No newline at end of file + static const supervisionAction = '/supervision/action'; + static const supervisionUserSetting = '/supervision/userSettings'; +}