Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Ability to pass JSON style as a string (rather than a URL) #6335

Closed
tsemerad opened this issue Sep 14, 2016 · 4 comments
Closed

Ability to pass JSON style as a string (rather than a URL) #6335

tsemerad opened this issue Sep 14, 2016 · 4 comments

Comments

@tsemerad
Copy link

Platform: iOS and Android

I'm storing style JSON in a document database. Using the database's API I can retrieve the style JSON, but there doesn't appear to be a way to use that style JSON in the SDK if it isn't a URL. Have you considered supporting passing the JSON as a string on the iOS and Android SDKs (or possibly a dictionary)?

Another use case I need this for is for pre-fetching style JSON while I have a connection. When my app launches, and has internet connection, I want it to grab 3 different styles (modified versions of streets, dark, and satellite streets), and store them for later use if the user switches to one of them while offline.

@1ec5
Copy link
Contributor

1ec5 commented Sep 15, 2016

I can't speak for the Android SDK, but with the latest iOS SDK 3.4.0 alpha, you can save that JSON file to a file (say, in the Caches folder) and refer to it by an absolute file URL.

Another option is to bundle those three styles (and potentially their resources) in the application's resource bundle and refer to them by relative URLs. This works in released versions of the iOS SDK, as well as on Android by putting the styles in the APK. (If you do bundle resources like the sprite sheet alongside the style like this, be sure to refer to them in the style JSON with asset: URLs (like asset://sprites.json.)

The core library does support setting a JSON string as the style, and indeed the iOS SDK did support that in the early days, but we removed it for maintainability reasons (too many different code paths for populating the map with content).

I think we'd like to avoid restoring that API if at all possible, to avoid extra serialization or deserialization, but also because the SDK strives to be as object-oriented as possible. If neither of the two approaches above – absolute file URL or relative URL – works for your use cases, we'd love to find out why.

@tsemerad
Copy link
Author

Saving the JSON to a file should work perfectly. The documentation made it clear to me that asset:// could only be used on resources bundled with the app - it's great to hear that that limitation is changing! Looking around at other issues, it appears I can use the asset:// syntax to reference local files in iOS, but it's still not yet available on Android according to #6273. I'll keep an eye on that. Thank you.

@1ec5
Copy link
Contributor

1ec5 commented Sep 15, 2016

OK, good to hear. Let’s track the Android side in #6273 then. (By the way, on iOS/macOS, you can simply use the +[NSURL fileURLWithPath:] initializer. The SDK converts it into an asset: URL under the hood, but you shouldn’t need to worry about that.)

@1ec5 1ec5 closed this as completed Sep 15, 2016
@1ec5
Copy link
Contributor

1ec5 commented Sep 15, 2016

Oh, and I forgot the best approach that works in the released versions of both SDKs. 😆 You may be able to use the offline map API to download the styles and all their resources – crucially, including the vector tile data – for offline usage.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants