map plugin : improve styles
This commit is contained in:
parent
2b0f4f8742
commit
b47195488a
3 changed files with 6 additions and 81 deletions
|
|
@ -1,76 +0,0 @@
|
|||
/**
|
||||
* Geocoding utility using Nominatim API
|
||||
* https://nominatim.openstreetmap.org/
|
||||
*
|
||||
* Usage policy: https://operations.osmfoundation.org/policies/nominatim/
|
||||
* Rate limit: 1 request per second
|
||||
*/
|
||||
|
||||
const NOMINATIM_URL = 'https://nominatim.openstreetmap.org/search';
|
||||
|
||||
/**
|
||||
* Search for an address using Nominatim
|
||||
* @param {string} query - Address to search for
|
||||
* @returns {Promise<Array>} Array of results with lat, lon, display_name, etc.
|
||||
*/
|
||||
export async function geocode(query) {
|
||||
if (!query || query.trim().length < 3) {
|
||||
return [];
|
||||
}
|
||||
|
||||
try {
|
||||
const params = new URLSearchParams({
|
||||
q: query.trim(),
|
||||
format: 'json',
|
||||
addressdetails: '1',
|
||||
limit: '5',
|
||||
// Respectful user agent as requested by Nominatim policy
|
||||
'accept-language': 'fr'
|
||||
});
|
||||
|
||||
const response = await fetch(`${NOMINATIM_URL}?${params.toString()}`, {
|
||||
headers: {
|
||||
'User-Agent': 'GeoProject/1.0 (Kirby CMS Map Editor)'
|
||||
}
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`Nominatim API error: ${response.status}`);
|
||||
}
|
||||
|
||||
const data = await response.json();
|
||||
|
||||
// Transform results to a consistent format
|
||||
return data.map(result => ({
|
||||
id: result.place_id,
|
||||
displayName: result.display_name,
|
||||
lat: parseFloat(result.lat),
|
||||
lon: parseFloat(result.lon),
|
||||
type: result.type,
|
||||
importance: result.importance,
|
||||
boundingBox: result.boundingbox
|
||||
}));
|
||||
|
||||
} catch (error) {
|
||||
console.error('Geocoding error:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Debounce function to limit API calls
|
||||
* @param {Function} func - Function to debounce
|
||||
* @param {number} wait - Milliseconds to wait
|
||||
* @returns {Function} Debounced function
|
||||
*/
|
||||
export function debounce(func, wait = 500) {
|
||||
let timeout;
|
||||
return function executedFunction(...args) {
|
||||
const later = () => {
|
||||
clearTimeout(timeout);
|
||||
func(...args);
|
||||
};
|
||||
clearTimeout(timeout);
|
||||
timeout = setTimeout(later, wait);
|
||||
};
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue