Instance Method

init

Initialize a mapkit object by providing an authorization callback and language.

Declaration

void init(MapKitInitOptions options);

Parameters

options

MapKit JS initialization options.

Discussion

Once you have completed your one-time Setting Up MapKit JS on the Apple Developer Website, you are ready to initialize MapKit JS.

If you are using a server to deliver new tokens to MapKit JS, make an asynchronous request to your server here and call the done() function with the result.

mapkit.init({
    authorizationCallback: function(done) {
        fetch("/gettoken")
            .then(res => res.text())
            .then(done);
    },
    language: "es"
});

Otherwise, you can simply pass a pre-generated token string.

mapkit.init({
    authorizationCallback: function(done) {
        done("your-token-string");
    },
    language: "es"
});

The init call can be made before the content of your page has fully loaded (i.e., before the window's “load” event has fired).

That's all that's needed to start using the MapKit JS framework. MapKit JS is standalone with no dependencies on other JavaScript libraries. At the same time, it doesn't preclude you from using other JavaScript libraries and frameworks in your codebase.

Signing Your Authorization Token with JWT

Visit http://jwt.io for a list of recommended modules for most development languages. When configuring a JWT to generate a signed authorization token, you need to include all relevant properties.

The payload object accepts the following properties.

Property

Value

iat

Time issued in seconds since UNIX epoch. (required)

exp

Expiry time in seconds since UNIX epoch. (required)

iss

Your Apple Developer Team ID. (required)

origin

A domain restriction which should match the Origin header passed by a browser. (recommended)

The config.header object requires all of the following properties.

Property

Value

alg

"ES256"

typ

"JWT"

kid

Your Key ID

The code for signing a JWT token can be run server-side, and exposed as a web service to your front end. It can also be run on a development machine to generate a long-lived token to be checked directly into source code. Libraries for signing JWT tokens for most programming languages can be found at http://jwt.io.

See Also

Initialization

Handling Initialization Events

Handle events that trigger when MapKit JS initializes.

MapKitInitOptions

Initialization options for MapKit JS.

addEventListener

Subscribes a listener function to an event type.

removeEventListener

Unsubscribes a listener function from an event type.