phil/geolocation-bundle

此包已废弃,不再维护。未建议替代包。

一个用于处理地理位置的Symfony2 Bundle。为您的实体添加地理位置。添加地址实体。添加用户地理位置。为您的项目添加计算地理距离的doctrine函数。

安装: 794

依赖: 0

建议者: 0

安全: 0

星标: 12

关注者: 1

分支: 3

开放问题: 1

类型:symfony-bundle

0.5.2 2015-01-09 17:48 UTC

This package is auto-updated.

Last update: 2021-02-10 15:47:35 UTC


README

未更新...正在为Symfony 3开发新版本。已分为不同的Bundle

一个用于处理地理位置的Symfony2 Bundle。为您的实体添加地理位置。添加地址实体。添加用户地理位置。为您的项目添加计算地理距离的doctrine函数。

Latest Stable Version Total Downloads License Build Status Coverage Status SensioLabsInsight

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 实体

请随时发送一些修正和建议。