Instance Method

init

Initialize MapKit JS by providing an authorization callback function and optional language.

Declaration

void init(MapKitInitOptions options);

Parameters

options

MapKit JS initialization options.

Discussion

Once you have created a JWT according to the guidelines in Creating and Using Tokens with MapKit JS, you are ready to begin using MapKit JS. The first step is to initialize the mapkit object with a token. To do this, set authorizationCallback to a function that obtains a token and passes it to the done() function.

If you have created a server endpoint to deliver new tokens to MapKit JS, make an asynchronous request to this endpoint in your authorizationCallback function and call done() with the result.

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

If you haven't set up a server endpoint, you can alternately set authorizationCallback to a function that simply provides a pre-generated token string.

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

It is safe to call mapkit.init before the content of your page has fully loaded; in other words, before the window.load event fires. A mapkit object dispatches a configuration-change event once it has been initialized, and an error event when initialization fails. You can learn more about both of these events in Handling Initialization Events.

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.