phil / geolocation-bundle
一个用于处理地理位置的Symfony2 Bundle。为您的实体添加地理位置。添加地址实体。添加用户地理位置。为您的项目添加计算地理距离的doctrine函数。
Requires
- php: >=5.4.21
- doctrine/doctrine-bundle: ~1.2
- doctrine/doctrine-fixtures-bundle: ~2.2
- doctrine/orm: ~2.2,>=2.2.3
- guzzle/guzzle: 3.*
- league/geotools: ~0.3
- sensio/framework-extra-bundle: ~3.0
- stof/doctrine-extensions-bundle: ~1.1
- symfony/symfony: ~2.4
- twig/extensions: ~1.0
- willdurand/geocoder: ~2.4
- willdurand/geocoder-bundle: ~3.0
Requires (Dev)
- satooshi/php-coveralls: ~0.6
- symfony/browser-kit: ~2.1
- symfony/finder: ~2.1
README
未更新...正在为Symfony 3开发新版本。已分为不同的Bundle
一个用于处理地理位置的Symfony2 Bundle。为您的实体添加地理位置。添加地址实体。添加用户地理位置。为您的项目添加计算地理距离的doctrine函数。
1 安装
1.1 Composer
```
"require": {
....
"phil/geolocation-bundle": "~0.2"
},
```
或
```
php composer.phar require phil/geolocation-bundle
```
1.2 启用Bundle
```php
// app/AppKernel.php
public function registerBundles()
{
$bundles = array(
// ...
new Phil\GeolocationBundle\PhilGeolocationBundle(),
);
}
```
1.3 注册所需的Doctrine函数
您需要手动注册您想使用的Doctrine函数。有关详细信息,请参阅https://symfony.com.cn/doc/current/cookbook/doctrine/custom_dql_functions.html。
```yaml
# in app/config/config.yml
doctrine:
dbal:
types:
point: Phil\GeolocationBundle\ORM\PointType
connections:
default:
mapping_types: { point: point }
orm:
dql:
numeric_functions:
POINTSTR: Phil\GeolocationBundle\ORM\PointStr
DISTANCE: Phil\GeolocationBundle\ORM\Distance
```
1.4 更新您的模式
doctrine:schema:update
2 使用方法
2.1 实体
您可以为其中一个实体创建一个关联,或者可以使用其中一个特质。您可以使用接口,需要AddressInterface和GeocodeInterface。AddressableEntity和GeocodableEntity是这些接口的特质。
地址与任何其他实体无关。
2.2 格式化器
```php
$formatted = $this->get("padam87.address.formatter")->format($address);
标志
```php
use Phil\GeolocationBundle\Service\FormatterService;
...
$formatted = $this->get("phil.geolocation.address.formatter")->format($address, FormatterService::FLAG_NOBR);
```
可用标志
FLAG_NOBR
不会添加换行符
FLAG_HTML
以html格式输出地址
FLAG_NOCASE
不会应用大小写更改
2.3 Twig扩展
{{ address|address()|raw }}
这将输出格式化的地址,默认添加FLAG_HTML
2.4 地理编码
```php
use Phil\GeolocationBundle\Entity\Address;
...
$address = new Address();
```
监听器将处理其他部分;)。如果您使用自己的实体(不使用特质),则需要这两个接口:AddressInterface和GeocodeInterface。
2.5 导入地理数据
这可能是最烦人的步骤:存储所有地理数据及其地理位置,用于您需要的国家。幸运的是,获取这些信息并将其导入数据库并不难。
对于邮政编码按国家(来自geonames.org的数据)
前往http://download.geonames.org/export/zip/并下载您需要的国家的存档。让我们以DE.zip
为例。解压包含的DE.txt
文件,例如到/tmp/DE.txt
。
创建一个 fixture 类(在一个独立的文件夹中,以便只加载这个文件夹)并继承提供的基类
// MyCompany/MyBundle/Doctrine/Fixtures/PhilGeolocation/MyGeonamesPostalCodeData.php namespace MyCompany\MyBundle\Doctrine\Fixtures\PhilGeolocation; use Phil\GeolocationBundle\DataFixtures\ORM\loadPostalCodeData; use Doctrine\Common\Persistence\ObjectManager; class MyGeonamesPostalCodeData extends loadPostalCodeData { public function load(ObjectManager $manager) { $this->clearPostalCodesTable($manager); $this->addEntries($manager, '/tmp/DE.txt', loadPostalCodeData::FORMAT_GEONAMES); } }
对于按国家邮政编码(您的数据:示例在 DataFixtures/data 中可用)
创建一个 fixture 类(在一个独立的文件夹中,以便只加载这个文件夹)并继承提供的基类
// MyCompany/MyBundle/Doctrine/Fixtures/PhilGeolocation/MyGeonamesPostalCodeData.php namespace MyCompany\MyBundle\Doctrine\Fixtures; use Phil\GeolocationBundle\DataFixtures\ORM\loadPostalCodeData; use Doctrine\Common\Persistence\ObjectManager; class MyGeonamesPostalCodeData extends loadPostalCodeData { public function load(ObjectManager $manager) { $this->clearPostalCodesTable($manager); $this->addEntries($manager, '/tmp/postalcode.csv', loadPostalCodeData::FORMAT_CSV); } }
现在,备份您的数据库!如果出现问题,不要责怪其他人导致数据丢失。然后导入 fixture,并记得使用 --append
参数。
# in a shell php app/console doctrine:fixtures:load --append --fixtures="src/MyCompany/MyBundle/DataFixtures/ORM"
sf doctrine:fixtures:load --append --fixtures="src/Phil/TestBundle/DataFixtures/ORM"
3 感谢
一些想法来自 padam87/address-bundle craue/geo-bundle
4 TODO
还有很多要做的事
- 完成所有测试
- 更多文档
- 清理代码
- 为地理位置添加所有 require 实体
请随时发送一些修正和建议。