给定经度和纬度,我怎样才能得到一个国家的代码和名称?

Given longitude and latitude, how can I get the code and name of a country?

给定经纬度,如何获取国家代码和名称?

由此source我得到了以下对应国家经纬度的数据。如何获取坐标所在国家的名称和代码?

[
  {
    "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"
  },
  ...
]

完整数据集https://jsfiddle.net/kmznxrhe/

例如,给定以下坐标纬度:12.4157952,经度:-86.8777984,如何获取这些坐标所在国家/地区的名称和代码?

我不分享示例,因为我真的不知道如何完成此任务

提前致谢

I have obtained the following data that correspond to the latitude and longitude of countries.

因此您的数据集仅包含一个 单一 位于该国家/地区内部的坐标,而没有提供有关该国家/地区边界线的任何信息。

How can I obtain the name and code of the country in which those coordinates are located?

此过程称为反向地理编码。有一些 API 可用于此任务,例如OSM 的 Nominatim API.

一个例子URL查询你给出的坐标作为例子: https://nominatim.openstreetmap.org/reverse?lat=12.4157952&lon=-86.8777984&zoom=3&format=json

有了这个数据集,您唯一能做的就是通过特定的经纬度

获取国家/地区

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"
//}

但是您无法检查覆盖范围(例如:12.4157952、-86.8777984),为此我建议 this post