使⽤geocoder_你在哪?使⽤GeocoderPHP实现地理位置
使⽤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; . 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 .
或从获得其中⼀个档案。
地理编码 (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 or based clients. Additionally you can add PHP clients like , , and the . You’ll nee
d to add those to your project yourself if you want to use them.
要使⽤Geocoder,您⾸先需要使⽤HttpAdapter在Web服务上触发请求。 HttpAdaper是⼀个Web客户端,实际上PhpGeocoder具有5种不同的风格。 您可以使⽤始终可⽤的基于或的客户端。 另外,您可以添加PHP客户端,例如 , , 和 。 如果要使⽤它们,则需要⾃⼰将它们添加到项⽬中。
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: , , , , , , , , , , (Denmark only), (Canada only), (USA only), (USA & Canada), (France only), (China only),
地理编码器: , , , , , , , , , , (仅丹麦), (仅加拿⼤), (仅美国) , (美国和加拿⼤), (仅法国), (仅中国),
IP geocoders: , , , , , , ,
IP地址解析器: , , , , , , ,
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.
phpjson格式化输出
⼤多数可以免费使⽤,有些需要您注册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 () coordinate. If your input is in another projection you could use the PHP version of 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( )坐标点。 如果您的输⼊在另⼀个投影中,则可以使⽤PHP版本对其进⾏转换。 ⼀旦有了地理编码器的实例,语法就⾮常简单。 您需要记住的唯⼀⼀件事是:⾸先是纬度,然后是经度(如果您还不⼩⼼切换了纬度和经度,请举⼿……)。确保您的地址解析器⽀持反向地址解析,因为并⾮全部都⽀持。 我将对荷兰的地理中⼼进⾏地理编码:
$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 if you’re interesed in the results for your IP.
结果还是Geocoded结果。 我不会显⽰⼀个⽰例,因为您会失望的。 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 dep
ending 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块中。 地理编码器可以根据您使⽤的地理编码器的输出向您抛出多个异常。 ⼤多数都是很明显的,但是可以通过添加⼀些捕获条件来很好地处理:

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。