Skip to content

Commit

Permalink
fix: only display marker when coords exist (#1074)
Browse files Browse the repository at this point in the history
  • Loading branch information
XavierJp committed May 28, 2024
1 parent 0c158bb commit 707a60f
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 51 deletions.
27 changes: 27 additions & 0 deletions components/map/check-lat-lng.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { LngLatLike } from 'maplibre-gl';

export function checkLatLng(
latitude: string,
longitude: string
): LngLatLike | null {
try {
const lat = parseFloat(latitude);
const lng = parseFloat(longitude);
if (
isNaN(lat) ||
isNaN(lng) ||
lat < -90 ||
lat > 90 ||
lng < -180 ||
lng > 180
) {
throw new Error('Invalid coords');
}

return {
lat,
lng,
};
} catch {}
return null;
}
26 changes: 2 additions & 24 deletions components/map/map-etablissement.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,12 @@
import constants from '#models/constants';
import { IEtablissement } from '#models/core/types';
import { formatSiret } from '#utils/helpers';
import maplibregl, { LngLatLike, Map } from 'maplibre-gl';
import maplibregl, { Map } from 'maplibre-gl';
import 'maplibre-gl/dist/maplibre-gl.css';
import { useEffect, useRef } from 'react';
import { checkLatLng } from './check-lat-lng';
import './map.css';

function checkLatLng(latitude: string, longitude: string): LngLatLike | null {
try {
const lat = parseFloat(latitude);
const lng = parseFloat(longitude);
if (
isNaN(lat) ||
isNaN(lng) ||
lat < -90 ||
lat > 90 ||
lng < -180 ||
lng > 180
) {
throw new Error('Invalid coords');
}

return {
lat,
lng,
};
} catch {}
return null;
}

export function MapEtablissement({
etablissement,
}: {
Expand Down
65 changes: 38 additions & 27 deletions components/map/map-results.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { formatIntFr, formatSiret } from '#utils/helpers';
import maplibregl, { Map } from 'maplibre-gl';
import 'maplibre-gl/dist/maplibre-gl.css';
import { useEffect, useRef } from 'react';
import { checkLatLng } from './check-lat-lng';
import './map.css';

export default function MapWithResults({
Expand Down Expand Up @@ -35,42 +36,52 @@ export default function MapWithResults({
});

results.results.forEach((result) => {
var popup = new maplibregl.Popup({ offset: 25 }).setHTML(
`<div><strong><a href="/entreprise/${result.chemin}">${formatIntFr(
result.siren
)}</a></strong><br/>${result.nomComplet}<br/>📍${
result.siege.adresse
}</div>`
const coordsSiege = checkLatLng(
result.siege.latitude,
result.siege.longitude
);

new maplibregl.Marker({ color: constants.colors.frBlue })
//@ts-ignore
.setLngLat([result.siege.longitude, result.siege.latitude])
.setPopup(popup)
//@ts-ignore
.addTo(map.current);
if (coordsSiege) {
const popup = new maplibregl.Popup({ offset: 25 }).setHTML(
`<div><strong><a href="/entreprise/${result.chemin}">${formatIntFr(
result.siren
)}</a></strong><br/>${result.nomComplet}<br/>📍${
result.siege.adresse
}</div>`
);

new maplibregl.Marker({ color: constants.colors.frBlue })
//@ts-ignore
.setLngLat([result.siege.longitude, result.siege.latitude])
.setPopup(popup)
//@ts-ignore
.addTo(map.current);
}

result.matchingEtablissements.forEach((match) => {
if (match.estSiege) {
return null;
}

var popup = new maplibregl.Popup({ offset: 25 }).setHTML(
`<div><strong><a href="/etablissement/${match.siret}">${formatSiret(
match.siret
)}</a></strong><br/>Etablissement secondaire de <a href="/entreprise/${
match.siren
}">${result.nomComplet}</a><br/>📍${match.adresse}</div>`
);
const coordsEtab = checkLatLng(match.latitude, match.longitude);
if (coordsEtab) {
var popup = new maplibregl.Popup({ offset: 25 }).setHTML(
`<div><strong><a href="/etablissement/${match.siret}">${formatSiret(
match.siret
)}</a></strong><br/>Etablissement secondaire de <a href="/entreprise/${
match.siren
}">${result.nomComplet}</a><br/>📍${match.adresse}</div>`
);

new maplibregl.Marker({
color: shouldColorZipCode ? 'yellow' : constants.colors.pastelBlue,
})
//@ts-ignore
.setLngLat([match.longitude, match.latitude])
.setPopup(popup)
//@ts-ignore
.addTo(map.current);
new maplibregl.Marker({
color: shouldColorZipCode ? 'yellow' : constants.colors.pastelBlue,
})
//@ts-ignore
.setLngLat([match.longitude, match.latitude])
.setPopup(popup)
//@ts-ignore
.addTo(map.current);
}
});
});
}, [results, shouldColorZipCode]);
Expand Down

0 comments on commit 707a60f

Please sign in to comment.