using System.Collections.Generic; using UnityEngine; [System.Serializable] public class FsaEntry { public string fsa; public double lat; public double lon; } [System.Serializable] public class FsaTable { public FsaEntry[] entries; } public class FsaLocator : MonoBehaviour { [Header("FSA JSON (FSA_Canada.json dans Resources)")] public TextAsset fsaJson; private Dictionary fsaLookup; private void Awake() { fsaLookup = new Dictionary(); if (fsaJson == null) { Debug.LogError("FsaLocator: fsaJson n'est pas assigné."); return; } FsaTable table = JsonUtility.FromJson(fsaJson.text); if (table == null || table.entries == null) { Debug.LogError("FsaLocator: JSON FSA invalide."); return; } foreach (var e in table.entries) { if (string.IsNullOrEmpty(e.fsa)) continue; string key = e.fsa.ToUpper(); if (!fsaLookup.ContainsKey(key)) { fsaLookup[key] = new Vector2((float)e.lat, (float)e.lon); } } Debug.Log("FsaLocator: " + fsaLookup.Count + " FSA chargés."); } public bool TryGetLatLonFromPostal(string postalCode, out double lat, out double lon) { lat = 0; lon = 0; if (string.IsNullOrWhiteSpace(postalCode)) return false; string pc = postalCode.ToUpper().Trim().Replace(" ", ""); if (pc.Length < 3) return false; string fsa = pc.Substring(0, 3); if (fsaLookup != null && fsaLookup.TryGetValue(fsa, out Vector2 pos)) { lat = pos.x; lon = pos.y; return true; } return false; } }