@צדיק-תמים לפעולה הזאת קוראים geolocation. כלומר למצוא מיקום גיאוגרפי (=קווי רוחב/אורך) על פי נתונים אחרים (לדוגמא שם עיר).
יש לזה חבילה טובה node-geocoder, היא תומכת בכמה מנועי geolocation - לחלקם יש עלויות כגון גוגל, שמצריך שימוש ב-GeoLocation API של גוגל, על ידי טוקן. אבל היא גם תומכת במנוע של openstreetmap שהוא חינמי.
אני מצרך עבורך קוד מוכן בדיוק עבור זה, שמצאתי אצלי בגינזך:
const NodeGeocoder = require('node-geocoder')
const geocoder = NodeGeocoder({ provider: 'openstreetmap' })
/**
* get location and GeoLocation library city name for entered city name
* @param {String} cityName - name of city to geolocate
* @returns {Object} city - { success: boolean, latitude: latitude, longitude: longitude, cityname: name of located city }
* @returns {Object} error - { success: boolean, error: error_message }
*/
async function getCityByName (cityName) {
const locates = await geocoder.geocode(cityName) // check if the city exists
if (!Array.isArray(locates) || !locates.length) {
return { success: false, error: 'not a city' }
} else {
for (const locate of locates) {
if (locate.countryCode !== 'IL') {
continue
}
if (locate.city === cityName) {
const city = { success: true, latitude: locate.latitude, longitude: locate.longitude, cityname: locate.city }
return city
}
}
return { success: false, error: 'not a city' }
}
}
module.exports = getCityByName
תתאים אותו לצרכיך, בהצלחה!!