@@ -11,54 +11,49 @@ import MapKit
1111import CoreLocation
1212
1313class MapViewModel : ObservableObject {
14- @Published var coordinateRegion : MKCoordinateRegion = MKCoordinateRegion ( )
14+
15+ let locationManagerDelegate = LocationManagerDelegate ( )
16+
17+ var region = MKCoordinateRegion ( center: CLLocationCoordinate2D ( latitude: 37.3318 , longitude: - 122.0312 ) , span: MKCoordinateSpan ( latitudeDelta: 0.0004 , longitudeDelta: 0.0004 ) )
18+ let viewSpanDelta = 0.0004 // Delta lat/lng to show to the user
19+
20+
1521 @Published var items : [ DisplayUnitWithCoordinate ] = [ ]
1622 @Published var selectedQuest : DisplayUnit ?
17- private let locationManager = LocationManagerCoordinator ( )
1823 @Published var isLoading : Bool = false
19- let viewSpanDelta = 0.0004 // Delta lat/lng to show to the user
20- let dataSpanDistance : CLLocationDistance = 100 // Distance from user location to get the data
21-
24+ let dataSpanDistance : CLLocationDistance = 1000 // Distance from user location to get the data
25+
2226 init ( ) {
23- // self.coordinateRegion = MKCoordinateRegion()
24- locationManager. locationUpdateHandler = { [ weak self] location in
27+ locationManagerDelegate. locationManager. delegate = locationManagerDelegate
28+ locationManagerDelegate. locationManager. requestWhenInUseAuthorization ( )
29+ locationManagerDelegate. locationManager. startUpdatingLocation ( )
30+
31+ locationManagerDelegate. locationUpdateHandler = { [ weak self] location in
2532 guard let self = self else { return }
26- DispatchQueue . main. async {
27- print ( " Centering map on location " )
28- self . centerMapOnLocation ( location)
29- }
33+ fetchOSMDataFor ( currentLocation: location)
3034 }
35+
36+ }
37+
38+ @objc private func locationDidChange( ) {
39+ guard let userLocation = locationManagerDelegate. location else { return }
40+ fetchOSMDataFor ( currentLocation: userLocation)
3141 }
3242
33- func fetchData ( ) {
43+ func fetchOSMDataFor ( currentLocation : CLLocation ) {
3444 isLoading = true
35- let boundingBox = boundingBoxAroundLocation ( location: locationManager. currentLocation ?? CLLocation ( latitude: coordinateRegion. center. latitude, longitude: coordinateRegion. center. longitude) , distance: dataSpanDistance)
36- AppQuestManager . shared. fetchData ( fromBBOx: boundingBox) { [ weak self] in
45+ let bBox = boundingBoxAroundLocation ( location: currentLocation, distance: dataSpanDistance)
46+ self . region = MKCoordinateRegion ( center: currentLocation. coordinate, span: MKCoordinateSpan (
47+ latitudeDelta: viewSpanDelta,
48+ longitudeDelta: viewSpanDelta
49+ ) )
50+ AppQuestManager . shared. fetchData ( fromBBOx: bBox) { [ weak self] in
3751 guard let self = self else { return }
3852 self . items = AppQuestManager . shared. fetchQuestsFromDB ( )
3953 self . isLoading = false
4054 }
4155 }
42-
43- func centerMapOnLocation( _ location: CLLocation ) {
44- // Update coordinate region only if necessary
45- let region = MKCoordinateRegion ( center: location. coordinate, span: MKCoordinateSpan (
46- latitudeDelta: viewSpanDelta,
47- longitudeDelta: viewSpanDelta
48- ) )
4956
50- if !coordinateRegionIsEqual( region, coordinateRegion) {
51- coordinateRegion = region
52- fetchData ( ) // Fetch data when the map region changes
53- }
54- }
55-
56- private func coordinateRegionIsEqual( _ region1: MKCoordinateRegion , _ region2: MKCoordinateRegion ) -> Bool {
57- return region1. center. latitude == region2. center. latitude &&
58- region1. center. longitude == region2. center. longitude &&
59- region1. span. latitudeDelta == region2. span. latitudeDelta &&
60- region1. span. longitudeDelta == region2. span. longitudeDelta
61- }
6257 private func boundingBoxAroundLocation( location: CLLocation , distance: CLLocationDistance ) -> BBox {
6358 let region = MKCoordinateRegion ( center: location. coordinate, latitudinalMeters: distance, longitudinalMeters: distance)
6459 let center = region. center
@@ -68,6 +63,7 @@ class MapViewModel: ObservableObject {
6863 let minLon = center. longitude - span. longitudeDelta / 2
6964 let maxLon = center. longitude + span. longitudeDelta / 2
7065
66+
7167 return BBox ( minLat: minLat, maxLat: maxLat, minLon: minLon, maxLon: maxLon)
7268 }
7369}
0 commit comments