Geocode country coordinates

node v18.11.0
version: 0.0.1
endpointsharetweet
const NodeGeocoder = require('node-geocoder'); const options = { provider: 'openstreetmap' }; const geocoder = NodeGeocoder(options); const countries = ["United States", "China", "Japan", "Germany", "United Kingdom", "South Korea", "Netherlands", "France", "Taiwan", "Spain", "Turkiye", "Kuwait", "Lituania", "Italy", "Portugal", "Mexico", "India", "Greece"]; async function getPortCoordinates(country) { try { const location = await geocoder.geocode(`${country} coastal port`); if (location && location.length > 0) { console.log(`Found ${country}`) return { lat: location[0].latitude, lng: location[0].longitude }; } else { return { lat: null, lng: null }; } } catch (error) { console.error(error); return { lat: null, lng: null }; } } async function main() { const portCoordinates = {}; for (const country of countries) { const coordinates = await getPortCoordinates(country); portCoordinates[country] = coordinates; } console.log(portCoordinates); } main();
Loading…

no comments

    sign in to comment