Skip to content

Commit

Permalink
Update mapbox manager
Browse files Browse the repository at this point in the history
  • Loading branch information
vishnuravi committed Apr 3, 2024
1 parent 0166dfe commit 6e80de1
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 65 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build-and-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:
contents: read
buildandtest:
name: Build
runs-on: [macos-latest, self-hosted]
runs-on: [macos, self-hosted]
steps:
- uses: actions/checkout@v2
- uses: maxim-lobanov/setup-xcode@v1
Expand Down
4 changes: 0 additions & 4 deletions StrokeCog.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@
6347EB742BBBF442008E0C4A /* Constants.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6347EB732BBBF442008E0C4A /* Constants.swift */; };
63BBF8162BB8993B006890CE /* StudyIDView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 63BBF8152BB8993B006890CE /* StudyIDView.swift */; };
63BBF8192BB89CF7006890CE /* studyIDs.csv in Resources */ = {isa = PBXBuildFile; fileRef = 63BBF8182BB89CF7006890CE /* studyIDs.csv */; };
63F4C3972BBCCC070033D985 /* MapboxMap.swift in Sources */ = {isa = PBXBuildFile; fileRef = 63F4C3962BBCCC070033D985 /* MapboxMap.swift */; };
63F4C3992BBCCC300033D985 /* MapboxView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 63F4C3982BBCCC300033D985 /* MapboxView.swift */; };
63F4C39B2BBCCCF80033D985 /* LocationService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 63F4C39A2BBCCCF80033D985 /* LocationService.swift */; };
63F4C39D2BBCCD200033D985 /* LocationUtils.swift in Sources */ = {isa = PBXBuildFile; fileRef = 63F4C39C2BBCCD200033D985 /* LocationUtils.swift */; };
Expand Down Expand Up @@ -153,7 +152,6 @@
6347EB732BBBF442008E0C4A /* Constants.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Constants.swift; sourceTree = "<group>"; };
63BBF8152BB8993B006890CE /* StudyIDView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StudyIDView.swift; sourceTree = "<group>"; };
63BBF8182BB89CF7006890CE /* studyIDs.csv */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = studyIDs.csv; sourceTree = "<group>"; };
63F4C3962BBCCC070033D985 /* MapboxMap.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MapboxMap.swift; sourceTree = "<group>"; };
63F4C3982BBCCC300033D985 /* MapboxView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MapboxView.swift; sourceTree = "<group>"; };
63F4C39A2BBCCCF80033D985 /* LocationService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LocationService.swift; sourceTree = "<group>"; };
63F4C39C2BBCCD200033D985 /* LocationUtils.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LocationUtils.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -329,7 +327,6 @@
isa = PBXGroup;
children = (
6347EB632BBBA895008E0C4A /* StrokeCogMapView.swift */,
63F4C3962BBCCC070033D985 /* MapboxMap.swift */,
63F4C3982BBCCC300033D985 /* MapboxView.swift */,
63F4C39A2BBCCCF80033D985 /* LocationService.swift */,
63F4C39C2BBCCD200033D985 /* LocationUtils.swift */,
Expand Down Expand Up @@ -646,7 +643,6 @@
2FE5DC4629EDD7F2004B9AB4 /* Bundle+Image.swift in Sources */,
2FE5DC4F29EDD7FA004B9AB4 /* EventContext.swift in Sources */,
6347EB642BBBA895008E0C4A /* StrokeCogMapView.swift in Sources */,
63F4C3972BBCCC070033D985 /* MapboxMap.swift in Sources */,
63F4C3992BBCCC300033D985 /* MapboxView.swift in Sources */,
2FE5DC5029EDD7FA004B9AB4 /* EventContextView.swift in Sources */,
63F4C3A32BBCE79B0033D985 /* LocationPermissions.swift in Sources */,
Expand Down
57 changes: 0 additions & 57 deletions StrokeCog/Map/MapboxMap.swift

This file was deleted.

63 changes: 61 additions & 2 deletions StrokeCog/Map/MapboxView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,75 @@ struct MapManagerViewWrapper: UIViewControllerRepresentable {
}

public class MapManagerView: UIViewController {
internal lazy var mapView: MapView = {
private enum Constants {
static let geoSourceId = "GEOSOURCE"
static let circleLayerId = "CIRCLELAYER"
static let zoomLevel: Double = 14.0
static let countryLabelLayerId = "country-label"
}

private lazy var mapView: MapView = {
let map = MapView(frame: view.bounds)
map.autoresizingMask = [.flexibleWidth, .flexibleHeight]
MapboxMap.initializeMap(mapView: map, reload: true)
map.location.options.puckType = .puck2D()
map.ornaments.options.scaleBar.visibility = .visible
return map
}()

override public func viewDidLoad() {
super.viewDidLoad()
initializeMap()
self.view.addSubview(mapView)
}

/// Initialize map with locations and optional reloading
private func initializeMap() {
self.mapView.mapboxMap.onNext(event: .mapLoaded) { _ in
let locations = LocationService.shared.allLocations
do {
try self.addGeoJSONSource(with: locations)
try self.addCircleLayer(sourceId: Constants.geoSourceId)
self.centerCamera(at: locations.last, zoomLevel: Constants.zoomLevel)
self.setupDynamicLocationUpdates()
} catch {
print("[MapboxMap] Error: \(error.localizedDescription)")
}
}
}

/// Add GeoJSON source to the map
private func addGeoJSONSource(with locations: [CLLocationCoordinate2D]) throws {
var source = GeoJSONSource(id: Constants.geoSourceId)
source.data = .feature(Feature(geometry: .multiPoint(MultiPoint(locations))))
try mapView.mapboxMap.addSource(source)
}

/// Add circle layer to the map
private func addCircleLayer(sourceId: String) throws {
var circlesLayer = CircleLayer(id: Constants.circleLayerId, source: sourceId)
circlesLayer.circleColor = .constant(StyleColor(.red))
circlesLayer.circleStrokeColor = .constant(StyleColor(.black))
circlesLayer.circleStrokeWidth = .constant(2)
try mapView.mapboxMap.addLayer(circlesLayer)
}

/// Center the map's camera
private func centerCamera(at location: CLLocationCoordinate2D?, zoomLevel: Double) {
guard let center = location else {
return
}

mapView.mapboxMap.setCamera(to: CameraOptions(center: center, zoom: zoomLevel))
}

/// Set up dynamic updates for locations
private func setupDynamicLocationUpdates() {
LocationService.shared.onLocationsUpdated = { locations in
self.mapView.mapboxMap.updateGeoJSONSource(
withId: Constants.geoSourceId,
geoJSON: .feature(Feature(geometry: .lineString(LineString(locations))))
)
self.centerCamera(at: locations.last, zoomLevel: Constants.zoomLevel)
}
}
}
2 changes: 1 addition & 1 deletion StrokeCog/Onboarding/LocationPermissions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ struct LocationPermissions: View {
action: {
do {
locationProcessing = true
// Notification Authorization is not available in the preview simulator.
// Location authorization is not available in the preview simulator.
if ProcessInfo.processInfo.isPreviewSimulator {
try await _Concurrency.Task.sleep(for: .seconds(5))
} else {
Expand Down

0 comments on commit 6e80de1

Please sign in to comment.