Contents

init(options)

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

Declaration

init(options: MapKitInitializationOptions): void;

Parameters

  • options:

    MapKit JS initialization options.

Mentioned in

Discussion

Unless you wish to explicitly control initialization timing in JavaScript, use data-token instead of this method. See Loading the latest version of MapKit JS for more information.

If you’re using this method, provide the token by setting authorizationCallback on a JavaScript object as a function. The authorizationCallback function calls done() to return the token. Creating a Maps token shows how to create Maps tokens.

When you create 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. The following example shows creating a callback that requests a token from a server endpoint and sets a preferred language for the map:

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

If you don’t set up a server endpoint, you can alternatively set authorizationCallback to a function that provides a pregenerated token string.

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

An instance of mapkit emits a configuration-change event after MapKit JS initializes, and an error event when initialization fails. You can learn more about both of these events in Handling initialization events.

See Also

Initialization