-
Notifications
You must be signed in to change notification settings - Fork 0
add ios offline maps tutorial code #17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
chriswhong
wants to merge
1
commit into
main
Choose a base branch
from
cw/ios-offline-maps
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
598 changes: 598 additions & 0 deletions
598
ios-offline-maps/ios-offline-maps.xcodeproj/project.pbxproj
Large diffs are not rendered by default.
Oops, something went wrong.
7 changes: 7 additions & 0 deletions
7
ios-offline-maps/ios-offline-maps.xcodeproj/project.xcworkspace/contents.xcworkspacedata
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
42 changes: 42 additions & 0 deletions
42
...maps/ios-offline-maps.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
11 changes: 11 additions & 0 deletions
11
ios-offline-maps/ios-offline-maps/Assets.xcassets/AccentColor.colorset/Contents.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"colors" : [ | ||
{ | ||
"idiom" : "universal" | ||
} | ||
], | ||
"info" : { | ||
"author" : "xcode", | ||
"version" : 1 | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
ios-offline-maps/ios-offline-maps/Assets.xcassets/AppIcon.appiconset/Contents.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
{ | ||
"images" : [ | ||
{ | ||
"idiom" : "universal", | ||
"platform" : "ios", | ||
"size" : "1024x1024" | ||
}, | ||
{ | ||
"appearances" : [ | ||
{ | ||
"appearance" : "luminosity", | ||
"value" : "dark" | ||
} | ||
], | ||
"idiom" : "universal", | ||
"platform" : "ios", | ||
"size" : "1024x1024" | ||
}, | ||
{ | ||
"appearances" : [ | ||
{ | ||
"appearance" : "luminosity", | ||
"value" : "tinted" | ||
} | ||
], | ||
"idiom" : "universal", | ||
"platform" : "ios", | ||
"size" : "1024x1024" | ||
} | ||
], | ||
"info" : { | ||
"author" : "xcode", | ||
"version" : 1 | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
ios-offline-maps/ios-offline-maps/Assets.xcassets/Contents.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"info" : { | ||
"author" : "xcode", | ||
"version" : 1 | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
ios-offline-maps/ios-offline-maps/Assets.xcassets/map_marker.imageset/Contents.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{ | ||
"images" : [ | ||
{ | ||
"filename" : "map_marker.png", | ||
"idiom" : "universal", | ||
"scale" : "1x" | ||
}, | ||
{ | ||
"idiom" : "universal", | ||
"scale" : "2x" | ||
}, | ||
{ | ||
"idiom" : "universal", | ||
"scale" : "3x" | ||
} | ||
], | ||
"info" : { | ||
"author" : "xcode", | ||
"version" : 1 | ||
} | ||
} |
Binary file added
BIN
+3.09 KB
...ffline-maps/ios-offline-maps/Assets.xcassets/map_marker.imageset/map_marker.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// Import necessary frameworks for the app | ||
import SwiftUI // For the user interface | ||
import MapboxMaps // For map rendering and interaction | ||
import CoreLocation | ||
|
||
struct ContentView: View { | ||
@State private var showingDownloadView = false | ||
|
||
var body: some View { | ||
ZStack { | ||
// Map takes full screen as the base layer | ||
MapReader { proxy in | ||
Map(initialViewport: .camera( | ||
center: CLLocationCoordinate2D( | ||
latitude: 39.5, | ||
longitude: -98.0 | ||
), | ||
zoom: 2)) { | ||
chriswhong marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
} | ||
.mapStyle(.standard()) // Use the Mapbox Standard style | ||
.onMapLoaded { event in | ||
// Map is loaded and ready | ||
print("Map loaded successfully") | ||
} | ||
} | ||
chriswhong marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// Download button overlay | ||
VStack { | ||
HStack { | ||
Spacer() | ||
chriswhong marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Button(action: { | ||
showingDownloadView = true | ||
}) { | ||
HStack { | ||
Image(systemName: "arrow.down.circle.fill") | ||
Text("Manage Offline Regions") | ||
} | ||
chriswhong marked this conversation as resolved.
Show resolved
Hide resolved
|
||
.padding() | ||
.background(Color.blue) | ||
.foregroundColor(.white) | ||
.cornerRadius(10) | ||
.shadow(radius: 3) | ||
} | ||
.padding() | ||
} | ||
Spacer() | ||
} | ||
} | ||
.sheet(isPresented: $showingDownloadView) { | ||
TileRegionDownloadView() | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> | ||
<plist version="1.0"> | ||
<dict> | ||
<key>MBXAccessToken</key> | ||
<string>YOUR_MAPBOX_ACCESS_TOKEN</string> | ||
</dict> | ||
</plist> |
139 changes: 139 additions & 0 deletions
139
ios-offline-maps/ios-offline-maps/OfflineRegionManager.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
// | ||
// OfflineRegionManager.swift | ||
// simple-map-swiftui | ||
// | ||
// Created on 8/12/25. | ||
// | ||
|
||
import Foundation | ||
import MapboxMaps | ||
|
||
class OfflineRegionManager { | ||
|
||
private static var hasDownloadedStylePack = false | ||
|
||
static func ensureStylePackDownloaded() { | ||
chriswhong marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// Only download once per app session | ||
if hasDownloadedStylePack { | ||
return | ||
} | ||
|
||
let offlineManager = OfflineManager() | ||
|
||
let stylePackLoadOptions = StylePackLoadOptions( | ||
glyphsRasterizationMode: .ideographsRasterizedLocally, | ||
metadata: ["name": "mapbox-standard-stylepack"], | ||
acceptExpired: false | ||
) | ||
|
||
offlineManager.loadStylePack( | ||
for: .standard, | ||
loadOptions: stylePackLoadOptions! | ||
) { _ in } completion: { result in | ||
switch result { | ||
case let .success(stylePack): | ||
// Style pack download finishes successfully | ||
print("Downloaded style pack: \(stylePack)") | ||
hasDownloadedStylePack = true | ||
|
||
case let .failure(error): | ||
// Handle error occurred during the style pack download | ||
if case StylePackError.canceled = error { | ||
print("Style pack download cancelled") | ||
} else { | ||
print("Style pack download failed: \(error)") | ||
} | ||
} | ||
} | ||
} | ||
|
||
static func downloadRegion( | ||
region: OfflineRegion, | ||
downloadingRegions: Set<String>, | ||
onDownloadingRegionsUpdate: @escaping (Set<String>) -> Void, | ||
onProgress: @escaping (String, Float) -> Void, | ||
onCompletion: @escaping (String, Result<TileRegion, Error>) -> Void | ||
) { | ||
// Don't start if already downloading | ||
if downloadingRegions.contains(region.id) { | ||
return | ||
} | ||
|
||
let tileStore = TileStore.default | ||
let offlineManager = OfflineManager() | ||
|
||
// Create tileset descriptor | ||
let tilesetDescriptor = offlineManager.createTilesetDescriptor( | ||
for: TilesetDescriptorOptions( | ||
styleURI: .standard, // get tiles for the Mapbox Standard style | ||
zoomRange: 10...14, | ||
tilesets: [] | ||
) | ||
) | ||
|
||
// Create load options using the region's polygon and metadata | ||
let loadOptions = TileRegionLoadOptions( | ||
geometry: .polygon(region.polygon), | ||
descriptors: [tilesetDescriptor], | ||
metadata: ["name": region.name], | ||
acceptExpired: true | ||
)! | ||
|
||
// Start downloading - notify UI to add to downloading set | ||
var updatedDownloadingRegions = downloadingRegions | ||
updatedDownloadingRegions.insert(region.id) | ||
onDownloadingRegionsUpdate(updatedDownloadingRegions) | ||
onProgress(region.id, 0.0) | ||
|
||
let _ = tileStore.loadTileRegion( | ||
forId: region.id, | ||
loadOptions: loadOptions | ||
) { progress in | ||
let totalResources = max(progress.requiredResourceCount, 1) | ||
let progressValue = Float(progress.completedResourceCount) / Float(totalResources) | ||
onProgress(region.id, progressValue) | ||
} completion: { result in | ||
// Remove from downloading set and notify completion | ||
updatedDownloadingRegions.remove(region.id) | ||
onDownloadingRegionsUpdate(updatedDownloadingRegions) | ||
onCompletion(region.id, result) | ||
} | ||
} | ||
|
||
static func clearAllRegions(onCompletion: @escaping () -> Void) { | ||
let tileStore = TileStore.default | ||
|
||
// Get all tile regions from the store | ||
tileStore.allTileRegions { result in | ||
switch result { | ||
case .success(let tileRegions): | ||
// Remove each region found in the store | ||
for tileRegion in tileRegions { | ||
tileStore.removeRegion(forId: tileRegion.id) { removeResult in | ||
switch removeResult { | ||
case .success: | ||
print("Removed region: \(tileRegion.id)") | ||
case .failure(let error): | ||
print("Failed to remove region \(tileRegion.id): \(error)") | ||
} | ||
} | ||
} | ||
|
||
// Clear ambient cache after removing regions | ||
tileStore.clearAmbientCache { cacheResult in | ||
switch cacheResult { | ||
case .success(let bytes): | ||
print("Cleared \(bytes) bytes from cache") | ||
case .failure(let error): | ||
print("Failed to clear cache: \(error)") | ||
} | ||
onCompletion() | ||
} | ||
|
||
case .failure(let error): | ||
print("Failed to get tile regions: \(error)") | ||
onCompletion() | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.