flutter-mapbox-gl / maps

A Mapbox GL flutter package for creating custom maps
Other
1.04k stars 502 forks source link

Flutter Mapbox GL

Please note that this project is community driven and is not an official Mapbox product.

We welcome feedback and contributions.

Table of contents

Introduction

This Flutter plugin allows to show embedded interactive and customizable vector maps inside a Flutter widget. For the Android and iOS integration, we use mapbox-gl-native. For web, we rely on mapbox-gl-js. This project only supports a subset of the API exposed by these libraries.

screenshot.png

Setting up

This package is available on pub.dev.

Get it by running the following command:

flutter pub add mapbox_gl

Mobile

Secret Mapbox access token

A secret access token with the Downloads: Read scope is required for the underlying Mapbox SDKs to be downloaded. Information on setting it up is available in the Mapbox documentation: Android, iOS.

If the properly configured token is not present, the build process fails with one the following errors (for Android/iOS respectively):

* What went wrong:
A problem occurred evaluating project ':mapbox_gl'.
> SDK Registry token is null. See README.md for more information.
[!] Error installing Mapbox-iOS-SDK
curl: (22) The requested URL returned error: 401 Unauthorized

Web

Include the JavaScript and CSS files in the <head> of your index.html file:

<script src='https://api.mapbox.com/mapbox-gl-js/v2.8.2/mapbox-gl.js'></script>
<link href='https://api.mapbox.com/mapbox-gl-js/v2.8.2/mapbox-gl.css' rel='stylesheet' />

<style>
   .mapboxgl-map {
      position: relative;
      width: 100%;
      height: 100%;
    }
</style>

Note: Look for latest version in Mapbox GL JS documentation.

All platforms

Public Mapbox access token

A public access token must be provided to a MapboxMap widget for retrieving styles and resources. While you can hardcode it directly into source files, it's good practise to retrieve access tokens from some external source (e.g. a config file or an environment variable). The example app uses the following technique:

The access token is passed via the command line arguments when either building

flutter build <platform> --dart-define ACCESS_TOKEN=YOUR_TOKEN_HERE

or running the application

flutter run --dart-define ACCESS_TOKEN=YOUR_TOKEN_HERE

Then it's retrieved in Dart:

MapboxMap(
  ...
  accessToken: const String.fromEnvironment("ACCESS_TOKEN"),
  ...
)

Supported API

Feature Android iOS Web
Style :white_check_mark: :white_check_mark: :white_check_mark:
Camera :white_check_mark: :white_check_mark: :white_check_mark:
Gesture :white_check_mark: :white_check_mark: :white_check_mark:
User Location :white_check_mark: :white_check_mark: :white_check_mark:
Style DSL :x: :x: :x:
Raster Layer :white_check_mark: :white_check_mark: :white_check_mark:
Symbol Layer :white_check_mark: :white_check_mark: :white_check_mark:
Circle Layer :white_check_mark: :white_check_mark: :white_check_mark:
Line Layer :white_check_mark: :white_check_mark: :white_check_mark:
Fill Layer :white_check_mark: :white_check_mark: :white_check_mark:
Fill Extrusion Layer :white_check_mark: :white_check_mark: :white_check_mark:
Hillshade Layer :white_check_mark: :white_check_mark: :white_check_mark:
Heatmap Layer :white_check_mark: :white_check_mark: :white_check_mark:
Vector Source :white_check_mark: :white_check_mark: :white_check_mark:
Raster Source :white_check_mark: :white_check_mark: :white_check_mark:
GeoJson Source :white_check_mark: :white_check_mark: :white_check_mark:
Image Source :white_check_mark: :white_check_mark: :white_check_mark:
Expressions :white_check_mark: :white_check_mark: :white_check_mark:
Symbol Annotation :white_check_mark: :white_check_mark: :white_check_mark:
Circle Annotation :white_check_mark: :white_check_mark: :white_check_mark:
Line Annotation :white_check_mark: :white_check_mark: :white_check_mark:
Fill Annotation :white_check_mark: :white_check_mark: :white_check_mark:

Map Styles

Map styles can be supplied by setting the styleString in the MapOptions. The following formats are supported:

  1. Passing the URL of the map style. This can be one of the built-in map styles, also see MapboxStyles or a custom map style served remotely using a URL that start with 'http(s)://' or 'mapbox://'
  2. Passing the style as a local asset. Create a JSON file in the assets and add a reference in pubspec.yml. Set the style string to the relative path for this asset in order to load it into the map.
  3. Passing the style as a local file. create an JSON file in app directory (e.g. ApplicationDocumentsDirectory). Set the style string to the absolute path of this JSON file.
  4. Passing the raw JSON of the map style. This is only supported on Android.

Offline Sideloading

Support for offline maps is available by side loading the required map tiles and including them in your assets folder.

   assets:
     - assets/cache.db
    try {
      await installOfflineMapTiles(join("assets", "cache.db"));
    } catch (err) {
      print(err);
    }

Downloading Offline Regions

An offline region is a defined region of a map that is available for use in conditions with limited or no network connection. Tiles for selected region, style and precision are downloaded from Mapbox using proper SDK methods and stored in application's cache.

    final Function(DownloadRegionStatus event) onEvent = (DownloadRegionStatus status) {
      if (status.runtimeType == Success) {
        // ...
      } else if (status.runtimeType == InProgress) {
        int progress = (status as InProgress).progress.round();
        // ...
      } else if (status.runtimeType == Error) {
        // ...
      }
    };

    final OfflineRegion offlineRegion = OfflineRegion(
      bounds: LatLngBounds(
        northeast: LatLng(52.5050648, 13.3915634),
        southwest: LatLng(52.4943073, 13.4055383),
      ),
      id: 1,
      minZoom: 6,
      maxZoom: 18,
      mapStyleUrl: 'mapbox://styles/mapbox/streets-v11',
    );

    downloadOfflineRegionStream(offlineRegion, onEvent);

Create a static map snapshot

The snapshotManager generates static raster images of the map. Each snapshot image depicts a portion of a map defined by an SnapshotOptions object you provide.

    final renderBox = mapKey.currentContext?.findRenderObject() as RenderBox;

    final snapshotOptions = SnapshotOptions(
      width: renderBox.size.width,
      height: renderBox.size.height,
      writeToDisk: true,
      withLogo: false,
    );

    final uri = await mapController?.takeSnapshot(snapshotOptions);

Location features

Android

Add the ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permission in the application manifest android/app/src/main/AndroidManifest.xml to enable location features in an Android application:

<manifest ...
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

Starting from Android API level 23 you also need to request it at runtime. This plugin does not handle this for you. The example app uses the flutter 'location' plugin for this.

iOS

To enable location features in an iOS application:

If you access your users' location, you should also add the following key to ios/Runner/Info.plist to explain why you need access to their location data:

xml ...
    <key>NSLocationWhenInUseUsageDescription</key>
    <string>[Your explanation here]</string>

Recommended explanation about "Shows your location on the map and helps improve the map".

Flutter 3.x.x issues and experimental workarounds

Since Flutter 3.x.x was introduced, it exposed some race conditions resulting in occasional crashes upon map disposal. The parameter useDelayedDisposal was introduced as a workaround for this issue until Flutter and/or Mapbox fix this issue properly. Use with caution - this is not yet production ready since several users still report crashes after using this workaround.

Running the example code

See the documentation about this topic

Contributing

We welcome contributions to this repository! If you're interested in helping build this Mapbox-Flutter integration, please read the contribution guide to learn how to get started.