cethyworks/google-place-autocomplete-bundle

提供一种Google Place Autocomplete类型,这是最简约、最不显眼的实现方式。

安装数: 10,669

依赖项: 0

建议者: 0

安全性: 0

星标: 10

关注者: 2

分支: 10

开放问题: 4

类型:symfony-bundle

v5.1 2019-11-26 16:06 UTC

This package is not auto-updated.

Last update: 2024-09-25 14:22:17 UTC


README

提供一种Google Place Autocomplete类型,这是最简约、最不显眼的实现方式。

CircleCI

版本

适用于symfony >= 3.4

使用最新版本或 >= v3.3

适用于symfony < 3.4

使用 2.2 或更低版本

安装

1. 使用Composer require命令

$ composer require cethyworks/google-place-autocomplete-bundle

2. 注册bundle

// AppKernel.php
class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = [
            // ...
            new Cethyworks\ContentInjectorBundle\CethyworksContentInjectorBundle(),
            new Cethyworks\GooglePlaceAutocompleteBundle\CethyworksGooglePlaceAutocompleteBundle(),
        ];
        // ...

如何使用

1. 可选:添加一个config/packages/cethyworks_google_place_autocomplete.yaml文件,包含

cethyworks_google_place_autocomplete:
    google:
        api_key: 'your_api_key'

2. 在你的表单中使用Cethyworks\GooglePlaceAutocompleteBundle\Form\SimpleGooglePlaceAutocompleteType;

3. 完成!

从Google Place API获取更多数据

如果你需要从地点API结果中获取更多信息,你可以在表单中使用ComplexGooglePlaceAutocompleteType代替。

与返回简单的string不同,此类型返回一个Cethyworks\GooglePlaceAutocompleteBundle\Model\Place对象。

为了持久化它,该bundle提供了doctrine映射,你可以在实体中使用它,如下所示

use Cethyworks\GooglePlaceAutocompleteBundle\Model\Place;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Table(name="dummy_entity")
 * @ORM\Entity()
 */
class DummyEntity
{
    /**
     * @var Place
     *
     * @ORM\Embedded(class="Cethyworks\GooglePlaceAutocompleteBundle\Model\Place")
     *
     * @Assert\NotBlank()
     */
    private $locationAddress;
    
    // ...
}

工作原理

当使用SimpleGooglePlaceAutocompleteTypeComplexGooglePlaceAutocompleteType时,它会自动注册2个InjectorCommands(一个用于库调用,一个用于输入控件),将必要的javascript代码(包含输入id和google api_key)注入到Response中。

附加信息

Cethyworks\ContentInjectorBundle

Google Place Autocomplete 文档

路线图

  • 更新README.md,添加将地点转换为其他实体的示例
  • 更新地点实体以检索更多数据