给定纬度/经度坐标,我们如何找到城市/国家?
例如,如果我们有这组坐标,
"latitude": 48.858844300000001,
"longitude": 2.2943506,
我们如何找到城市/国家?
For example if we have these set of coordinates
"latitude": 48.858844300000001,
"longitude": 2.2943506,
How can we find out the city/country?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(18)
另一种选择:
优点:
缺点:
Another option:
Advantages:
Disadvantages:
免费的 Google Geocoding API 通过 HTTP REST API 提供此服务。请注意,该 API 使用和费率受到限制,但您可以付费无限制访问。
尝试此链接查看输出示例(这是 json 格式,输出也可以是 XML 格式)
https://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452&sensor=true
The free Google Geocoding API provides this service via a HTTP REST API. Note, the API is usage and rate limited, but you can pay for unlimited access.
Try this link to see an example of the output (this is in json, output is also available in XML)
https://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452&sensor=true
您需要
geopy
然后:
获取更多信息:
You need
geopy
and then:
to get more information:
Open Street Map 的 Nominatim 是一个开源替代方案。
您所要做的就是在 URL 中设置变量,它会返回该位置的城市/国家。请检查以下链接以获取官方文档:Nominatim
An Open Source alternative is Nominatim from Open Street Map.
All you have to do is set the variables in an URL and it returns the city/country of that location. Please check the following link for official documentation: Nominatim
我正在寻找类似的功能,并且看到了数据“http://download.geonames.org/export /dump/”在之前的回复中分享(感谢分享,这是一个很好的来源),并实现了基于city1000.txt数据的服务。
你可以看到它运行在
http://scatter-otl.rhcloud.com/location ?lat=36&long=-78.9(断开的链接)只需更改您所在位置的纬度和经度即可。
它部署在 OpenShift(RedHat 平台)上。长时间空闲后的首次调用可能需要一些时间,但通常性能令人满意。
请随意使用此服务...
另外,您可以在以下位置找到项目源代码:
https://github.com/turgos/Location。
I was searching for a similar functionality and I saw the data "http://download.geonames.org/export/dump/" shared on earlier reply (thank you for sharing, it is an excellent source), and implemented a service based on the cities1000.txt data.
You can see it running at
http://scatter-otl.rhcloud.com/location?lat=36&long=-78.9(broken link)Just change the latitude and longitude for your locations.
It is deployed on OpenShift (RedHat Platform). First call after a long idle period may take sometime, but usually performance is satisfactory.
Feel free to use this service as you like...
Also, you can find the project source at
https://github.com/turgos/Location.
我使用过 Geocoder,这是一个很好的 Python 库,支持多个提供商,包括 Google、Geonames、和 OpenStreetMaps,仅举几例。我尝试过使用 GeoPy 库,但它经常超时。开发自己的 GeoNames 代码并不是最有效地利用您的时间,而且您最终可能会得到不稳定的代码。根据我的经验,Geocoder 使用起来非常简单,并且有足够好的文档。下面是一些通过纬度和经度查找城市,或通过城市名称查找纬度/经度的示例代码。
I've used Geocoder, a good Python library that supports multiple providers, including Google, Geonames, and OpenStreetMaps, to mention just a few. I've tried using the GeoPy library, and it often gets timeouts. Developing your own code for GeoNames is not the best use of your time and you may end up getting unstable code. Geocoder is very simple to use in my experience, and has good enough documentation. Below is some sample code for looking up city by latitude and longitude, or finding latitude/longitude by city name.
我知道这个问题确实很老了,但我一直在研究同样的问题,我发现了一个非常高效和方便的包,
reverse_geocoder
,由 Ajay Thampi 构建。代码可在此处获取。它基于 KD 树的并行实现,对于大量点来说非常高效(我花了几秒钟才获得 100,000 个点。
它基于 此数据库,已由 @turgos 突出显示。
如果您的任务是快速查找坐标列表中的国家/地区和城市,那么这是一个很棒的工具。
I know this question is really old, but I have been working on the same issue and I found an extremely efficient and convenient package,
reverse_geocoder
, built by Ajay Thampi.The code is available here. It based on a parallelised implementation of K-D trees which is extremely efficient for large amounts of points (it took me few seconds to get 100,000 points.
It is based on this database, already highlighted by @turgos.
If your task is to quickly find the country and city of a list of coordinates, this is a great tool.
我花了大约 30 分钟试图找到如何在 Javascript 中执行此操作的代码示例。我无法找到您发布的问题的快速明确答案。所以...我自己做了。希望人们可以使用它,而不必深入研究 API 或盯着他们不知道如何阅读的代码。哈,如果没有别的事,我可以参考这篇文章来了解我自己的东西。很好的问题,感谢论坛的讨论!
这是利用 Google API。
。
I spent about an 30min trying to find a code example of how to do this in Javascript. I couldn't find a quick clear answer to the question you posted. So... I made my own. Hopefully people can use this without having to go digging into the API or staring at code they have no idea how to read. Ha if nothing else I can reference this post for my own stuff.. Nice question and thanks for the forum of discussion!
This is utilizing the Google API.
.
这实际上取决于您有哪些技术限制。
一种方法是拥有一个空间数据库,其中包含您感兴趣的国家和城市的轮廓。我所说的轮廓是指国家和城市存储为空间类型多边形。您的坐标集可以转换为空间类型点,并针对多边形进行查询,以获取该点所在的国家/城市名称。
以下是一些支持空间类型的数据库: SQL Server 2008 ,MySQL,postGIS - postgreSQL 和 Oracle。
如果您想使用服务而不是拥有自己的数据库,您可以使用雅虎的 GeoPlanet< /a>.对于服务方法,您可能需要查看此答案gis.stackexchange.com,其中介绍了解决您问题的服务的可用性。
It really depends on what technology restrictions you have.
One way is to have a spatial database with the outline of the countries and cities you are interested in. By outline I mean that countries and cities are store as the spatial type polygon. Your set of coordinates can be converted to the spatial type point and queried against the polygons to get the country/city name where the point is located.
Here are some of the databases which support spatial type: SQL server 2008, MySQL, postGIS - an extension of postgreSQL and Oracle.
If you would like to use a service in stead of having your own database for this you can use Yahoo's GeoPlanet. For the service approach you might want to check out this answer on gis.stackexchange.com, which covers the availability of services for solving your problem.
Loc2country 是一个基于 Golang 的工具,它返回给定位置坐标 (lat/隆)。它以微秒级响应。它使用 geohash 来生成国家/地区地图。
geohash 数据是使用 georaptor 生成的。
我们在该工具中使用第 6 级的 geohash,即大小为 1.2km x 600m 的盒子。
Loc2country is a Golang based tool that returns the ISO alpha-3 country code for given location coordinates (lat/lon). It responds in microseconds. It uses a geohash to country map.
The geohash data is generated using georaptor.
We use geohash at level 6 for this tool, i.e., boxes of size 1.2km x 600m.
您可以使用 Google Geocoding API
Bellow 是返回地址、城市、州和国家/地区的 php 函数
You can use Google Geocoding API
Bellow is php function that returns Adress, City, State and Country
如果您使用 Google 的 Places API,则可以通过以下方式使用 Javascript 从地点对象获取国家/地区和城市:
If you are using Google's Places API, this is how you can get country and city from the place object using Javascript:
您可以通过以下方式实现:https://www.weatherapi.com/ 免费。
我的演示是在 React 中一步步进行的,但是你可以用任何你想要的方式来完成,关键是这个 Weather API,它接受 LON 和 LAT 作为字符串来生成城市和天气信息 -> https://api.weatherapi.com/v1/forecast.json?key=YOUR_KEY&q=LATITUDE,LONGITUDE&days=1&aqi=no&alerts=n
注意:您将生成自己的密钥,通过注册,
为此您将需要 3 个状态:
第一:使用此代码请求用户访问“位置”(这将有一个弹出窗口)并将状态设置为纬度和经度。
其次使用 https://www.weatherapi.com/ API 获取城市和其他信息(基于 Lat) Lon
API 如下所示: https://api.weatherapi.com/v1/forecast.json?key=3e5e13fac8354c818de152831211305&q=53.3498053,-6.2603097&days=1&aqi=no&alerts=n
API 及说明: https://api.weatherapi.com/v1/forecast.json?key=3e5e13fac8354c818de152831211305&q=LATITUDE,LONGITUDE&days=1&aqi=no&alerts=n
现在调用这个具有纬度和经度的 API,用于获取位置数据,包括城市。我使用 useEffect 作为触发器,因此一旦获得有关 Latitude 的信息,我就会使用 axios 调用 api,并将 City state 设置为来自 api 对象的内容。
API 的结果:
这是我的 YouTube 频道的视频,您可以在其中观看演示:https://youtu.be /gxcG8V3Fpbk
You can do it with: https://www.weatherapi.com/ its FREE.
My demo is in React and step by step, but you can do it in any way you want, the key is this Weather API, that accepts LON and LAT as a string to produce city and weather info -> https://api.weatherapi.com/v1/forecast.json?key=YOUR_KEY&q=LATITUDE,LONGITUDE&days=1&aqi=no&alerts=n
Note: you will to generate YOUR OWN KEY, by signing up
You will need 3 states for this:
First: Request access to 'location' from user (this will have a POP-UP), by using this code and set state to Latitude and Longitude.
Second use https://www.weatherapi.com/ API to get City and other intel, based on Lat and Lon
API looks like this: https://api.weatherapi.com/v1/forecast.json?key=3e5e13fac8354c818de152831211305&q=53.3498053,-6.2603097&days=1&aqi=no&alerts=n
API with explanation: https://api.weatherapi.com/v1/forecast.json?key=3e5e13fac8354c818de152831211305&q=LATITUDE,LONGITUDE&days=1&aqi=no&alerts=n
Now call this API with latitude and longitude to get location data, including city. I am using useEffect as a trigger, so as soon as I get info on Latitude I call the api using axios and set City state to what ever comes out of the api object.
RESULT from API:
Here is video to my youtube channel, where you can see a demo of this: https://youtu.be/gxcG8V3Fpbk
请检查以下答案。这对我有用
Please check the below answer. It works for me
尽量减少库的数量。
获取在其网站上使用 api 的密钥,然后在 http 请求中获取结果:
Minimize the amount of libraries.
Get a key to use the api at their website and just get the result in a http request:
更新:我的解决方案不够准确,有时它会返回边界旁边的不正确的国家/地区坐标,或者当坐标位于海边时,它不会返回任何国家/地区。最后我选择了付费的 MapBox 反向地理编码 API。对 URL
https://api.mapbox.com/geocoding/v5/mapbox.places/,.json?access_token=
的请求返回 geojson包含位置数据 - 地名、地区、国家。原始答案:
从 https://www.naturalearthdata.com/downloads/ 下载国家/地区 (我建议使用 1:10m 以获得更好的精度),从中生成 GeoJSON,并使用某种算法来检测给定坐标是否在国家/地区多边形内。
我使用以下步骤生成 GeoJSON 文件:
conda install -c conda-forge gdal
(使用提升的管理员权限,更多信息请参阅https://anaconda.org/conda-forge/gdal)C:\ProgramData\Anaconda3\Library\bin\ogr2ogr.exe -f GeoJSON -t_srs crs:84 data.geo.json ne_10m_admin_0_countries.shp
这将生成
data.geo.json
大约有 24MB。您也可以在此处下载。C#:
测试:
JS:您可以使用 https://github.com/vkurchatkin/which-country 并替换不太准确的 https://github.com/vkurchatkin/which-country/blob/master/lib/data.geo.json 由生成的。不过我没有测试过。
Update: My solution was not accurate enough, sometimes it returned incorrect country for coordinates right next to a border, or it would not return any country when the coordinates were at a seashore for example. At the end I went for paid MapBox reverse geocoding API. A request to URL
https://api.mapbox.com/geocoding/v5/mapbox.places/<longitude>,<latitude>.json?access_token=<access token>
returns geojson with location data - place name, region, country.Original answer:
Download countries from https://www.naturalearthdata.com/downloads/ (I recommend using 1:10m for better accuracy), generate GeoJSON from it, and use some algorithm to detect if given coordinates are within a country polygon(s).
I used these steps to generate GeoJSON file:
conda install -c conda-forge gdal
(use elevated admin rights, more info on https://anaconda.org/conda-forge/gdal)setx PROJ_LIB C:\ProgramData\Anaconda3\Library\share\proj\
C:\ProgramData\Anaconda3\Library\bin\ogr2ogr.exe -f GeoJSON -t_srs crs:84 data.geo.json ne_10m_admin_0_countries.shp
This will generate
data.geo.json
which has around 24MB. You can alternatively download it here.C#:
Tests:
JS: you can use https://github.com/vkurchatkin/which-country and replace the not so accurate https://github.com/vkurchatkin/which-country/blob/master/lib/data.geo.json by the generated one. I didn't test it though.
根据之前的答案,我采用了使用 GeoNames 的想法。我需要一种方法来根据汽车仪表板的坐标获取近似城市,这需要大量请求(至少每秒 1 个),并且不应依赖于可靠的互联网连接。因为数据库可以离线使用,所以这对我来说是最好的解决方案。
在实现方面,我选择了 redis 作为后端数据库,并选择 GEO* 命令来进行查找。
基本上,您下载一个国家/地区信息文件(例如: http://download.geonames.org /export/dump/BE.zip),这是一个文本文件,其中的字段由制表符分隔。架构可在 http://download.geonames.org/export/dump 自述文件中找到。
对于每一行,您添加地理空间条目:
然后使用简单的
GEORADIUS
您可以根据您的位置获取最近的条目。这并不完全准确,只能给出近似值,但对于离线免费服务来说,这已经相当不错了。显然,一切都取决于您的需求。我编写了一个脚本,下载整个欧洲并将其添加到 redis 中,并添加一些示例代码来查询数据库,全部用 python 编写,您可以在这里找到代码: https://github.com/maxux/magibux-dashboard/blob/master/backend/tools/geonames.py
对于整个欧洲(不包括俄罗斯,它不是我的需要,而且相当大),内存数据库约为 30 MB。
Based on previous answers, I took the idea using GeoNames. I needed a way to get approximation city based from a coordinate for Car Dashboard, this require lot of requests (at least 1 per second) and should not rely on a reliable internet connection. Because database is available offline, that was the best solution for me.
Implementation wise, I selected redis for the backend database and GEO* commands to do lookup.
Basically, you download a country info file (eg: http://download.geonames.org/export/dump/BE.zip) which is a text file with fields separated by tabs. Schema is available on http://download.geonames.org/export/dump readme.
For each line, you add the geospatial entry:
Then using a simple
GEORADIUS
you can get the nearest entry based on your location. This is not fully accurate and this only give an approximation but for an offline free service, this is pretty good. All depend of your needs obviously.I wrote a script which download whole Europe and add it into redis and add some sample code to query database, all in python, you can found code here: https://github.com/maxux/magibux-dashboard/blob/master/backend/tools/geonames.py
For whole Europe (excluding Russia which was not needed for my needs and is quite large), in memory database is ~30 MB.
下载 https://download.geonames.org/export/dump/cities15000.zip< /a> 文件,并尝试类似这样的操作(避免 Web 服务并具有合理的性能):
此逻辑使用非常简单的空间索引来避免对城市进行完整扫描。但这意味着垂直距离超过 70 英里的城市可能会从最近的城市搜索中跳过。
Download the https://download.geonames.org/export/dump/cities15000.zip file, and try something like this (avoids webservices and has reasonable performance):
This logic uses a very simple spatial index to avoid a complete scan of the cities. But it means that cities more than 70 miles vertically might be skipped from the nearest city search.