php 将纬度/经度转换为城市名称?(反向定位)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3392160/
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
Converting latitude/longitude into city name? (reverse geolocating)
提问by Walker
I'm working on a job board in Codeigniter PHP + jQuery where employers enter their location and we use Google Maps API to plot it. While this has had awesome usability results, the problem is when we try to display these locations to job seekers they are muddled and hard to visually discern (they read like this: "02905, 2 miles away","171 John St., 4 miles away", "Providence, RI, 10 miles away".
我正在 Codeigniter PHP + jQuery 中的工作板工作,雇主在其中输入他们的位置,我们使用 Google Maps API 来绘制它。虽然这有很棒的可用性结果,但问题是当我们尝试向求职者显示这些位置时,他们会感到混乱且难以在视觉上辨别(他们读作:“02905,2 英里外”,“171 John St., 4英里远”,“普罗维登斯,罗德岛,10 英里远”。
I want to be able to reverse geolocate from a longitude/latitude to a set level (ideally, city name) so that in the search results I can have the locations be listed like this: "Providence, RI, 10 miles away", "Providence, RI, 5 miles away", "Cranston, RI, 16 miles away".
我希望能够将地理定位从经度/纬度反向到设定级别(理想情况下,城市名称),以便在搜索结果中我可以像这样列出位置:“普罗维登斯,RI,10 英里远”,“罗德岛普罗维登斯,5 英里外”,“罗德岛克兰斯顿,16 英里外”。
Is there a way to reverse geocode to city? I see many examples for ways to reverse geocode to nearest addressable location, but no way to control the level.
有没有办法将地理编码反向到城市?我看到了许多将地理编码反向到最近可寻址位置的方法的示例,但无法控制级别。
Hope that's clear! Feel free to comment with any questions you need clarification on.
希望这很清楚!如有任何需要澄清的问题,请随时发表评论。
采纳答案by Ossama
The Google Geocoder, which supports reverse geocodingreturns address_components. You just need to pull out the component of the address tagged with localityand politicalif you just want the city name. If you want states as well they are tagged with administrative_area_level_1
支持反向地理编码的 Google Geocoder返回address_components。你只需要拉出地址的成分标记locality和political如果你只想城市名称。如果你也想要状态,它们会被标记为administrative_area_level_1
回答by Maksym Kozlenko
FREE SOURCES (e.g. Creative Commons)
免费资源(例如知识共享)
Geonames
地名
If you're looking for free (as freedom) sources, you can use Geonames API findNearbyPlaceName.
如果您正在寻找免费(作为自由)来源,您可以使用 Geonames API findNearbyPlaceName。
For example the following returns nearest Placename:
例如,以下返回最近的地名:
http://api.geonames.org/findNearbyPlaceName?lat=47.3&lng=9&username=demo
http://api.geonames.org/findNearbyPlaceName?lat=47.3&lng=9&username=demo
More information is available here
更多信息请点击这里
http://www.geonames.org/export/web-services.html#findNearbyPlaceName
http://www.geonames.org/export/web-services.html#findNearbyPlaceName
Freebase
游离碱
Instead of single point it takes bounded box. Call sample:
它不是单点,而是有界框。调用示例:
Geocoder.ca
地理编码器
Reverse geocoding for North American addresses.
北美地址的反向地理编码。
Info: http://geocoder.ca/
信息:http: //geocoder.ca/
示例调用:http: //geocoder.ca/?latt=40.70771000786733&longt=-74.0109443664550&reverse=1&allna=1&geoit=xml&corner=1&jsonp=1&callback=test
Map Quest API
地图任务 API
Using Open Street Map nominatim service:
使用开放街道地图提名服务:
http://open.mapquestapi.com/nominatim/v1/search?q=50.4,30.4&format=json
http://open.mapquestapi.com/nominatim/v1/search?q=50.4,30.4&format=json
NON FREE SOURCES
非免费资源
Yahoo PlaceFinder
雅虎地方搜索
Documentation http://developer.yahoo.com/geo/placefinder/
文档http://developer.yahoo.com/geo/placefinder/
Sample call:
示例调用:
http://where.yahooapis.com/geocode?q=38.898717,+-77.035974&gflags=R&appid=[yourappidhere]
http://where.yahooapis.com/geocode?q=38.898717,+-77.035974&gflags=R&appid=[yourappidhere]
Google Maps API
谷歌地图 API
Documentation:
文档:
http://code.google.com/apis/maps/documentation/geocoding/#ReverseGeocoding
http://code.google.com/apis/maps/documentation/geocoding/#ReverseGeocoding
回答by zebediah49
Can't you just do it do full address, and then drop the street and number?
你不能只做完整的地址,然后去掉街道和号码吗?
I would say it'd be more valuable to use the full address, so as to get a more accurate distance (even if you only display the city,state).
我会说使用完整地址更有价值,以获得更准确的距离(即使您只显示城市,州)。
EDIT: If you do do it that way, at least it should be returned in a standard form, which you can then easily regex the data you want out of.
编辑:如果你这样做,至少它应该以标准形式返回,然后你可以轻松地正则表达式你想要的数据。
回答by James D
Reverse geocoding services (such as Google, Yahoo, and GeoNames) will work well for this purpose with a few caveats:
反向地理编码服务(例如 Google、Yahoo 和 GeoNames)可以很好地用于此目的,但有一些注意事项:
- Although I can't say for sure how the commercial services work, last time I checked GeoNames uses a closest point of interest strategy to determine the city. This can be a problem if your query point is somewhat near a border between two cities and the nearest entry in the GeoNames database is on the other side of that border. This actually happens a fair bit in my experience, so this is not just some technicality.
- Also, if you are concerned about user experience, any web API solution adds latency and reduces reliability.
- 虽然我不能确定商业服务是如何运作的,但上次我检查 GeoNames 时使用最近的兴趣点策略来确定城市。如果您的查询点有点靠近两个城市之间的边界,并且 GeoNames 数据库中最近的条目在该边界的另一侧,则这可能是一个问题。根据我的经验,这实际上发生了很多,所以这不仅仅是一些技术问题。
- 此外,如果您担心用户体验,任何 Web API 解决方案都会增加延迟并降低可靠性。
I run a web site that offers a number of commercial geographic web APIs, and sells the Java libraries that we built to back those APIs. One of them (which only has US coverage) uses a proper polygonal map of the cities and towns in the US to return the result that contains your query point. It is accurate and fast, so if this is a mission critical component in your system, I suggest taking a look at our web site:
我经营一个网站,提供许多商业地理网络 API,并出售我们为支持这些 API 而构建的 Java 库。其中之一(仅包含美国覆盖范围)使用美国城镇的适当多边形地图来返回包含您的查询点的结果。它准确且快速,因此如果这是您系统中的关键任务组件,我建议您查看我们的网站:
The page that is specific to looking up cities from lat/lon is:
特定于从纬度/经度查找城市的页面是:
回答by turgos
I was searching for an API to convert lat/long to city. Seems like this was the original question on this thread as well.
我正在寻找一个 API 来将纬度/经度转换为城市。似乎这也是该线程上的原始问题。
I saw the data "http://download.geonames.org/export/dump/" shared on another stackoverflow thread Given the lat/long coordinates, how can we find out the city/country?, and implemented my own Web Service.
我看到在另一个 stackoverflow 线程上共享的数据“ http://download.geonames.org/export/dump/”给定纬度/经度坐标,我们如何找出城市/国家?,并实现了我自己的 Web 服务。
You can see it running at http://scatter-otl.rhcloud.com/location?lat=36&long=-78.9Just change the latitude and longitude for your locations.
您可以在http://scatter-otl.rhcloud.com/location?lat=36&long=-78.9 上看到它正在运行,只需更改您所在位置的纬度和经度即可。
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...
它部署在 OpenShift(RedHat 平台)上。长时间空闲后的首次调用可能需要一些时间,但通常性能令人满意。随意使用此服务...
Also, you can find the project source at https://github.com/turgos/Location.
此外,您可以在https://github.com/turgos/Location找到项目源。
回答by Kurt Van den Branden
Html5 geolocation requires user permission. If you don't want this behaviour, you can go for an external provider like https://geoip-db.com. They offer a free geolocation service based on ip-addresses.
Html5 地理定位需要用户许可。如果您不想要这种行为,您可以选择外部提供商,例如https://geoip-db.com。他们提供基于 ip 地址的免费地理定位服务。
- JSON:https: //geoip-db.com/json/
- JSONP:https://geoip-db.com/json/geoip.php ?jsonp =callback
Try this example:
试试这个例子:
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no"/>
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<title>Geo City Locator</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
</head>
<body >
<div>Country: <span id="country"></span></div>
<div>State: <span id="state"></spa></div>
<div>City: <span id="city"></span></div>
<div>Latitude: <span id="latitude"></span></div>
<div>Longitude: <span id="longitude"></span></div>
<div>IP: <span id="ip"></span></div>
<script>
$.getJSON('https://geoip-db.com/json/geoip.php?jsonp=?')
.done (function(location) {
$('#country').html(location.country_name);
$('#state').html(location.state);
$('#city').html(location.city);
$('#latitude').html(location.latitude);
$('#longitude').html(location.longitude);
$('#ip').html(location.IPv4);
});
</script>
</body>
</html>

