Javascript 从经纬度获取国家

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4497728/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 12:39:18  来源:igfitidea点击:

Get country from latitude longitude

javascriptgoogle-mapsgeolocation

提问by aWebDeveloper

I would like to know how I can get the country name from latitude & longitude using JavaScript. I am open to use of Google Maps' JavaScript API. Can I also get city and zip?

我想知道如何使用 JavaScript 从纬度和经度获取国家/地区名称。我愿意使用 Google Maps 的 JavaScript API。我也可以得到城市和邮编吗?

Edit:My aim is to fill up the address field automatically, not to display it on a map.

编辑:我的目标是自动填写地址字段,而不是在地图上显示它。

回答by Christian

I don't know if it works with google maps, but there is a web service that returns a country code and takes as parameters the lat and long.

我不知道它是否适用于谷歌地图,但是有一个网络服务返回一个国家代码并将纬度和经度作为参数。

Here is an example:
http://api.geonames.org/countryCodeJSON?lat=49.03&lng=10.2&username=demo

这是一个例子:http:
//api.geonames.org/countryCodeJSON?lat=49.03&lng=10.2&username=demo

Returns a JSON data: {"languages":"de","distance":"0","countryCode":"DE","countryName":"Germany"}

返回一个 JSON 数据: {"languages":"de","distance":"0","countryCode":"DE","countryName":"Germany"}

I also found a little description:

我还找到了一个小说明:

The iso country code of any given point.

  • Webservice Type: REST
  • Url: ws.geonames.org/countryCode?
  • Parameters: lat, lng, type, lang, radius(buffer in km for closest country in coastal areas)
  • Result: returns the iso country code for the given latitude/longitude

With the parameter type=xmlthis service returns an xml document with iso country code and country name. The optional parameter lang can be used to specify the language the country name should be in. JSON output is produced with type=JSON

任何给定点的 iso 国家/地区代码。

  • 网络服务类型:REST
  • 网址: ws.geonames.org/countryCode?
  • 参数:lat, lng, type, lang, radius(沿海地区最近国家的缓冲区以公里为单位)
  • 结果:返回给定纬度/经度的 iso 国家/地区代码

使用参数,type=xml此服务返回一个带有 iso 国家/地区代码和国家/地区名称的 xml 文档。可选参数 lang 可用于指定国家/地区名称应使用的语言。 JSON 输出使用type=JSON

See the docs

查看文档

Edit:Please note that demois just a demonstration user and you should create a user account at http://www.geonames.org/loginin order to use the service.

编辑:请注意,这demo只是一个演示用户,您应该在http://www.geonames.org/login创建一个用户帐户才能使用该服务。

回答by Nishad Up

Google Geocoding API provides these results in JSON format. It has a free tier but you will need to pay for unlimited requests to the API.

Google Geocoding API 以 JSON 格式提供这些结果。它有一个免费层,但您需要为对 API 的无限制请求付费。

The API link to the request will look like this:

请求的 API 链接如下所示:

'https://maps.googleapis.com/maps/api/geocode/json?latlng=11.2742848,75.8013801&key=YOUR_API_KEY_HERE'

回答by Krishna Patel

<form id="form1" runat="server">
<div>
    <script type="text/javascript" src="http://j.maxmind.com/app/geoip.js" ></script>
    <br />Country Code:
    <script type="text/javascript">document.write(geoip_country_code());</script>
    <br />Country Name:
    <script type="text/javascript">document.write(geoip_country_name());</script>
    <br />City:
    <script type="text/javascript">document.write(geoip_city());</script>
    <br />Region:
    <script type="text/javascript">document.write(geoip_region());</script>
    <br />Region Name:
    <script type="text/javascript">document.write(geoip_region_name());</script>
    <br />Latitude:
    <script type="text/javascript">document.write(geoip_latitude());</script>
    <br />Longitude:
    <script type="text/javascript">document.write(geoip_longitude());</script>
    <br />Postal Code:
    <script type="text/javascript">document.write(geoip_postal_code());</script>

</div>
</form>

回答by headuck

If a self-contained library (i.e. no server / internet connection, and fast) is desirable, and only country information is required, the following library based on openstreetmap data can be useful - https://github.com/hlaw/codegrid-js

如果需要一个自包含的库(即没有服务器/互联网连接,并且速度快),并且只需要国家信息,那么以下基于 openstreetmap 数据的库可能会很有用 - https://github.com/hlaw/codegrid- js

<script type="text/javascript" src="[path]/codegrid.js"></script>
grid = codegrid.CodeGrid(); // initialize

grid.getCode (lat, lng, function (err, code) { ... });

The callback is called with code, the country code for the latlng.

使用codelatlng 的国家代码调用回调。

回答by aniri

Yes, you can use google maps to find the country for given latitudes and longitudes. Use the reverse geocoding service.

是的,您可以使用谷歌地图查找给定纬度和经度的国家/地区。使用反向地理编码服务

You can get the country, city, zip code and the complete address from the response you get from the server. Check the example from the link for details ;)

您可以从服务器的响应中获取国家、城市、邮政编码和完整地址。查看链接中的示例以获取详细信息;)

回答by pyros2097

If you are using golang you can use one of these 2 libraries to get the country and you don't need to even need make an api call as the data set is inbuilt so its unlimited.

如果您使用的是 golang,您可以使用这两个库之一来获取国家/地区,您甚至不需要进行 api 调用,因为数据集是内置的,因此它是无限的。

https://github.com/SocialHarvest/geobed

https://github.com/SocialHarvest/geobed

https://github.com/bradfitz/latlong

https://github.com/bradfitz/latlong