Hi Arnie,
It's a bit of both and unless there is a Leaflet plugin you will need
to interrogate the returned country code and adjust the address
format. Sing out if you need help or want to see a JSFiddle.
I've been tiling historical military maps and displaying the modern
location via a draggable Leaflet marker. Naturally, if you want the
address shown within an HTML <div> outside Leaflet, then just use
Leaflet to find where the user clicked:
georefNewMarker.on('click', selectNewFeature);
// lots of code
selectNewFeature(e) {
currentLat = e.latlng.lat;
currentLon = e.latlng.lng;
// lots of code
}
To display the marker on a Leaflet map with the modern address, pass
the lat, lon into Nominatm and its OpenStreetMap reverse geocode API
will return the places format. See documentation at
https://nominatim.org/release-docs/latest/api/Output/
Here is an abridged version of the code that returns a modern address
straight into the marker:
async function quickGeocode() {
const URL = `
https://nominatim.openstreetmap.org/reverse?lat=${currentLat}&lon=${currentLon}&format=json&zoom=18&addressdetails=1`;
try {
const fetchResult = fetch(URL)
const response = await fetchResult;
const jsonData = await response.json();
if (jsonData) currentMarker.modernaddress = jsonData.display_name;
} catch (e) {
throw Error(e);
}
}
This is where I finish, but you now need the country code and postcode
- include addressdetails=1 in your API call. It returns data
including:
"address": { "city": "London", "state_district": "Greater London",
"state": "England", "postcode": "SW1A 2DU", "country": "United
Kingdom", "country_code": "gb" },
Depending on the number of countries you need to support, read the
contry_code and adjust the postcode display to suit with an IF
statement, a SWITCH statement or some deep level of abstraction if
that is what you prefer.
Leaflet geocoding plugins are at
https://leafletjs.com/plugins.html#geocoding.
Good luck,
Bill