Skip to content

Commit 8bc97e5

Browse files
authored
Merge pull request #105 from TaskarCenterAtUW/feature-improve-map
Improve User Experience by showing user location while loading quests
2 parents 26546c1 + a97a418 commit 8bc97e5

File tree

7 files changed

+40
-144
lines changed

7 files changed

+40
-144
lines changed

GoInfoGame/GoInfoGame.xcodeproj/project.pbxproj

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,6 @@
194194
FABF3CFD2B8607860080EAC9 /* MeasureWidthContainer.swift in Sources */ = {isa = PBXBuildFile; fileRef = FABF3CFC2B8607860080EAC9 /* MeasureWidthContainer.swift */; };
195195
FABF3D002B8607D80080EAC9 /* MeasureSidewalkView.swift in Sources */ = {isa = PBXBuildFile; fileRef = FABF3CFF2B8607D80080EAC9 /* MeasureSidewalkView.swift */; };
196196
FAC9E60F2B04F9C800E2C608 /* OverpassRequestManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = FAC9E60E2B04F9C800E2C608 /* OverpassRequestManager.swift */; };
197-
FAC9E6112B06811300E2C608 /* LocationManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = FAC9E6102B06811300E2C608 /* LocationManager.swift */; };
198197
FAD5C4F32AFCBE700040C61A /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = FAD5C4F22AFCBE700040C61A /* AppDelegate.swift */; };
199198
FAD5C4F52AFCBE700040C61A /* SceneDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = FAD5C4F42AFCBE700040C61A /* SceneDelegate.swift */; };
200199
FAD5C4FA2AFCBE700040C61A /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = FAD5C4F82AFCBE700040C61A /* Main.storyboard */; };
@@ -475,7 +474,6 @@
475474
FABF3CFC2B8607860080EAC9 /* MeasureWidthContainer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MeasureWidthContainer.swift; sourceTree = "<group>"; };
476475
FABF3CFF2B8607D80080EAC9 /* MeasureSidewalkView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MeasureSidewalkView.swift; sourceTree = "<group>"; };
477476
FAC9E60E2B04F9C800E2C608 /* OverpassRequestManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OverpassRequestManager.swift; sourceTree = "<group>"; };
478-
FAC9E6102B06811300E2C608 /* LocationManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LocationManager.swift; sourceTree = "<group>"; };
479477
FAD5C4EF2AFCBE700040C61A /* GoInfoGame.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = GoInfoGame.app; sourceTree = BUILT_PRODUCTS_DIR; };
480478
FAD5C4F22AFCBE700040C61A /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
481479
FAD5C4F42AFCBE700040C61A /* SceneDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SceneDelegate.swift; sourceTree = "<group>"; };
@@ -1236,7 +1234,6 @@
12361234
FAD5C4F22AFCBE700040C61A /* AppDelegate.swift */,
12371235
FAD5C4F42AFCBE700040C61A /* SceneDelegate.swift */,
12381236
FAC9E60E2B04F9C800E2C608 /* OverpassRequestManager.swift */,
1239-
FAC9E6102B06811300E2C608 /* LocationManager.swift */,
12401237
FABF3CF32B7419BB0080EAC9 /* LocationManagerDelegate.swift */,
12411238
FA1992EC2BB1A78C003B4719 /* KeychainManager.swift */,
12421239
05CB71DD2B0FAEC200DED821 /* UI */,
@@ -1770,7 +1767,6 @@
17701767
973FC0192B4FB17000878269 /* LocalizedStrings.swift in Sources */,
17711768
A48037312BBA8C25007EE7E4 /* WorkspacesApiManager.swift in Sources */,
17721769
FABF3D002B8607D80080EAC9 /* MeasureSidewalkView.swift in Sources */,
1773-
FAC9E6112B06811300E2C608 /* LocationManager.swift in Sources */,
17741770
97439F782B87444200DA43E1 /* CrossingKerbHeightForm.swift in Sources */,
17751771
A4834A3F2B67737500D4F0AA /* StoredChangeset.swift in Sources */,
17761772
973FC02F2B58F81F00878269 /* StepsRamp.swift in Sources */,

GoInfoGame/GoInfoGame/LocationManager.swift

Lines changed: 0 additions & 103 deletions
This file was deleted.

GoInfoGame/GoInfoGame/LocationManagerDelegate.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,15 @@ class LocationManagerDelegate: NSObject, ObservableObject, CLLocationManagerDele
1212

1313
var locationManager = CLLocationManager()
1414
@Published var location: CLLocation?
15-
var locationUpdateHandler: ((CLLocation) -> Void)?
15+
var locationUpdateHandler: ((CLLocationCoordinate2D) -> Void)?
1616

1717
var hasUpdatedLocation = false
1818

1919
override init() {
2020
super.init()
21+
locationManager.delegate = self
22+
23+
locationManager.startUpdatingLocation()
2124
}
2225

2326
func requestLocationAuthorization() {
@@ -60,7 +63,7 @@ class LocationManagerDelegate: NSObject, ObservableObject, CLLocationManagerDele
6063
guard let mostRecentLocation = locations.last else { return }
6164
guard !hasUpdatedLocation else { return }
6265
location = mostRecentLocation
63-
locationUpdateHandler?(mostRecentLocation)
66+
locationUpdateHandler?(mostRecentLocation.coordinate)
6467
hasUpdatedLocation = true
6568
stopUpdatingLocation()
6669
}

GoInfoGame/GoInfoGame/UI/InitialView/InitialViewModel.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ class InitialViewModel: ObservableObject {
2828

2929
}
3030
// Method for fetching workspaces based on location
31-
func fetchWorkspaceFor(currentLocation: CLLocation) {
31+
func fetchWorkspaceFor(currentLocation: CLLocationCoordinate2D) {
3232
isLoading = true
33-
let latString = "\(currentLocation.coordinate.latitude)"
34-
let longString = "\(currentLocation.coordinate.longitude)"
33+
let latString = "\(currentLocation.latitude)"
34+
let longString = "\(currentLocation.longitude)"
3535
WorkspacesApiManager.shared.fetchWorkspaces(lat: latString, lon: longString) { result in
3636
DispatchQueue.main.async {
3737
switch result {

GoInfoGame/GoInfoGame/UI/Map/CustomMap.swift

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88
import Foundation
99
import SwiftUI
1010
import MapKit
11-
import CoreLocation
1211
import osmparser
1312

1413
// Custom Map for managing map interactions between SwiftUI and UIKit components
1514
struct CustomMap: UIViewRepresentable {
1615

17-
@Binding var region: MKCoordinateRegion
16+
var region: MKCoordinateRegion
17+
var userLocation = CLLocationCoordinate2D(latitude: 17.4700, longitude: 78.3534)
1818
@Binding var trackingMode: MapUserTrackingMode
1919
var items: [DisplayUnitWithCoordinate]
2020
@Binding var selectedQuest: DisplayUnit?
@@ -24,7 +24,6 @@ struct CustomMap: UIViewRepresentable {
2424

2525
@State var lineCoordinates: [CLLocationCoordinate2D] = []
2626

27-
2827
var contextualInfo: ((String) -> Void)?
2928

3029
// Creates and configures the UIView
@@ -42,25 +41,22 @@ struct CustomMap: UIViewRepresentable {
4241

4342
// Updates the UIView with new data
4443
func updateUIView(_ mapView: MKMapView, context: Context) {
45-
// Manage annotations
44+
mapView.setCenter(userLocation, animated: true)
45+
context.coordinator.updateUserRegion(mapView)
4646
manageAnnotations(mapView, context: context)
47-
// Update the region if necessary
48-
context.coordinator.updateRegion(mapView)
4947

5048
if shouldShowPolyline {
5149
if !lineCoordinates.isEmpty {
52-
let polyline = MKPolyline(coordinates: lineCoordinates, count: lineCoordinates.count)
53-
mapView.addOverlay(polyline)
54-
}
50+
let polyline = MKPolyline(coordinates: lineCoordinates, count: lineCoordinates.count)
51+
mapView.addOverlay(polyline)
52+
}
5553
} else {
5654
mapView.overlays.forEach { overlay in
5755
if overlay is MKPolyline {
5856
mapView.removeOverlay(overlay)
5957
}
6058
}
6159
}
62-
63-
6460
}
6561

6662
// Creates the coordinator
@@ -79,6 +75,17 @@ struct CustomMap: UIViewRepresentable {
7975
self.contextualInfo = parent.contextualInfo
8076
}
8177

78+
// Helper method to update the region
79+
func updateUserRegion(_ mapView: MKMapView) {
80+
// Update the region only if it hasn't been set yet
81+
if self.parent.isPresented {
82+
return
83+
} else if !isRegionSet {
84+
mapView.setRegion(parent.region, animated: true)
85+
isRegionSet = true
86+
}
87+
}
88+
8289
//renders polyline
8390
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
8491
if let routePolyline = overlay as? MKPolyline {
@@ -145,8 +152,8 @@ struct CustomMap: UIViewRepresentable {
145152
if let annotation = annotation as? MKClusterAnnotation {
146153
let displayAnnotation = annotation.memberAnnotations.first as! DisplayUnitAnnotation
147154
selectedAnAnnotation(selectedQuest: displayAnnotation)
148-
} else {
149-
selectedAnAnnotation(selectedQuest: annotation as! DisplayUnitAnnotation)
155+
} else if let annotation = annotation as? DisplayUnitAnnotation {
156+
selectedAnAnnotation(selectedQuest: annotation)
150157
}
151158

152159
// Deselect the annotation to prevent re-adding on selection
@@ -211,16 +218,6 @@ struct CustomMap: UIViewRepresentable {
211218
UIGraphicsEndImageContext()
212219
annotationView.image = resizedImage
213220
}
214-
// Helper method to update the region
215-
func updateRegion(_ mapView: MKMapView) {
216-
// Update the region only if it hasn't been set yet
217-
if self.parent.isPresented {
218-
return
219-
} else if !isRegionSet {
220-
mapView.setRegion(parent.region, animated: true)
221-
}
222-
}
223-
224221
}
225222

226223
// Helper method to manage annotations
@@ -247,7 +244,8 @@ struct CustomMap: UIViewRepresentable {
247244
if !context.coordinator.isRegionSet {
248245
/// resetting region only when app is launched/re-launched
249246
if existingCoordinates.count == 0 {
250-
mapView.setRegion(region, animated: true)
247+
mapView.setCenter(userLocation, animated: true)
248+
// mapView.setRegion(region, animated: true)
251249
}
252250
context.coordinator.isRegionSet = true
253251
}

GoInfoGame/GoInfoGame/UI/Map/MapView.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ struct MapView: View {
2525

2626
var body: some View {
2727
ZStack{
28-
CustomMap(region: $viewModel.region,
28+
CustomMap(region: viewModel.region,
29+
userLocation: viewModel.userlocation,
2930
trackingMode: $trackingMode,
3031
items: viewModel.items,
3132
selectedQuest: $viewModel.selectedQuest,

GoInfoGame/GoInfoGame/UI/Map/MapViewModel.swift

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ class MapViewModel: ObservableObject {
1414

1515
let locationManagerDelegate = LocationManagerDelegate()
1616
@Published var isLoading: Bool = false
17-
var region = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: 37.3318, longitude: -122.0312), span: MKCoordinateSpan(latitudeDelta: 0.0004 , longitudeDelta: 0.0004 ))
17+
var region = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: 37.3318, longitude: -122.0312), span: MKCoordinateSpan(latitudeDelta: 0.0009 , longitudeDelta: 0.0009))
1818
let viewSpanDelta = 0.001 // Delta lat/lng to show to the user
19-
19+
var userlocation = CLLocationCoordinate2D(latitude: 17.4700, longitude: 78.3534)
2020
@Published var refreshMap = UUID()
2121
@Published var items: [DisplayUnitWithCoordinate] = []
2222
@Published var selectedQuest: DisplayUnit?
@@ -29,20 +29,21 @@ class MapViewModel: ObservableObject {
2929

3030
locationManagerDelegate.locationUpdateHandler = { [weak self] location in
3131
guard let self = self else { return }
32+
self.userlocation = location
3233
fetchOSMDataFor(currentLocation: location)
3334
}
3435

3536
}
3637

3738
@objc private func locationDidChange() {
3839
guard let userLocation = locationManagerDelegate.location else { return }
39-
fetchOSMDataFor(currentLocation: userLocation)
40+
fetchOSMDataFor(currentLocation: userLocation.coordinate)
4041
}
4142

42-
func fetchOSMDataFor(currentLocation: CLLocation) {
43+
func fetchOSMDataFor(currentLocation: CLLocationCoordinate2D) {
4344
isLoading = true
4445
let bBox = boundingBoxAroundLocation(location: currentLocation, distance: dataSpanDistance)
45-
self.region = MKCoordinateRegion(center: currentLocation.coordinate, span: MKCoordinateSpan(
46+
self.region = MKCoordinateRegion(center: currentLocation, span: MKCoordinateSpan(
4647
latitudeDelta: viewSpanDelta,
4748
longitudeDelta: viewSpanDelta
4849
))
@@ -59,8 +60,8 @@ class MapViewModel: ObservableObject {
5960
self.items = AppQuestManager.shared.fetchQuestsFromDB()
6061
}
6162

62-
private func boundingBoxAroundLocation(location: CLLocation, distance: CLLocationDistance) -> BBox {
63-
let region = MKCoordinateRegion(center: location.coordinate, latitudinalMeters: distance, longitudinalMeters: distance)
63+
private func boundingBoxAroundLocation(location: CLLocationCoordinate2D, distance: CLLocationDistance) -> BBox {
64+
let region = MKCoordinateRegion(center: location, latitudinalMeters: distance, longitudinalMeters: distance)
6465
let center = region.center
6566
let span = region.span
6667
let minLat = center.latitude - span.latitudeDelta / 2

0 commit comments

Comments
 (0)