sircamp / google-map-form-type-builder

用于谷歌地图和Symfony的表单构建器

1.0 2015-09-01 13:57 UTC

This package is not auto-updated.

Last update: 2024-09-28 18:00:07 UTC


README

使用谷歌地图在表单上设置经纬度、街道、邮编和城市值。地图允许您通过您已填写的数据或当前位置按钮搜索地点。当在地图上放置或拖动标记时,经纬度字段将更新。

安装

此包与Symfony >= 2.1兼容。将以下内容添加到您的composer.json文件中:

    "sircamp/google-map-form-type-bundle": "dev-master"

在您的app/AppKernel.php中注册此包,通过在包数组中添加以下行来实现:

    new Sircamp\GoogleMapFormTypeBundle\SircampGoogleMapFormTypeBundle(),

请记住,在您的配置文件(config.yml或其他)中的assetic字段上添加此包

将OhGoogleMapFormTypeBundle添加到assetic

# app/config/config.yml
# Assetic Configuration
assetic:
    bundles:        [ 'SircampGoogleMapFormTypeBundle' ]

用法

此包包含一个名为GoogleMapType的新FormType,可以在您的表单中使用,如下所示:

    $builder->add('address', 'sircamp_google_maps');

在您的模型中,您将需要处理经纬度、街道、城市、邮编数组

    
    use Symfony\Component\Validator\Constraints as Assert;
    use Sircamp\GoogleMapFormTypeBundle\Validator\Constraints as OhAssert;

    class MyEntity
    {
        // ... include your latitude,longitude,street,city,postacode fields here

   /**
    * @Assert\NotBlank()
    * @SircampAssert\Address()
    */
    public function getAddress()
    {
       return array('address' => array(
       'lat' => $this->latitude,
       'lng' => $this->longitude, 
       'street' => $this->street,
       'city' => $this->city, 
       'postcode'=>$this->postcode));
    }

    public function setAddress($address)
    {
       // die(var_dump($address,$address['address']['lat']));
       $this
          ->setCity($address['address']['city'])
          ->setPostcode($address['address']['postcode'])
          ->setStreet($address['address']['street'])
          ->setLatitude($address['address']['lat'])
          ->setLongitude($address['address']['lng']);
       
       return $this;
    }

  }

Include the twig template on your config.yalm for the layout. 
```yaml
    # your config.yml
    twig:
        form:
            resources:
                # This uses the default - you can put your own one here
                - 'SircampGoogleMapFormTypeBundle:Form:fields.html.twig'

如果您打算在模板中覆盖某些元素,可以通过扩展默认的google_maps.html.twig来实现。此示例在新的地图位置被选中时向javascript添加了一个回调函数。

致谢

此库基于以下原始作品

  • Ollie Harridge (ollietb)