0

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

Mario
  • 4,784
  • 3
  • 34
  • 50
  • This dataset doesn't really cover the area of the country, so unless you get that I don't see how you can find which country coordinates 12.4157952, -86.8777984 belong to. – vanowm Aug 27 '21 at 21:28
  • Also: https://stackoverflow.com/questions/4497728/get-country-from-latitude-longitude – vanowm Aug 27 '21 at 21:31
  • This question appears to be duplicated. There are some possible answers at https://stackoverflow.com/questions/6747833/ – Roberto C. Rodriguez-Hidalgo Aug 27 '21 at 21:47
  • Does this answer your question? [How can I find a user’s country using HTML5 geolocation?](https://stackoverflow.com/questions/6747833/how-can-i-find-a-user-s-country-using-html5-geolocation) – Roberto C. Rodriguez-Hidalgo Aug 27 '21 at 21:49
  • I understand those are the latitude and longitude of the geometric center point for each Country and area – Mario Aug 27 '21 at 21:49

2 Answers2

1

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

0

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

slauth
  • 2,667
  • 1
  • 10
  • 18