Skip to content

Commit

Permalink
attribution support
Browse files Browse the repository at this point in the history
  • Loading branch information
zbigg committed Apr 30, 2024
1 parent ec279e9 commit c794f07
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 13 deletions.
7 changes: 3 additions & 4 deletions docs/api-reference/carto/fetch-map.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,12 @@ fetchMap({cartoMapId}).then(map => new Deck(map));
```js
import mapboxgl from 'mapbox-gl';

fetchMap({cartoMapId}).then(({initialViewState, mapStyle, layers}) => {
// Add Mapbox GL for the basemap. It's not a requirement if you don't need a basemap.
const MAP_STYLE = `https://basemaps.cartocdn.com/gl/${mapStyle.styleType}-gl-style/style.json`;
fetchMap({cartoMapId}).then(({initialViewState, basemap, layers}) => {
const deckgl = new deck.DeckGL({
container: 'container',
controller: true,
mapStyle: MAP_STYLE,
// Add Mapbox GL for the basemap. It's not a requirement if you don't need a basemap.
mapStyle: basemap.styleUrl,
initialViewState,
layers
});
Expand Down
20 changes: 11 additions & 9 deletions modules/carto/src/api/basemap-style.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import {KeplerMapConfig} from './types';
import {Basemap, KeplerMapConfig} from './types';

const getRasterJsonMapStyle = (url: string) => ({
const getRasterJsonMapStyle = (basemap: Basemap) => ({
version: 8,
sources: {
'basemap-tile-source': {
type: 'raster',
tiles: [url],
tiles: [basemap.settings.url],
tileSize: 256
}
},
Expand All @@ -17,7 +17,8 @@ const getRasterJsonMapStyle = (url: string) => ({
minzoom: 0,
maxzoom: 22
}
]
],
attribution: basemap.attribution
});

function isRasterBasemap(url: string) {
Expand All @@ -32,13 +33,14 @@ function isWmtsBasemap(url: string) {
return url.includes('service=WMS');
}

export function getCustomBasemapStyle(url: string): string | any {
export function getCustomBasemapStyle(basemap: Basemap): string | any {
const url = basemap.settings.url;
if (isRasterBasemap(url)) {
return getRasterJsonMapStyle(url);
return getRasterJsonMapStyle(basemap);
} else if (isTileJsonBasemap(url)) {
return url;
} else if (isWmtsBasemap(url)) {
return getRasterJsonMapStyle(url);
return getRasterJsonMapStyle(basemap);
}
throw new Error('Unknown basemap format');
}
Expand All @@ -55,15 +57,15 @@ export function getCartoMapStyle(config: KeplerMapConfig) {
const currentCustomBasemap = config.customBaseMaps?.custom;
if (currentCustomBasemap) {
return {
styleUrl: getCustomBasemapStyle(currentCustomBasemap.settings.url),
styleUrl: getCustomBasemapStyle(currentCustomBasemap),
attribution: currentCustomBasemap.attribution
};
}
}
if (CARTO_MAP_STYLES.includes(styleType)) {
const {label} = mapStyle.visibleLayerGroups;
const labelSuffix = label ? '' : '-nolabels';
const styleUrl = `${CARTO_MAP_BASEURL}${mapStyle.styleType}${labelSuffix}-gl-style/style.json`;
const styleUrl = `${CARTO_MAP_BASEURL}${styleType}${labelSuffix}-gl-style/style.json`;
return {
styleUrl,
attribution: CARTO_MAP_ATRRIBUTION
Expand Down

0 comments on commit c794f07

Please sign in to comment.