Skip to content

Commit

Permalink
🐛 fix createShowPoisOnMap fetch url useless
Browse files Browse the repository at this point in the history
  • Loading branch information
JohannLai committed Jan 28, 2024
1 parent 74ebaba commit ac76320
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 8 deletions.
3 changes: 3 additions & 0 deletions tools/showPoisOnMap.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@ it('should successfully get a map', async () => {
}
],
mapStyle: 'streets-v12',
zoom: 10,
});

console.log(result);

expect(result).toMatchObject({
imageURL: expect.stringContaining('https://api.mapbox.com/styles/v1'),
});
Expand Down
11 changes: 3 additions & 8 deletions tools/showPoisOnMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function createShowPoisOnMap({
const description = `
Displays specific Points of Interest (POIs) on a map using the Mapbox Static API. Returns a URL to the map image.
pois: An array of POIs to be displayed on the map.
zoom: The zoom level for the map depends from the place size. For larger places use min value and for smaller use max. For countries use zoom '1.0'-'3.0'; for national parks, states use '4.0'-'6.0'; landmarks, places or cities use '7.0'-'9.0'. For streets use '10.0'-'20.0'. If multiple places are provided, this will automatically set to 'auto'.
zoom: The zoom level for the map depends from the place size. For larger places use min value and for smaller use max. For countries use zoom '1.0'-'3.0'; for national parks, states use '4.0'-'6.0'; landmarks, places or cities use '7.0'-'9.0'. For streets use '15'. If multiple places are provided, this will automatically set to 'auto'.
mapStyle: The style of the map. Can be 'streets-v12' or 'satellite-streets-v12'.
`

Expand All @@ -37,7 +37,7 @@ function createShowPoisOnMap({

let markers;
let padding = "";
let formatZoom = (pois.length == 1 ? 9 : 'auto');
let formatZoom = zoom?.toString() || (pois.length == 1 ? '9' : 'auto'); // Use provided zoom level or default
if (pois.length > 1) {
markers = pois.map((poi, index) => `pin-s-${index + 1}+FF2F48(${poi.longitude},${poi.latitude})`).join(',');
padding = "&padding=50,50,50,50";
Expand All @@ -47,12 +47,7 @@ function createShowPoisOnMap({
formatZoom = `${formatZoom},0`;
}
let coordinates = pois.length == 1 ? `${pois[0].longitude},${pois[0].latitude},${formatZoom}` : 'auto';
let url = `https://api.mapbox.com/styles/v1/mapbox/${mapStyle}/static/${markers}/${coordinates}/600x400@2x?access_token=${accessToken}${padding}`;

let response = await fetch(url);
if (!response.ok) {
throw new Error('Network response was not ok');
}
let url = `https://api.mapbox.com/styles/v1/mapbox/${mapStyle || 'streets-v12'}/static/${markers}/${coordinates}/600x400@2x?access_token=${accessToken}${padding}`;

return { imageURL: url };
};
Expand Down

0 comments on commit ac76320

Please sign in to comment.