Skip to content
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

Support styleString as "Documents directory/Temporary directory" absolute path #520

Merged

Conversation

bahung1221
Copy link
Contributor

@bahung1221 bahung1221 commented Feb 7, 2021

Hi,

This PR will enable support for using app documents directory or temporary directory as styleString parameter for both Android and iOS.

Use case:

Currently, for dynamic styles, flutter-mapbox-gl only support styleString as raw JSON string and this feature only work on Android, so we doesn't have any way to load dynamic styles on iOS device.

This PR will allow us to save raw JSON that we got from anywhere (API call, generated by code,...) to app storage at runtime and then able to pass their path as styleString parameter.

Sample code:

  ...
  @override
  initState() {
    super.initState();

    getApplicationDocumentsDirectory().then((dir) async {
      String documentDir = dir.path;
      String stylesDir = '$documentDir/styles';
      String styleJSON = '{...}'; // JSON string

      await new Directory(stylesDir).create(recursive: true);

      File styleFile = new File('$stylesDir/style.json');

      await styleFile.writeAsString(styleJSON);

      setState(() {
        styleAbsoluteFilePath = styleFile.path;
      });
    });
  }
  ...
  @override
  Widget build(BuildContext context) {
    if (styleAbsoluteFilePath == null) {
      return Scaffold(
        body: Center(child: Text('Loading local style file...')),
      );
    }

    return new Scaffold(
      body: MapboxMap(
       ...,
        styleString: styleAbsoluteFilePath,
       ...
      )
    );
  }
  ...

Tested on Android 9 simulator, Android 10 real device, and iOS 14.3 simulator.

Thanks for such a great library and sorry for my bad English 😄

@bahung1221
Copy link
Contributor Author

Hi @tobrun , sorry for troubling you but I need this feature, my deadline is coming 🤣
May you help to review this PR when you have free time?

Cheers 🍺

Copy link
Collaborator

@tobrun tobrun left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool workaround!

@tobrun tobrun merged commit d083d12 into flutter-mapbox-gl:master Feb 12, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants