Geocoder
========
[![Build
Status](https://travis-ci.org/geocoder-php/Geocoder.svg?branch=master)](http://travis-ci.org/geocoder-php/Geocoder)
[![Total
Downloads](https://poser.pugx.org/willdurand/Geocoder/downloads.png)](https://packagist.org/packages/willdurand/Geocoder)
[![Latest Stable
Version](https://poser.pugx.org/willdurand/Geocoder/v/stable.png)](https://packagist.org/packages/willdurand/Geocoder)
![PHP7 ready](https://img.shields.io/badge/PHP7-ready-green.svg)
> **Important:** You are browsing the documentation of Geocoder **4.x** (not
> released yet).
>
> Documentation for version **3.x** is available here: [Geocoder 3.x
> documentation](https://github.com/geocoder-php/Geocoder/blob/3.x/README.md).
>
> Documentation for version **2.x** is available here: [Geocoder 2.x
> documentation](https://github.com/geocoder-php/Geocoder/blob/2.x/README.md).
---
**Geocoder** is a PHP library which helps you build geo-aware applications by
providing a powerful abstraction layer for geocoding manipulations.
* [Installation](#installation)
* [Usage](#usage)
- [Address & AddressCollection](#address--addresscollection)
- [The ProviderAggregator](#the-provideraggregator)
- [TimedGeocoder](#timedgeocoder)
- [HTTP Adapters](#http-adapters)
- [Providers](#providers)
- [Address-based Providers](#address-based-providers)
- [ArcGISOnline](#arcgisonline)
- [GeoIP2](#geoip2)
- [GoogleMaps](#googlemaps)
- [GoogleMapsBusiness](#googlemapsbusiness)
- [Mapzen](#mapzen)
- [MaxMindBinary](#maxmindbinary)
- [Nominatim](#nominatim)
- [TomTom](#tomtom)
- [Yandex](#yandex)
- [IP-based Providers](#ip-based-providers)
- [Locale Aware Providers](#locale-aware-providers)
- [The Chain Provider](#the-chain-provider)
- [Dumpers](#dumpers)
- [GPS eXchange Format (GPX)](#gps-exchange-format-gpx)
- [GeoJSON](#geojson)
- [GeoArray](#geoarray)
- [Keyhole Markup Language (KML)](#keyhole-markup-language-kml)
- [Well-Known Binary (WKB)](#well-known-binary-wkb)
- [Well-Known Text (WKT)](#well-known-text-wkt)
- [Formatters](#formatters)
* [Extending Things](#extending-things)
* [Versioning](#versioning)
* [Cookbook](#cookbook)
Installation
------------
The recommended way to install Geocoder is through
[Composer](http://getcomposer.org):
```
$ composer require willdurand/geocoder
```
Usage
-----
[Geocoder](https://github.com/geocoder-php/Geocoder) and its companion
[Geocoder Extra](https://github.com/geocoder-php/geocoder-extra) provides a lot
of [providers](#providers).
Choose the one that fits your need first. Let's say the `GoogleMaps` one is what
you were looking for, so let's see how to use it. In the code snippet below,
`curl` has been chosen as [HTTP layer](#http-adapters) but it is up to you
since each HTTP-based provider implements
[PSR-7](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-7-http-message.md).
```php
$adapter = new \Http\Adapter\Guzzle6\Client();
$geocoder = new \Geocoder\Provider\GoogleMaps($adapter);
$geocoder->geocode(...);
$geocoder->reverse(...);
```
The `Geocoder` interface, which all providers implement, exposes two main
methods:
* `geocode($streetOrIpAddress)`
* `reverse($latitude, $longitude)`
It also contains methods to control the number of results:
* `limit($limit)`
* `getLimit()`
### Address & AddressCollection
Both `geocode()` and `reverse()` methods return a collection of `Address`
objects (`AddressCollection`), each providing the following API:
* `getCoordinates()` will return a `Coordinates` object (with `latitude` and
`longitude` properties);
* `getLatitude()` will return the `latitude` value;
* `getLongitude()` will return the `longitude` value;
* `getBounds()` will return an `Bounds` object (with `south`, `west`, `north`
and `east` properties);
* `getStreetNumber()` will return the `street number/house number` value;
* `getStreetName()` will return the `street name` value;
* `getLocality()` will return the `locality` or `city`;
* `getPostalCode()` will return the `postalCode` or `zipcode`;
* `getSubLocality()` will return the `city district`, or `sublocality`;
* `getAdminLevels()` will return an ordered collection (`AdminLevelCollection`)
of `AdminLevel` object (with `level`, `name` and `code` properties);
* `getCountry()` will return a `Country` object (with `name` and `code`
properties);
* `getCountryCode()` will return the ISO `country` code;
* `getTimezone()` will return the `timezone`.
The `AddressCollection` exposes the following methods:
* `count()` (this class implements `Countable`);
* `first()` retrieves the first `Address`;
* `slice($offset, $length = null)` returns `Address` objects between `$offset`
and `length`;
* `get($index)` fetches an `Address` using its `$index`;
* `all()` returns all `Address` objects;
* `getIterator()` (this class implements `IteratorAggregate`).
### The ProviderAggregator
The `ProviderAggregator` is used to register several providers so that you can
decide which provider to use later on.
``` php
<?php
$geocoder = new \Geocoder\ProviderAggregator();
$geocoder->registerProviders([
new \Geocoder\Provider\GoogleMaps(
$adapter, $locale, $region, $useSsl
),
new \Geocoder\Provider\GoogleMapsBusiness(
$adapter, '<CLIENT_ID>', '<PRIVATE_KEY>', $locale, $region, $useSsl
),
new \Geocoder\Provider\Yandex(
$adapter, $locale, $toponym
),
new \Geocoder\Provider\MaxMind(
$adapter, '<MAXMIND_API_KEY>', $service, $useSsl
),
new \Geocoder\Provider\ArcGISOnline(
$adapter, $sourceCountry, $useSsl
),
]);
$geocoder->registerProvider(
new \Geocoder\Provider\Nominatim(
$adapter, 'http://your.nominatim.server', $locale
)
);
$geocoder
->using('google_maps')
->geocode('...');
$geocoder
->limit(10)
->reverse($lat, $lng);
```
The `ProviderAggregator`'s API is fluent, meaning you can write:
``` php
<?php
$addresses = $geocoder
->registerProvider(new \My\Provider\Custom($adapter))
->using('custom')
->limit(10)
->geocode('68.145.37.34')
;
```
The `using()` method allows you to choose the `provider` to use by its name.
When you deal with multiple providers, you may want to choose one of them. The
default behavior is to use the first one but it can be annoying.
The `limit()` method allows you to configure the maximum number of results being
returned. Depending on the provider you may not get as many results as expected,
it is a maximum limit, not the expected number of results.
### TimedGeocoder
The `TimedGeocoder` class profiles each `geocode` and `reverse` call. So you can
easily figure out how many time/memory was spent for each geocoder/reverse call.
```php
// configure you geocoder object
$stopwatch = new \Symfony\Component\Stopwatch\Stopwatch();
$geocoder = new \Geocoder\TimedGeocoder($geocoder, $stopwatch);
$geocoder->geocode('Paris, France');
// Now you can debug your application
```
We use the [symfony/stopwatch](http://symfony.com/doc/current/components/stopwatch.html)
component under the hood. Which means, if you use the Symfony framework the
geocoder calls will appear in your timeline section in the Web Profiler.
### HTTP Adapters
In order to talk to geocoding APIs, you need HTTP adapters. While it was part of
the library in Geocoder 1.x and 2.x, Geocoder 3.x and upper now relies on the
[PSR-7
Standard](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-7-http-message.md)
which defines how HTTP message should be implemented. You can use any library to send HTTP messages
that implements [php-http/client-implementation](https://packagist.org/providers/php-http/client-implementation).
To use Guzzle 6 you should run the follwing command:
```
$ composer require php-http/guzzle6-adapter
```
### Providers
Providers perform the geocoding black magic for you (talking to the APIs,
fetching results, dealing with erro
没有合适的资源?快使用搜索试试~ 我知道了~
Geocoder:一个帮助我们建立地理感知应用程序的php类库
共72个文件
php:63个
md:5个
yml:1个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 131 浏览量
2022-04-24
17:08:59
上传
评论
收藏 94KB ZIP 举报
温馨提示
Geocoder可以帮助你建立地理感知应用程序。它提供了一个抽象层,地理编码操作。该库分为两个部分:HttpAdapter和供应商,是真正的可扩展性
资源推荐
资源详情
资源评论
收起资源包目录
geocoder-php-Geocoder-v3.3.0-67-g33a37a5.zip (72个子文件)
geocoder-php-Geocoder-v3.3.0-67-g33a37a5
geocoder-php-Geocoder-33a37a5
composer.json 2KB
.travis.yml 699B
docs
cookbook
http-client.md 1KB
cache.md 1KB
LICENSE 1KB
CONTRIBUTING.md 1KB
src
Provider
IpInfoDb.php 5KB
Mapzen.php 6KB
GeoIPs.php 7KB
GoogleMaps.php 10KB
FreeGeoIp.php 3KB
OpenCage.php 7KB
Geonames.php 5KB
IpAddressGeocoder.php 420B
AbstractProvider.php 2KB
AbstractHttpProvider.php 2KB
BingMaps.php 5KB
Chain.php 3KB
Geoip.php 3KB
GeoIP2.php 4KB
MapQuest.php 5KB
MaxMind.php 15KB
LocaleAwareProvider.php 679B
Provider.php 551B
MaxMindBinary.php 4KB
Nominatim.php 6KB
HostIp.php 2KB
GeoPlugin.php 3KB
Yandex.php 5KB
ArcGISOnline.php 5KB
TomTom.php 5KB
LocaleTrait.php 884B
Location.php 2KB
ProviderAggregator.php 3KB
Model
AdminLevel.php 1KB
Bounds.php 2KB
AddressFactory.php 4KB
AdminLevelCollection.php 3KB
Address.php 5KB
AddressCollection.php 2KB
Coordinates.php 1KB
Country.php 1KB
Collection.php 686B
Dumper
GeoArray.php 1KB
Gpx.php 2KB
Dumper.php 580B
GeoJson.php 1KB
Wkt.php 725B
Kml.php 1KB
Wkb.php 720B
Formatter
StringFormatter.php 2KB
Geocoder.php 1KB
Assert.php 1KB
TimedGeocoder.php 2KB
Adapter
GeoIP2Adapter.php 3KB
Exception
QuotaExceeded.php 372B
ZeroResults.php 671B
InvalidServerResponse.php 642B
InvalidCredentials.php 389B
Exception.php 336B
FunctionNotFound.php 736B
InvalidArgument.php 393B
ChainZeroResults.php 938B
CollectionIsEmpty.php 113B
UnexpectedValue.php 387B
ProviderNotRegistered.php 901B
HttpError.php 368B
ExtensionNotLoaded.php 377B
UnsupportedOperation.php 388B
.gitignore 73B
CHANGELOG.md 14KB
README.md 21KB
共 72 条
- 1
资源评论
reg183
- 粉丝: 1811
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功