feat : Cluster Map

This commit is contained in:
2025-07-29 10:50:18 +03:30
parent 8acd951c05
commit 716dd6e70a
7 changed files with 225 additions and 75 deletions

View File

@@ -1,4 +1,5 @@
import 'dart:async';
import 'dart:math';
import 'package:flutter/material.dart';
import 'package:rasadyar_core/core.dart';
@@ -11,12 +12,12 @@ import '../filter/view.dart';
class InspectionMapLogic extends GetxController with GetTickerProviderStateMixin {
final BaseLogic baseLogic = Get.find<BaseLogic>();
final distance = Distance();
Rx<LatLng> currentLocation = LatLng(34.798315281272544, 48.51479142983491).obs;
Rx<Resource<List<PoultryLocationModel>>> allPoultryLocation =
Resource<List<PoultryLocationModel>>.loading().obs;
RxList<PoultryLocationModel> markers = <PoultryLocationModel>[].obs;
RxList<Marker> markers = <Marker>[].obs;
Timer? _debounceTimer;
RxBool isLoading = false.obs;
@@ -25,6 +26,7 @@ class InspectionMapLogic extends GetxController with GetTickerProviderStateMixin
RxInt filterIndex = 0.obs;
RxInt showIndex = 0.obs;
bool showSlideHint = true;
RxInt currentZoom = 15.obs;
late Rx<SlidableController> slidController;
@@ -114,17 +116,28 @@ class InspectionMapLogic extends GetxController with GetTickerProviderStateMixin
isLoading.value = false;
}
void debouncedUpdateVisibleMarkers({required LatLng center}) {
void debouncedUpdateVisibleMarkers({required LatLng center, required double zoom}) {
_debounceTimer?.cancel();
_debounceTimer = Timer(const Duration(milliseconds: 300), () {
var raduis = getVisibleRadiusKm(
zoom: zoom,
screenWidthPx: Get.width.toDouble(),
latitude: center.latitude,
);
final filtered = filterNearbyMarkers(
allPoultryLocation.value.data ?? [],
center.latitude,
center.longitude,
10000, // Radius in meters
raduis * 1000, // Radius in meters
);
markers.assignAll(
filtered.map(
(e) => Marker(
point: LatLng(e.lat ?? 0, e.long ?? 0),
child: Icon(Icons.location_on, color: Colors.red),
),
),
);
markers.addAll(filtered);
});
}
@@ -135,11 +148,10 @@ class InspectionMapLogic extends GetxController with GetTickerProviderStateMixin
double radiusInMeters,
) {
final center = LatLng(centerLat, centerLng);
final distance = Distance();
return allMarkers.where((marker) {
var tmp =LatLng(marker.lat ?? 0, marker.long ?? 0);
return distance(center,tmp ) <= radiusInMeters;
var tmp = LatLng(marker.lat ?? 0, marker.long ?? 0);
return distance(center, tmp) <= radiusInMeters;
}).toList();
}
@@ -155,7 +167,11 @@ class InspectionMapLogic extends GetxController with GetTickerProviderStateMixin
isLoading.value = true;
allPoultryLocation.value = Resource<List<PoultryLocationModel>>.loading();
await safeCall(
call: () => inspectionRepository.getNearbyLocation(),
call: () => inspectionRepository.getNearbyLocation(
centerLat: currentLocation.value.latitude,
centerLng: currentLocation.value.longitude,
radius: 15, // Radius in K meters
),
onSuccess: (result) {
if (result != null) {
allPoultryLocation.value = Resource<List<PoultryLocationModel>>.success(result);
@@ -170,4 +186,14 @@ class InspectionMapLogic extends GetxController with GetTickerProviderStateMixin
},
);
}
double getVisibleRadiusKm({
required double zoom,
required double screenWidthPx,
required double latitude,
}) {
double metersPerPixel = 156543.03392 * cos(latitude * pi / 180) / pow(2, zoom);
double visibleWidthInMeters = metersPerPixel * screenWidthPx;
return (visibleWidthInMeters / 2) / 1000; // radius in KM
}
}

View File

@@ -1,6 +1,5 @@
import 'package:flutter/material.dart';
import 'package:rasadyar_core/core.dart';
import 'package:rasadyar_inspection/presentation/pages/filter/view.dart';
import 'package:rasadyar_inspection/presentation/routes/app_routes.dart';
import 'package:rasadyar_inspection/presentation/widget/base_page/view.dart';
import 'package:rasadyar_inspection/presentation/widget/custom_chips.dart';
@@ -39,40 +38,44 @@ class InspectionMapPage extends GetView<InspectionMapLogic> {
mapController: controller.animatedMapController.mapController,
options: MapOptions(
initialCenter: currentLocation.value,
initialZoom: 18,
interactionOptions: const InteractionOptions(
flags: InteractiveFlag.all & ~InteractiveFlag.rotate,
),
initialZoom: 15,
onPositionChanged: (camera, hasGesture) {
controller.debouncedUpdateVisibleMarkers(center: camera.center);
controller.debouncedUpdateVisibleMarkers(center: camera.center,zoom: camera.zoom);
},
),
children: [
TileLayer(
urlTemplate: 'https://tile.openstreetmap.org/{z}/{x}/{y}.png',
userAgentPackageName: 'ir.mnpc.rasadyar',
),
ObxValue((markers) {
return MarkerLayer(
markers: markers
.map(
(e) => markerWidget(
marker: LatLng(e.lat??0, e.long??0),
onTap: () {
Get.bottomSheet(
selectedLocationWidget2(
showHint: false,
sliderController: controller.slidController.value,
trigger: () {},
toggle: () {},
),
isScrollControlled: true,
enableDrag: true,
backgroundColor: Colors.transparent,
);
},
MarkerClusterLayerWidget(
options: MarkerClusterLayerOptions(
maxClusterRadius: 80,
size: const Size(40, 40),
alignment: Alignment.center,
padding: const EdgeInsets.all(50),
maxZoom: 15,
markers: controller.markers,
builder: (context, markers) {
return Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(20),
color: Colors.blue,
),
child: Center(
child: Text(
markers.length.toString(),
style: const TextStyle(color: Colors.white),
),
)
.toList(),
);
}, controller.markers),
),
);
},
),
),
],
);
}, controller.currentLocation),
@@ -521,7 +524,6 @@ Widget cardWithLabel({
);
}
Widget selectedLocationWidget({
required bool showHint,
required SlidableController sliderController,