The Mapbox module provides essential methods for configuring and managing the Mapbox Maps SDK in your React Native application.
Methods
setAccessToken
Sets the Mapbox access token required for using Mapbox tiles and services.
setAccessToken(accessToken: string | null): Promise<string | null>
The Mapbox access token. Can be null if using non-Mapbox tiles.
Returns: Promise that resolves to the access token.
Example:
import { setAccessToken } from '@rnmapbox/maps';
await setAccessToken('pk.your_access_token_here');
getAccessToken
Retrieves the currently set Mapbox access token.
getAccessToken(): Promise<string>
Returns: Promise that resolves to the current access token.
Example:
import { getAccessToken } from '@rnmapbox/maps';
const token = await getAccessToken();
console.log('Current token:', token);
setTelemetryEnabled
Enables or disables Mapbox telemetry collection.
setTelemetryEnabled(telemetryEnabled: boolean): void
Whether telemetry should be enabled.
Example:
import { setTelemetryEnabled } from '@rnmapbox/maps';
setTelemetryEnabled(false);
setConnected
Sets the connection state for Mapbox. Useful for forcing offline mode.
setConnected(connected: boolean): void
Whether Mapbox should consider the device connected to the internet.
Example:
import { setConnected } from '@rnmapbox/maps';
// Force offline mode
setConnected(false);
clearData
Clears temporary map data from the cache. Useful for reducing disk usage or clearing invalid cached data.
clearData(): Promise<void>
Returns: Promise that resolves when data is cleared.
Example:
import { clearData } from '@rnmapbox/maps';
await clearData();
console.log('Map cache cleared');
Adds a custom HTTP header to Mapbox requests.
addCustomHeader(
headerName: string,
headerValue: string,
options?: { urlRegexp?: string }
): void
The name of the header to add.
Optional configuration.If provided, the header will only be added to URLs matching this regular expression.
Example:
import { addCustomHeader } from '@rnmapbox/maps';
// Add header to all requests
addCustomHeader('Authorization', 'Bearer token123');
// Add header only to specific URLs
addCustomHeader('X-Custom-Header', 'value', {
urlRegexp: 'https://api\\.example\\.com/.*',
});
Removes a previously added custom HTTP header.
removeCustomHeader(headerName: string): void
The name of the header to remove.
Example:
import { removeCustomHeader } from '@rnmapbox/maps';
removeCustomHeader('Authorization');
setWellKnownTileServer
Deprecated - This method will be removed in a future version.
Sets a well-known tile server.
setWellKnownTileServer(tileServer: string): void
The tile server identifier.
Constants
StyleURL
Pre-defined Mapbox style URLs.
enum StyleURL {
Street = 'mapbox://styles/mapbox/streets-v11',
Dark = 'mapbox://styles/mapbox/dark-v10',
Light = 'mapbox://styles/mapbox/light-v10',
Outdoors = 'mapbox://styles/mapbox/outdoors-v11',
Satellite = 'mapbox://styles/mapbox/satellite-v9',
SatelliteStreet = 'mapbox://styles/mapbox/satellite-streets-v11',
TrafficDay = 'mapbox://styles/mapbox/navigation-preview-day-v4',
TrafficNight = 'mapbox://styles/mapbox/navigation-preview-night-v4',
}
Example:
import { StyleURL } from '@rnmapbox/maps';
import MapView from '@rnmapbox/maps';
<MapView styleURL={StyleURL.Dark} />
TileServers
Tile server identifiers.
const TileServers = {
Mapbox: 'mapbox',
};
LineJoin
Line join style constants.
const LineJoin = {
Bevel: 'bevel',
Round: 'round',
Miter: 'miter',
};
OfflinePackDownloadState
Offline pack download state constants.
const OfflinePackDownloadState = {
Inactive: 'inactive',
Active: 'active',
Complete: 'complete',
Unknown: 'unknown',
};