Given longitude and latitude, how can I get the code and name of a country?
From this source I have obtained the following data that correspond to the latitude and longitude of countries. How can I get the name and code of the country in which some coordinates are located?
[
{
"countryCode": "AD",
"latitude": "42.546245",
"longitude": "1.601554",
"countryName": "Andorra"
},
{
"countryCode": "AE",
"latitude": "23.424076",
"longitude": "53.847818",
"countryName": "United Arab Emirates"
},
{
"countryCode": "AF",
"latitude": "33.93911",
"longitude": "67.709953",
"countryName": "Afghanistan"
},
{
"countryCode": "AG",
"latitude": "17.060816",
"longitude": "-61.796428",
"countryName": "Antigua and Barbuda"
},
{
"countryCode": "AI",
"latitude": "18.220554",
"longitude": "-63.068615",
"countryName": "Anguilla"
},
{
"countryCode": "AL",
"latitude": "41.153332",
"longitude": "20.168331",
"countryName": "Albania"
},
{
"countryCode": "AM",
"latitude": "40.069099",
"longitude": "45.038189",
"countryName": "Armenia"
},
{
"countryCode": "AN",
"latitude": "12.226079",
"longitude": "-69.060087",
"countryName": "Netherlands Antilles"
},
{
"countryCode": "AO",
"latitude": "-11.202692",
"longitude": "17.873887",
"countryName": "Angola"
},
...
]
Full dataset https://jsfiddle.net/kmznxrhe/
For example, given the following coordinates Latitude: 12.4157952, Longitude: -86.8777984, how can I obtain the name and code of the country in which those coordinates are located?
I do not share an example because I do not really know how to do this task
Thanks in advance
With this dataset the only thing that you can do is get the country by a specific latitude and longitude
const data = [
{
"countryCode": "country",
"latitude": "latitude",
"longitude": "longitude",
"countryName": "name"
},
{
"countryCode": "NR",
"latitude": "-0.522778",
"longitude": "166.931503",
"countryName": "Nauru"
},
{
"countryCode": "ZW",
"latitude": "-19.015438",
"longitude": "29.154857",
"countryName": "Zimbabwe"
}
]
let myCountry = data.find(country => country.latitude === "-19.015438" && country.longitude === "29.154857")
console.log(myCountry)
//output
//{
// "countryCode": "ZW",
// "latitude": "-19.015438",
// "longitude": "29.154857",
// "countryName": "Zimbabwe"
//}
But you can't check the coverage (for the example: 12.4157952, -86.8777984), to do that I recommend this post
I have obtained the following data that correspond to the latitude and longitude of countries.
So your dataset contains only a single coordinate that is inside the country, without giving any information about the borderline of said country.
How can I obtain the name and code of the country in which those coordinates are located?
This process is called reverse geocoding. There are some APIs available for this task, e.g. OSM's Nominatim API.
An example URL querying the coordinates you gave as an example: https://nominatim.openstreetmap.org/reverse?lat=12.4157952&lon=-86.8777984&zoom=3&format=json