Skip to content

Allow for different Map instances to use different Mapbox access tokens #6331

Description

@anandthakker

Motivation

Currently, a Mapbox access token must be set on the global mapboxgl library object. There are some use cases that require multiple maps on a page, each using a different access token.

Design Alternatives

  1. Do nothing

It's technically possible to use the Map's transformRequest option to get different maps using different access tokens via something like:

mapboxgl.accessToken = '<access token>' // placeholder
var map1 = new mapboxgl.Map({
    container: container,
    style: style,
    transformRequest: (url) => ({url: url.replace('<access token>', token_1)})
});
var map2 = new mapboxgl.Map({
    container: container,
    style: style,
    transformRequest: (url) => ({url: url.replace('<access token>', token_2)})
});
  1. Accept accessToken as a Map option that takes precedence over mapboxgl.accessToken.

Design

While option 1 above works, I think it would be better to address this use case more directly via option 2.

Mock-Up

mapboxgl.accessToken = token_1;
var map1 = new mapboxgl.Map({ // defaults to `mapboxgl.accessToken`
    container: container,
    style: style,
});
var map2 = new mapboxgl.Map({
    container: container,
    style: style,
    accessToken: token_2 // overrides `mapboxgl.accessToken`
});

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions