-
Notifications
You must be signed in to change notification settings - Fork 509
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
Android's OfflineManager and iOS's MGLOffline #88
Comments
Offline side loading only atm: https://medium.com/@jamesayvaz/offline-maps-for-your-flutter-app-3ea64111b73c |
I'm gonna take a stab at this cause I'd really like it for a project I'm working on |
@max-outway I did quick and dirty implementation earlier this year. here is my code. Might help. /// Starts an offline map download.
///
/// The returned [Future] completes once listeners have been notified.
Future<void> downloadOfflineMap(MapDownloadOptions options) async {
assert(options != null);
await _channel.invokeMethod('map#startOfflineDownload', <String, dynamic>{
'mapName': options.mapName,
'minZoom': options.minZoom,
'maxZoom': options.maxZoom,
'targetBounds': options.targetBounds._toList()
});
notifyListeners();
}
case "map#startOfflineDownload":
guard let arguments = methodCall.arguments as? [String: Any] else { return }
guard let mapName = arguments["mapName"] as? String else { return }
guard let minZoom = arguments["minZoom"] as? Double else { return }
guard let maxZoom = arguments["maxZoom"] as? Double else { return }
guard let targetBounds = arguments["targetBounds"] as? [[Double]] else { return }
startOfflinePackDownload(mapName: mapName, minZoom: minZoom, maxZoom: maxZoom, targetBounds: MGLCoordinateBounds.fromArray(targetBounds)) // MARK: - MGLOfflinePack Start
func startOfflinePackDownload(mapName: String, minZoom: Double, maxZoom: Double, targetBounds: MGLCoordinateBounds) {
// Create a region that includes the current viewport and any tiles needed to view it when zoomed further in.
// Because tile count grows exponentially with the maximum zoom level, you should be conservative with your `toZoomLevel` setting.
let region = MGLTilePyramidOfflineRegion(styleURL: mapView.styleURL,
bounds: targetBounds,
fromZoomLevel: minZoom,
toZoomLevel: maxZoom)
// Store some data for identification purposes alongside the downloaded resources.
let userInfo = ["name": mapName]
let context = NSKeyedArchiver.archivedData(withRootObject: userInfo)
// Create and register an offline pack with the shared offline storage object.
MGLOfflineStorage.shared.addPack(for: region, withContext: context) { (pack, error) in
guard error == nil else {
// The pack couldn’t be created for some reason.
print("Map Download Error: \(error?.localizedDescription ?? "unknown error")")
return
}
// Start downloading.
pack!.resume()
}
}
// MARK: - MGLOfflinePack notification handlers
@objc func offlinePackProgressDidChange(notification: NSNotification) {
// Get the offline pack this notification is regarding,
// and the associated user info for the pack; in this case, `name = My Offline Pack`
if let pack = notification.object as? MGLOfflinePack,
let userInfo = NSKeyedUnarchiver.unarchiveObject(with: pack.context) as? [String: String] {
let progress = pack.progress
// or notification.userInfo![MGLOfflinePackProgressUserInfoKey]!.MGLOfflinePackProgressValue
let completedResources = progress.countOfResourcesCompleted
let expectedResources = progress.countOfResourcesExpected
// Calculate current progress percentage.
let progressPercentage = Float(completedResources) / Float(expectedResources)
mapDownloadProgressDidChange?(progressPercentage)
// todo return this percentage on a listener
// If this pack has finished, print its size and resource count.
if completedResources == expectedResources {
// let byteCount = ByteCountFormatter.string(fromByteCount: Int64(pack.progress.countOfBytesCompleted), countStyle: ByteCountFormatter.CountStyle.memory)
//print("Offline pack “\(userInfo["name"] ?? "unknown")” completed: \(byteCount), \(completedResources) resources")
} else {
// Otherwise, print download/verification progress.
//print("Offline pack “\(userInfo["name"] ?? "unknown")” has \(completedResources) of \(expectedResources) resources — \(progressPercentage * 100)%.")
}
}
}
@objc func offlinePackDidReceiveError(notification: NSNotification) {
if let pack = notification.object as? MGLOfflinePack,
let userInfo = NSKeyedUnarchiver.unarchiveObject(with: pack.context) as? [String: String],
let error = notification.userInfo?[MGLOfflinePackUserInfoKey.error] as? NSError {
print("Offline pack “\(userInfo["name"] ?? "unknown")” received error: \(error.localizedFailureReason ?? "unknown error")")
}
}
@objc func offlinePackDidReceiveMaximumAllowedMapboxTiles(notification: NSNotification) {
if let pack = notification.object as? MGLOfflinePack,
let userInfo = NSKeyedUnarchiver.unarchiveObject(with: pack.context) as? [String: String],
let maximumCount = (notification.userInfo?[MGLOfflinePackUserInfoKey.maximumCount] as AnyObject).uint64Value {
print("Offline pack “\(userInfo["name"] ?? "unknown")” reached limit of \(maximumCount) tiles.")
}
} |
@max-outway sounds great! @vincekruger thanks for sharing code snippets! |
Sweet, this obviously still needs a bit of work but does it look on the right track? git diff Main questions:
Thanks for any insights you guys have! |
@maximumbuster great work so far! I really need offline support for an app I'm working on too, so would love to see this implemented. |
@maximumbuster I just came across your offline implementation and I think this would be a great feature.
|
@m0nac0 great suggestions! I'll make those changes and put up a PR |
Ah wanted to submit a PR until i stumbled on this. Let me know if I should still, I've already done an offline manager for my own project - offline downloading, listing ,deleting downloaded maps and changing tile download limits and tested on both iOS and Android |
@hafiz703 if you already have this completed feel free to pr! |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
Possible duplicate of #27 ,
I just wanted to know whether there were any updates on that
The text was updated successfully, but these errors were encountered: