SPB Git

Repository Public

LocationMap.tsx 4.5 KB 107 lines tsx

1/**2 * =============================================================================3 *                  SP BOUCHER | SOFTWARE & RESEARCH4 *             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project          : UQO Éval — Le Grand Défi Immobilier8 * File             : apps/web/src/components/LocationMap.tsx9 * Module           : components10 * Repository       : https://git.spboucher.ai/uqo-eval-jeux11 * Project Website  : https://jeux.uqo-eval.app12 * Created          : 2026-11-2513 * Last Modified    : 2026-11-2514 *15 * Author           : Simon-Pierre Boucher16 * Position         : Professor of Finance17 * Institution      : Université du Québec en Outaouais (UQO)18 * Organization     : SP Boucher19 * Website          : https://spboucher.ai20 * Git              : https://git.spboucher.ai21 *22 * Preferred Contact: contact@spboucher.ai23 * Development      : dev@spboucher.ai24 * Support          : support@spboucher.ai25 * Security/Admin   : admin@spboucher.ai26 *27 * Copyright (c) 202628 * Simon-Pierre Boucher29 * All rights reserved where applicable. This file is governed by the30 * applicable project license, copyright notices and relevant law.31 * Existing third-party rights and notices remain unaffected.32 *33 * License          : None specified (no license file in this repository)34 *35 * Do not embed credentials, private keys, access tokens,36 * certificates or confidential personal information.37 *38 * =============================================================================39 * END OF SP BOUCHER SOURCE HEADER40 * =============================================================================41 */4243import { useEffect, useRef, useState } from "react";4445/** Polygone approchant un cercle de `radiusM` mètres. */46function circle(lat: number, lng: number, radiusM: number): [number, number][] {47  const points: [number, number][] = [];48  for (let i = 0; i <= 64; i++) {49    const a = (i / 64) * 2 * Math.PI;50    points.push([lng + (radiusM * Math.sin(a)) / (111_320 * Math.cos((lat * Math.PI) / 180)), lat + (radiusM * Math.cos(a)) / 111_320]);51  }52  return points;53}5455/**56 * Carte de l'emplacement approximatif. Le serveur ne transmet qu'un point57 * décalé : le cercle indique le secteur, jamais l'adresse.58 */59export default function LocationMap({ location, label }: { location: { lat: number; lng: number; radiusM: number }; label: string }) {60  const node = useRef<HTMLDivElement>(null);61  const [failed, setFailed] = useState(false);6263  useEffect(() => {64    let map: import("maplibre-gl").Map | null = null;65    let cancelled = false;66    void (async () => {67      try {68        const [{ default: maplibre }] = await Promise.all([import("maplibre-gl"), import("maplibre-gl/dist/maplibre-gl.css")]);69        if (cancelled || !node.current) return;70        map = new maplibre.Map({71          container: node.current,72          style: "https://tiles.openfreemap.org/styles/positron",73          center: [location.lng, location.lat],74          zoom: 12.6,75          attributionControl: { compact: true },76          cooperativeGestures: false,77        });78        map.addControl(new maplibre.NavigationControl({ showCompass: false }), "top-right");79        map.on("error", () => undefined);80        map.on("load", () => {81          if (!map) return;82          map.addSource("secteur", {83            type: "geojson",84            data: { type: "Feature", properties: {}, geometry: { type: "Polygon", coordinates: [circle(location.lat, location.lng, location.radiusM)] } },85          });86          map.addLayer({ id: "secteur-fond", type: "fill", source: "secteur", paint: { "fill-color": "#2bccae", "fill-opacity": 0.26 } });87          map.addLayer({ id: "secteur-trait", type: "line", source: "secteur", paint: { "line-color": "#006085", "line-width": 2 } });88        });89      } catch {90        setFailed(true);91      }92    })();93    return () => {94      cancelled = true;95      map?.remove();96    };97  }, [location.lat, location.lng, location.radiusM]);9899  if (failed) return <p className="muted small">La carte n'a pas pu être chargée. Emplacement : {label}.</p>;100  return (101    <figure className="map">102      <div ref={node} className="map-canvas" role="application" aria-label={`Carte du secteur : ${label}. Le cercle indique un emplacement approximatif.`} />103      <figcaption className="small muted">Emplacement approximatif (rayon d'environ {location.radiusM} m). L'adresse exacte n'est pas divulguée.</figcaption>104    </figure>105  );106}107
Files main
Menu

Theme