Skip to content

Commit

Permalink
fix MapTiler map loading in other SDKs + fix resizing
Browse files Browse the repository at this point in the history
  • Loading branch information
petr-hajek committed Dec 20, 2024
1 parent d42e5bd commit 0c17f53
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 14 deletions.
2 changes: 1 addition & 1 deletion dist/bundle.css

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions dist/bundle.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/bundle.js.map

Large diffs are not rendered by default.

7 changes: 5 additions & 2 deletions src/components/GlMap.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import throttle from 'lodash.throttle';
import { createEventDispatcher, onDestroy, onMount } from 'svelte';
import { config as configStore } from '../stores';
import { normalizeMapTilerUrl } from '../maptiler-urls';
export let id;
export let bearing;
Expand Down Expand Up @@ -50,7 +51,9 @@
let mapViewProps = {};
// We can set style (an object) here because mapStyle only changes when it needs to
$: ({ style, url } = mapStyle);
$: ({ style, url: baseUrl } = mapStyle);
$: url = normalizeMapTilerUrl(baseUrl, $configStore.maptilerApiKey);
// We group map-view props here as they are useful in a few contexts
$: mapViewProps = { bearing, center, pitch, zoom };
Expand Down Expand Up @@ -199,7 +202,7 @@
// Resize the map when adding more maps and changing container size
$: if (map && numberOfMaps) {
// As of `v3.0.0` maplibre no longer needs this resizing: https://github.com/maplibre/maplibre-gl-js/blob/main/CHANGELOG.md#potentially-breaking-changes
if (mapRenderer !== 'maplibre-gl' || mapRenderer !== 'maptiler-sdk') {
if (mapRenderer !== 'maplibre-gl' && mapRenderer !== 'maptiler-sdk') {
map.once('render', () => {
const container = document.getElementById(id);
if (container) {
Expand Down
9 changes: 3 additions & 6 deletions src/fetch-url.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { config as configStore } from './stores';
import { isMapboxUrl, normalizeMapboxUrl } from './mapbox-urls';
import { normalizeMapTilerUrl } from './maptiler-urls';

let mapboxGlAccessToken, maptilerApiKey;
configStore.subscribe(
Expand All @@ -11,12 +12,8 @@ const fetchUrl = async url => {
let nextUrl = new URL(url);
if (urlIsMapbox) {
nextUrl = normalizeMapboxUrl(nextUrl.toString(), mapboxGlAccessToken);
} else if (
maptilerApiKey &&
nextUrl.host === 'api.maptiler.com' &&
!nextUrl.searchParams.has('key')
) {
nextUrl.searchParams.append('key', maptilerApiKey);
} else if (nextUrl.host === 'api.maptiler.com') {
nextUrl = normalizeMapTilerUrl(nextUrl, maptilerApiKey);
}
let response;
try {
Expand Down
14 changes: 14 additions & 0 deletions src/maptiler-urls.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
* Add key to MapTiler url if it is missing
*/
export const normalizeMapTilerUrl = (url, maptilerApiKey) => {
let nextUrl = new URL(url);
if (
maptilerApiKey &&
nextUrl.host === 'api.maptiler.com' &&
!nextUrl.searchParams.has('key')
) {
nextUrl.searchParams.append('key', maptilerApiKey);
}
return nextUrl.toString();
};

0 comments on commit 0c17f53

Please sign in to comment.