使用geocoder

The beauty of SitePoint, to me, is that you can get inspired to try something or be told about some cool project out there. The internet is simply too big for one person to scout out on their own. Geocoder was one of those for me. I had never heard about it and came across it on the authors Trello board.

对我而言,SitePoint的优点在于,您可以激发灵感去尝试一些事情,或者被告知那里有一些很棒的项目。 互联网实在太大了,无法让一个人自己侦察。 Geocoder对我来说就是其中之一。 我从未听说过它,并在作者Trello董事会上碰到过它。

I love working with maps and geographic information and I use (reverse) geocoding heavily for a project I did for a client; CableTracks. We actually use a paid service for this although not for everything. The paid results hold much more information than you get from free services. I found out that Geocoder PHP actually is what I was missing for the integration of various services that we use.

我喜欢使用地图和地理信息,并且在为客户执行的项目中大量使用(反向)地理编码; 电缆轨道 。 我们实际上为此使用了付费服务,尽管不是全部。 与免费服务相比,付费结果包含的信息更多。 我发现实际上Geo Geocoder PHP是我所使用的各种服务集成所缺少的。

Geocoder PHP provides: “an abstraction layer for geocoding manipulations”. The library is split into three parts: an HttpAdapter for doing requests, several geocoding Providers and various Formatter/Dumpers to do output formatting.

Geocoder PHP提供:“用于地理编码操作的抽象层”。 该库分为三个部分:一个用于执行请求的HttpAdapter,多个地理编码提供程序以及用于格式化输出的各种Formatter / Dumper。

安装 (Installation)

Installation of Geocoder is most easily done using composer. Add the following to your composer.json:

使用composer最容易完成Geocoder的安装。 将以下内容添加到您的composer.json中

{
"require": {
"willdurand/geocoder": "@stable"
}
}

Or get one of the archives from the Geocoder PHP website.

或从Geocoder PHP网站获得其中一个档案。

地理编码 (GeoCoding)

We’ll first take a look at geocoding adresses. The input for this is usually a street address, but can also be a neighborhood, area, place, state or country. The geocoded output should have a good pointer to where on the globe you should look for what you used as input. The result, of course, depends on the quality of the used geocoder.

我们首先来看一下地址解析地址。 输入的内容通常是街道地址,但也可以是邻里,地区,地点,州或国家。 经过地理编码的输出应该很好地指示您应该在地球上的何处寻找用作输入的内容。 结果当然取决于所用地理编码器的质量。

To use Geocoder you’ll first need an HttpAdapter to fire the requests at the web service. The HttpAdaper is a web client that actually comes in 5 different flavors with PhpGeocoder. You can use the always available CUrl or Socket based clients. Additionally you can add PHP clients like Buzz Browser, Guzzle PHP HTTP Client, Zend HTTP and the GeoIP2 webservice API. You’ll need to add those to your project yourself if you want to use them.

要使用Geocoder,您首先需要使用HttpAdapter在Web服务上触发请求。 HttpAdaper是一个Web客户端,实际上PhpGeocoder具有5种不同的风格。 您可以使用始终可用的基于CUrl或Socket的客户端。 另外,您可以添加PHP客户端,例如Buzz浏览器 , Guzzle PHP HTTP客户端 , Zend HTTP和GeoIP2 Web服务API 。 如果要使用它们,则需要自己将它们添加到项目中。

With the HTTP adapter you can then use a geocoder to do the work. The list of supported geocoders is long, very long. They can be divided into a two groups;

然后,您可以使用HTTP适配器使用地址解析器来完成工作。 受支持的地址解析器列表非常长。 它们可以分为两组。

  • Geographical coders: Google Maps, OpenStreetMap, Bing Maps, MapQuest, Yandex, GeoPlugin, Geonames, ArcGIS Online, Google Maps for Business, TomTom, OIORest (Denmark only), Geocoder.ca (Canada only), Geocoder.us (USA only), DataScienceToolkit (USA & Canada), IGN OpenLS (France only), Baidu (China only),

    地理编码器: Google Maps , OpenStreetMap , Bing Maps , MapQuest , Yandex , GeoPlugin , Geonames , ArcGIS Online , Google Maps for Business , TomTom , OIORest (仅丹麦), Geocoder.ca (仅加拿大), Geocoder.us (仅美国) , DataScienceToolkit (美国和加拿大), IGN OpenLS (仅法国), 百度 (仅中国),

  • IP geocoders: FreeGeoIp, HostIp, IpInfoDB, Geoip, GeoIPs, MaxMind web service, IpGeoBase, DataScienceToolkit

    IP地址解析器: FreeGeoIp , HostIp , IpInfoDB , Geoip , GeoIP , MaxMind Web服务 , IpGeoBase , DataScienceToolkit

Most can be used for free and some require you to register for an API-key. Most will have some sort of rate or daily limit on the amount of queries that you can shoot their way. Google, for instance, will allow you to do 2500 requests/day. The quality of the geocoding services varies. Some support specific countries, and usually will be better for those countries. But this is not what we’ll be evaluating here. It is something you do want to test out for yourself, because results will depend on the place you’re geocoding.

大多数可以免费使用,有些需要您注册API密钥。 大多数网站都会对您可以查询的查询量设置某种速率或每日限制。 举例来说,Google将允许您每天进行2500个请求。 地理编码服务的质量各不相同。 有些支持特定国家,通常对那些国家会更好。 但这不是我们将在此处评估的内容。 您确实想对它进行测试,因为结果将取决于您要进行地理编码的位置。

Enough about all the possibilities, let’s get to work. We’ll start with a simple example first.

所有可能性已足够,让我们开始工作。 我们首先从一个简单的示例开始。

$lookfor = 'Laan van Meerdervoort, Den Haag, Nederland';
$adapter  = new \Geocoder\HttpAdapter\CurlHttpAdapter();
$geocoder = new \Geocoder\Geocoder();
$geocoder->registerProvider(new \Geocoder\Provider\GoogleMapsProvider($adapter));
$result = $geocoder->geocode($lookfor);

Simple, right? This is what you’ll get as result:

简单吧? 结果就是这样:

Geocoder\Result\Geocoded#1
(
[*:latitude] => 52.0739343
[*:longitude] => 4.2636776
[*:bounds] => array
(
'south' => 52.0610866
'west' => 4.2262026
'north' => 52.0868446
'east' => 4.3008719
)
[*:streetNumber] => null
[*:streetName] => 'Laan Van Meerdervoort'
[*:cityDistrict] => null
[*:city] => 'The Hague'
[*:zipcode] => null
[*:county] => 'The Hague'
[*:countyCode] => 'THE HAGUE'
[*:region] => 'South Holland'
[*:regionCode] => 'ZH'
[*:country] => 'The Netherlands'
[*:countryCode] => 'NL'
[*:timezone] => null
)

This is actually the most beautiful part of Geocoder PHP; which ever geocoder you use, what ever direction you’re geocoding in (normal/reverse), you’ll always get the same result set. That’s invaluable if you’d ever think about changing the used geocoder.

这实际上是Geocoder PHP中最漂亮的部分; 无论您使用哪种地理编码器,无论您使用哪种地理编码方向(正向/反向),都将始终获得相同的结果集。 如果您要考虑更改使用的地理编码器,那么这是无价的。

About my address choice; the Laan van Meerdervoort is one of the longest streets in the Netherlands (~5800m long). I left out a house number to get the full street. The difference in the latitude/longitude and the bounds make this obvious now. These bounds, in this case, hold the bounding box of the entire street. It’s very practical that you get both in your result; the center of the street and the bounds.

关于我的地址选择; Laan van Meerdervoort是荷兰最长的街道之一(长约5800m)。 我留下了门牌号码,以获得整条街。 现在,纬度/经度和范围的差异变得显而易见。 在这种情况下,这些边界将保持整条街道的边界框。 两者兼得是很实际的。 街道的中心和边界。

Most geocoders can be asked to be locale or region sensitive and Geocoder has this nicely integrated with a LocaleAwareProviderInterface. Say you’re Russian, for instance, and you need to go to the Peace Palace which is on the above street you’d use it like this:

可以要求大多数地址解析器对区域设置或区域敏感,并且地址解析器将其与LocaleAwareProviderInterface很好地集成在一起。 举例来说,假设您是俄罗斯人,则需要前往位于以上街道上的和平宫,您可以像这样使用它:

$geocoder->registerProvider(new \Geocoder\Provider\GoogleMapsProvider($adapter, 'Ru'));
$result = $geocoder->geocode('Laan van Medevoort 7, Den Haag, Nederland');

And get:

得到:

Geocoder\Result\Geocoded#1
(
[*:latitude] => 52.0739343
[*:longitude] => 4.2636776
[*:bounds] => array
(
'south' => 52.0610866
'west' => 4.2262026
'north' => 52.0868446
'east' => 4.3008719
)
[*:streetNumber] => 7
[*:streetName] => 'Laan Van Meerdervoort'
[*:cityDistrict] => null
[*:city] => 'Гаага'
[*:zipcode] => null
[*:county] => 'Гаага'
[*:countyCode] => 'ГААГА'
[*:region] => 'Южная Голландия'
[*:regionCode] => 'ZH'
[*:country] => 'Нидерланды'
[*:countryCode] => 'NL'
[*:timezone] => null
)

Of course you want to get the best results out of your geocoding. If you’re in the US you might want to use a coder that specifically supports the US and another for outside the US. The beauty is that Geocoder supports the chaining of geocoders. You can then throw your request at a set of coders and the first valid result will be returned. This is great because otherwise you’d have to try/catch a whole set of coders until you got a result. An example of this:

当然,您希望从地理编码中获得最佳结果。 如果您在美国,则可能要使用专门支持美国和美国以外的编码器。 优点是Geocoder支持地址解析器的链接。 然后,您可以向一组编码器提出请求,然后将返回第一个有效结果。 这很棒,因为否则您将不得不尝试/捕获整套编码器,直到获得结果为止。 一个例子:

$adapter  = new \Geocoder\HttpAdapter\CurlHttpAdapter();
$geocoder = new \Geocoder\Geocoder();
$chain    = new \Geocoder\Provider\ChainProvider([
new \Geocoder\Provider\OpenStreetMapProvider($adapter),
new \Geocoder\Provider\GoogleMapsProvider($adapter),
new \Geocoder\Provider\BingMapsProvider($adapter, $bingApiKey),
]);
$geocoder->registerProvider($chain);

Do note that Geocoder will return the first valid result. That might not be the best you can get! So take good care in the sequence you chain your coders together!

请注意,Geocoder将返回第一个有效结果。 那可能不是最好的! 因此,请按顺序将编码器链接在一起,请当心!

反向地理编码 (Reverse geocoding)

Reverse geocoding is when you’ve got a point on the globe, usually a GPS (WGS84) coordinate. If your input is in another projection you could use the PHP version of Proj4 to transform it. The syntax, once you’ve got an instance of a geocoder, again is quite simple. The only thing you’ll need to remember is that it’s latitude first, then longitude (hands up if you also keep switching latitude and longitude accidentally…). Make sure your geocoder supports reverse geocoding because not all do. I’ll geocode the geographic center of the Netherlands:

反向地理编码是指地球上通常有一个GPS( WGS84 )坐标点。 如果您的输入在另一个投影中,则可以使用Proj4PHP版本对其进行转换。 一旦有了地理编码器的实例,语法就非常简单。 您需要记住的唯一一件事是:首先是纬度,然后是经度(如果您还不小心切换了纬度和经度,请举手……)。 确保您的地址解析器支持反向地址解析,因为并非全部都支持。 我将对荷兰的地理中心进行地理编码:

$result = $geocoder->reverse(52.155247, 5.387452);

Which leads to a geocoded result like this.

这样会导致经过地理编码的结果。

Geocoder\Result\Geocoded#1
(
[*:latitude] => 52.1551992
[*:longitude] => 5.3872474
[*:bounds] => array
(
'south' => 52.1551992
'west' => 5.3872474
'north' => 52.1551992
'east' => 5.3872474
)
[*:streetNumber] => '30'
[*:streetName] => 'Krankeledenstraat'
[*:cityDistrict] => 'Stadskern'
[*:city] => 'Amersfoort'
[*:zipcode] => '3811 BN'
[*:county] => 'Amersfoort'
[*:countyCode] => 'AMERSFOORT'
[*:region] => 'Utrecht'
[*:regionCode] => 'UT'
[*:country] => 'The Netherlands'
[*:countryCode] => 'NL'
[*:timezone] => null
)

对IP地址进行地址解析 (Geocoding IP addresses)

I’ve personally used geocoding on IP addresses to determine what language to show a website in (combined with browser accepts of course). You feed the IP address of your user, usually from $_SERVER['REMOTE_ADDR'], to the geocoder and get a result. Something to consider is whether or not your server supports IPv6 and is able to serve over IPv6. If so, then you’ll need to use a service that supports geocoding IPv6 because not all do.

我个人曾使用IP地址地理编码来确定以哪种语言显示网站(当然,结合浏览器接受)。 您通常将用户的IP地址从$_SERVER['REMOTE_ADDR']到地址解析器并获得结果。 需要考虑的是您的服务器是否支持IPv6并能够通过IPv6服务。 如果是这样,那么您将需要使用支持对IPv6进行地理编码的服务,因为并非所有人都可以。

There are several IP geocoders available, and using them is as simple as using the others:

有几种IP地理编码器可用,使用它们与使用其他地理编码器一样简单:

$adapter  = new \Geocoder\HttpAdapter\CurlHttpAdapter();
$geocoder = new \Geocoder\Geocoder();
$result   = $geocoder->registerProvider( new \Geocoder\Provider\FreeGeoIpProvider($adapter) )
->geocode($_SERVER['REMOTE_ADDR']);

The result, again, is a Geocoded result. I won’t show an example because you’ll be disappointed. IP geocoders results vary a lot in quality. Most often you’ll only get a country and nothing else. I’ve set up some examples for Geocoding IP addresses if you’re interesed in the results for your IP.

结果还是Geocoded结果。 我不会显示一个示例,因为您会失望的。 IP地址解析器的结果质量有很大不同。 通常,您只会得到一个国家,而没有其他任何东西。 如果您对IP结果感兴趣,我将为IP地址地理编码设置一些示例 。

Another thing to note in the last example is that you should always nest your geocoding attempts with Geocoder PHP in a try/catch block. The geocoder is able to throw several exceptions at you depending on the output of the geocoder you’re using. Most are quite obvious but can be handled nicely by adding several catch conditions:

在最后一个示例中要注意的另一件事是,您应始终将地理编码尝试与Geocoder PHP嵌套在try / catch块中。 地理编码器可以根据您使用的地理编码器的输出向您抛出多个异常。 大多数都是很明显的,但是可以通过添加一些捕获条件来很好地处理:

try {
$result = $geocoder->registerProvider( new \Geocoder\Provider\FreeGeoIpProvider($adapter) )
->geocode($_SERVER['REMOTE_ADDR']);
} catch (NoResultException $e) {
$result = "No result was returned by the geocoder";
} catch (QuotaExceededException $e) {
$result = "We met our daily quota";
} catch (Exception $e) {
$result = "Error: " . $e->getMessage();
}

输出格式 (Output formatting)

The last thing I want to touch on is the output formatting. Geocoder has several output formatters that allow you to easily integrate its result into your application. There are two types of output; a string Formatter that has a sprintf-like interface and Dumpers which allow for a language specific output.

我最后要谈的是输出格式。 Geocoder具有多个输出格式化程序,可让您轻松将其结果集成到您的应用程序中。 有两种类型的输出; 一个字符串Formatter ,具有sprintf的接口和Dumpers ,它们允许特定于语言的输出。

The supported output dumpers are XML like GPS eXchange Format (GPX), Keyhole Markup Language (KML) for interfacing with Google Earth, Well-Known Binary (WKB) and Well-Known Text (WKT) for your geospatial enabled databases and finally GeoJSON for easy JavaScript interfacing.

支持的输出转储程序是XML,例如GPS eXchange Format(GPX),用于与Google Earth交互的锁Kong标记语言(KML),用于已启用地理空间的数据库的已知二进制(WKB)和已知文本(WKT),最后是用于简单JavaScript接口。

For example; get a geocoded result and feed that to a formatter or dumper;

例如; 获取经过地理编码的结果,并将其提供给格式化程序或转储程序;

$adapter  = new \Geocoder\HttpAdapter\CurlHttpAdapter();
$GeoJSON  = new \Geocoder\Dumper\GeoJsonDumper;
$geocoder = new \Geocoder\Geocoder();
$geocoder->registerProvider( new \Geocoder\Provider\OpenStreetMapProvider($adapter) );
echo $GeoJSON->dump( $geocoder->reverse(52.155247, 5.387452) );

This will output a GeoJSON result which is quite usable with OpenLayers for instance:

例如,这将输出一个与OpenLayers相当有用的GeoJSON结果:

{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [
5.3872095538856,
52.1551691
]
},
"properties": {
"streetName": "Lieve Vrouwekerkhof",
"zipcode": "3811 AA",
"city": "Amersfoort",
"cityDistrict": "Amersfoort",
"region": "Utrecht",
"country": "Nederland",
"countryCode": "NL"
}
}

结论 (Conclusion)

Compliments have to be made about the great class structure in Geocoder. It is a true OOPHP library that makes good use of namespaces and interfaces. It is extremely easy to use. Extending it or building your own Geocoders for it should be very easy.

必须对Geocoder中出色的类结构进行表扬。 这是一个真正的OOPHP库,它充分利用了名称空间和接口。 它非常易于使用。 对其进行扩展或构建自己的地址解析器应该非常容易。

If straightforward geocoding is what you are looking for, then this library will work great for you. I personally see room for an even better Geocoder because there is more power available. It would be great if you were able to set conditions on the request you’re making to a geocoding chain to get the best result available from all coders. For instance: “at least return a street name” in the result set or “have a confidence level of > 75%” (some geocoders supply this information) or a “maximum address error of less than 500 meters” (something that could be calculated taking the difference of the output address and the input coordinates). Of course all this depends on individual geocoders’ possibilities, but simple filtering can certainly be done.

如果您正在寻找简单的地理编码,那么此库将非常适合您。 我个人认为,有更好的Geocoder的空间,因为有更多可用的功能。 如果您能够对地理编码链提出的请求中设置条件,以从所有编码器中获得最佳结果,那就太好了。 例如:结果集中的“至少返回街道名称”或“置信度大于75%”(某些地理编码器会提供此信息)或“最大地址误差小于500米”(可能是以输出地址和输入坐标之差计算得出)。 当然,所有这些都取决于各个地理编码器的可能性,但是可以简单地进行过滤。

If you’re interested in the geocoders themselves; I’ve set up a small demo to check out the results for various geocoders.

如果您对地址解析器本身感兴趣; 我已经建立了一个小型演示,以检查各种地理编码器的结果。

Did you ever work with geocoders, needed a library like this or perhaps work with it already? Please share your thoughts with us.

您是否曾经使用过地理编码器,是否需要像这样的库,或者也许已经在使用它? 请与我们分享您的想法。

翻译自: https://www.sitepoint.com/implementing-geolocation-geocoder-php/

使用geocoder

使用geocoder_你在哪? 使用Geocoder PHP实现地理位置相关推荐

  1. 通过高德api获取经纬度

    项目场景: 通过输入地理位置使用高德地图api获取经纬度 代码实现 1.引入 <script type="text/javascript"src="https:// ...

  2. Cesium:地球初始设置

    当我们新建了一个Viewer后,默认的场景支持鼠标交互: ①按住鼠标左键拖拽 - 让相机在数字地球平面平移. ②按住鼠标右键拖拽 - 放缩相机. ③鼠标滚轮滑动 - 放缩相机. ④按住鼠标中键拖拽 - ...

  3. Cesium基础教程

    一.概述 Cesium是国外一个基于javascript的地图引擎,支持3D.2D.2.5D形式的展示,可以自行绘制图形.高亮区域,并提供良好的触摸支持,并支持大多数的浏览器和移动端. Cesium ...

  4. 高德地图实现矩形围栏绘制和编辑

    demo-效果图 全部代码 <!DOCTYPE html> <html lang="en"> <head><meta charset=&q ...

  5. [html] Google map的地理编码javascript例子 -- Google map geocoder example

    代码 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.o ...

  6. 高德地图逆地理编码Geocoder的getlocation获取不到位置信息

    问题:原生定位接口定位失败,则status返回error事件或回调error信息,获取不到位置信息 产生原因:逆编码方法使用不了原因是使用的高德地图的应用方向web服务,而Geocoder属于web端 ...

  7. 【GIS开发】地理编码服务Geocoder(Python)

    文章目录 1.简介 2.百度 3.高德 4.google map 5.nominatim 5.1 overview 5.2 web api 5.3 python api (geopy) 6.geoco ...

  8. soso地图api接口地理解析geocoder检索示例----并在信息框显示经纬度

    api官网: http://api.map.soso.com/doc_v2/example.html?sample-geocoding-simple#8map 示例代码如下(保存为html打开可见效果 ...

  9. 地名Java_ArcGIS For JavaScropt Geocoder —— 搜索地名

    描述:此示例演示如何使用Geocoder的小工具,以简化的过程中,发现在地图上的位置.地理搜索框添加到应用程序html> ArcGIS API for JavaScript | Simple G ...

最新文章

  1. “是男人就下一百层”h5游戏全网最详细教学、全代码,js操作
  2. java.lang.NullPointerException空指针问题
  3. python的主函数如何书写_Python程序的入口在哪里?main函数的恰当写法
  4. php函数用粗体字显示,用来设置粗体字的属性是什么
  5. java 数组拼接_打印Java数组最优雅的方式是什么?这波操作闪瞎我
  6. beego——模板处理
  7. CCF 201503-1 图像旋转
  8. linux 下的文件属性,Linux 下文件属性介绍(示例代码)
  9. 谷歌修复十多个安卓高危漏洞
  10. 七月算法机器学习2 数理统计与参数估计2
  11. 软件工程基础 实验3《系统设计》
  12. 小程序 - 判断元素是否在页面的显示区域内 wx.createIntersectionObserver
  13. c语言注释不能嵌套什么意思,C语言嵌套注释问题
  14. Android 输入法键盘的隐藏和显示
  15. 关于hadoop运行成功但是无法链接web页面
  16. python分析每月销售数据_如何用Python分析销售数据
  17. Linux之git用法
  18. Deep Depth Completion of a Single RGB-D Image(深度像素补全)全文机器翻译
  19. CMS是Content Management System的缩写,意为内容管理系统。
  20. 用python去除SQL中的注释

热门文章

  1. 用JAVA写一个简单版的记事本
  2. OpenGL之弧度和角度的换算
  3. LeetCode Top100 Liked 题单(序号19~)
  4. 解读手机拍照的各个参数(VLOG)
  5. Sakurairo主题的美化篇,底部美化
  6. 【AGC】使用云调试优惠扣费、华为设备上触发崩溃、无法下载华为应用市场问题小结
  7. 机试算法讲解: 第10题 让我们来查询学生的信息
  8. MATLAB一个测心术的小游戏程序
  9. 【诸葛学堂】《考书大课-统编版必读常考名著名师精讲直播课》
  10. 写代码复现论文的几点小建议!