Initialize a Map
Load a GrabMaps map using MapLibre GL JS and an OAuth access token.
MapLibre GL JS setup
Use MapLibre with Grab map data. MapLibre gives you control over the map container, camera, controls, sources, and layers. Grab provides the authenticated map style and tiles.
// Obtain an access token from your server before loading the map.
const accessToken = '<ACCESS_TOKEN>';
fetch('https://api.maps.grab.com/api/v1/styles/urban-light-partner/descriptor', {
headers: {
'Authorization': `Bearer ${accessToken}`
}
})
.then(response => {
if (!response.ok) {
throw new Error('Request failed with HTTP ' + response.status);
}
return response.json();
})
.then(style => {
const map = new maplibregl.Map({
container: 'map',
style: style,
center: [103.8198, 1.3521],
zoom: 12,
transformRequest: (url) => {
const resourceUrl = new URL(url, window.location.href);
return resourceUrl.origin === 'https://api.maps.grab.com'
? { url, headers: { Authorization: `Bearer ${accessToken}` } }
: { url };
},
});
map.addControl(new maplibregl.NavigationControl(), 'top-right');
});Common map options
| Option | Description |
|---|---|
center | Starting map center as [longitude, latitude]. |
zoom | 0–22. Use 10–15 for city-level views in Southeast Asia. |
pitch | 0–60. Recommended to use 45°. Higher values tilt the camera. |
bearing | Rotate the map clockwise in degrees. |
minZoom / maxZoom | Limit the zoom levels users can reach. |
maxBounds | Restrict panning to a bounding box |
Adding data layers
Add GeoJSON sources and layers to display search results or routes on the map.
map.on('load', () => {
map.addSource('selected-place', {
type: 'geojson',
data: {
type: 'FeatureCollection',
features: []
}
});
map.addLayer({
id: 'selected-place-point',
type: 'circle',
source: 'selected-place',
paint: {
'circle-radius': 8,
'circle-color': '#1FBF7F',
'circle-stroke-color': '#ffffff',
'circle-stroke-width': 2
}
});
});API reference
Endpoints used when initializing a map.
GET
/api/v1/styles/{Style}/descriptorReturns the selected named map style for MapLibre GL JS. Use urban-light-partner as the default style.
// Obtain an access token from your server before loading the map.
const accessToken = '<ACCESS_TOKEN>';
fetch('https://api.maps.grab.com/api/v1/styles/urban-light-partner/descriptor', {
headers: {
'Authorization': `Bearer ${accessToken}`
}
})
.then(response => {
if (!response.ok) {
throw new Error('Request failed with HTTP ' + response.status);
}
return response.json();
})
.then(style => {
const map = new maplibregl.Map({
container: 'map',
style: style,
center: [103.8198, 1.3521],
zoom: 12,
transformRequest: (url) => {
const resourceUrl = new URL(url, window.location.href);
return resourceUrl.origin === 'https://api.maps.grab.com'
? { url, headers: { Authorization: `Bearer ${accessToken}` } }
: { url };
},
});
map.addControl(new maplibregl.NavigationControl(), 'top-right');
});Response format
{
"version": 8,
"sources": { "map": { "type": "raster", "tiles": ["..."], ... } },
"layers": [...]
}