feat : cashing map from internet

This commit is contained in:
2025-08-20 11:05:31 +03:30
parent b2f26cdffd
commit 80e3b01998
9 changed files with 126 additions and 6 deletions

View File

@@ -1,9 +1,10 @@
import 'dart:async';
import 'dart:math';
import 'package:flutter/material.dart';
import 'package:rasadyar_core/core.dart';
import 'package:rasadyar_livestock/data/common/constant.dart';
import 'package:rasadyar_livestock/data/repository/livestock/livestock_repository.dart';
import 'package:rasadyar_livestock/data/repository/livestock/livestock_repository_imp.dart';
import 'package:rasadyar_livestock/injection/live_stock_di.dart';
import 'package:rasadyar_livestock/presentation/page/root/logic.dart';
@@ -21,9 +22,11 @@ class MapWidgetLogic extends GetxController with GetTickerProviderStateMixin {
Timer? _debounceTimer;
RxBool isLoading = false.obs;
late FMTCTileProvider tileProvider;
RxList<LatLng> markerLocations = RxList();
RootLogic rootLogic = Get.find<RootLogic>();
LivestockRepository repository = diLiveStock.get<LivestockRepository>();
LivestockRepository repository = diLiveStock.get<LivestockRepository>();
@override
void onInit() {
@@ -53,6 +56,10 @@ class MapWidgetLogic extends GetxController with GetTickerProviderStateMixin {
errorLocationType.remove(ErrorLocationType.serviceDisabled);
}
});
tileProvider = FMTCTileProvider(stores: {mapStoreKey: BrowseStoreStrategy.readUpdateCreate});
repository.addLocations(generateRandomLocations(currentLocation.value, 10, 100));
}
@override
@@ -60,6 +67,7 @@ class MapWidgetLogic extends GetxController with GetTickerProviderStateMixin {
super.onReady();
determineCurrentPosition();
getLoc();
}
@override
@@ -156,9 +164,38 @@ class MapWidgetLogic extends GetxController with GetTickerProviderStateMixin {
await safeCall(
call: () async => repository.getLocations(),
onSuccess: (result) {
iLog("OOOpssss => ${result.length}");
markerLocations.addAll(result);
},
onError: (error, stackTrace) {},
);
}
List<LatLng> generateRandomLocations(LatLng currentPosition, double radiusInKm, int count) {
final random = Random();
final locations = <LatLng>[];
for (int i = 0; i < count; i++) {
// فاصله تصادفی (۰ تا radius)
final distance = random.nextDouble() * radiusInKm * 1000; // متر
// زاویه تصادفی (۰ تا ۲π)
final angle = random.nextDouble() * 2 * pi;
// فاصله به درجه
final dx = distance * cos(angle);
final dy = distance * sin(angle);
// 1 درجه lat ≈ 111km
final newLat = currentPosition.latitude + (dy / 111000.0);
// 1 درجه lon ≈ 111km * cos(lat)
final newLng = currentPosition.longitude +
(dx / (111000.0 * cos(currentPosition.latitude * pi / 180)));
locations.add(LatLng(newLat, newLng));
}
return locations;
}
}

View File

@@ -104,6 +104,7 @@ class MapWidget extends GetView<MapWidgetLogic> {
flags: InteractiveFlag.all & ~InteractiveFlag.rotate,
),
initialZoom: 15,
onPositionChanged: (camera, hasGesture) {
controller.currentZoom.value = camera.zoom;
/* controller.debouncedUpdateVisibleMarkers(
@@ -117,6 +118,7 @@ class MapWidget extends GetView<MapWidgetLogic> {
TileLayer(
urlTemplate: 'https://tile.openstreetmap.org/{z}/{x}/{y}.png',
userAgentPackageName: 'ir.mnpc.rasadyar',
tileProvider: controller.tileProvider,
),
ObxValue((markers) {
@@ -153,8 +155,6 @@ class MapWidget extends GetView<MapWidgetLogic> {
top: 15,
left: 20,
child: ObxValue((status) {
return Text("Connection: ${status.value}", style: TextStyle(fontSize: 20));
}, NetworkStatus().isConnected),
),