This version of the bundle requires Symfony >= 3.4
PUGXGeoFormBundle uses willdurand/geocoder as a backend geocoding service.
- Download PUGXGeoFormBundle
- Enable the Bundle
- Form
Using composer
Run
$ composer require php-http/guzzle6-adapter pugx/geo-form-bundle
You can require a different HTTP client from Guzzle. Any PSR-7 compatible client should be OK.
If you don't use Flex, you need to enable the bundle in the kernel:
<?php
// e.g. app/AppKernel.php
public function registerBundles()
{
$bundles = [
// ...
new PUGX\GeoFormBundle\PUGXGeoFormBundle(),
];
}
Add a pugx_geo_form
entry in your config.yml, specifying your regione
(note: this is just a bias, not a geographic constraint - see for example
google maps api docs)
You can also customize the names for "latitude" and "longitude", or omit that options and get default ones.
If you installed an HTTP client different from Guzzle, you can specify it in the last two options.
pugx_geo_form:
region: IT
# the following options are not mandatory (here are shown with their default value)
names:
lat: latitude
lng: longitude
http_adapter: Http\Adapter\Guzzle6\Client
Example of configuration with Buzz instead of Guzzle:
pugx_geo_form:
# [...]
http_adapter: Http\Adapter\Buzz\Client
In order to use geolocalization in a form, you should:
- add two hidden "latitude" and "longitude" fields (feel free to use different names, see previous point);
- add the
geo_code
option in the form default options; - specify which field has geocodable information with the
geo_code_field
option.
Here's an example:
<?php
namespace App\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type;
use Symfony\Component\Form\FormBuilderInterface;
class SearchFormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('address', Type\TextType::class, ['required' => true, 'geo_code_field' => true]);
$builder->add('longitude', Type\HiddenType::class, ['required' => false]);
$builder->add('latitude', Type\HiddenType::class, ['required' => false]);
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'geo_code' => true,
]);
}
}
Before this form is bound, latitude and longitude fields will be populated with a call to your preferred geolocalization API (Google Maps by default).
If you want to concatenate more than one field (i.e. composing the full address of a venue through address, city, country),
you can specify the geo_code_field
option for more than one field:
<?php
namespace App\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type;
use Symfony\Component\Form\FormBuilderInterface;
class SearchFormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('address', TypeTextType::class, ['required' => true, 'geo_code_field' => true]);
$builder->add('city', TypeTextType::class, ['required' => true, 'geo_code_field' => true]);
$builder->add('country', TypeTextType::class, ['required' => true, 'geo_code_field' => true]);
$builder->add('longitude', TypeHiddenType::class, ['required' => false]);
$builder->add('latitude', TypeHiddenType::class, ['required' => false]);
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'geo_code' => true,
]);
}
}
In addition, a little Javascript snippet is included in the bundle for integrating Google Places Autocomplete. In order to use it, you have to add some classes to you form fields:
<?php
namespace AppBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type;
use Symfony\Component\Form\FormBuilderInterface;
class SearchFormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('address', TypeTextType::class, [
'required' => true,
'attr' => [
'class' => 'pugx-geocode'
)
]);
$builder->add('longitude', TypeHiddenType::class, [
'required' => false,
'attr' => [
'class' => 'pugx-geocode-longitude'
)
]);
$builder->add('latitude', TypeHiddenType::class, [
'required' => false,
'attr' => [
'class' => 'pugx-geocode-latitude'
)
]);
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'geo_code' => true,
]);
}
}
As you can see, pugx-geocode
is used for the meaningful geocoding field (the address), while pugx-geocode-latitude
and pugx-geocode-longitude
are used for easily identifying latitude and longitude fields.
Once you have the classes in place, you can include the snippet in your twig (jQuery is required):
<script src="//maps.googleapis.com/maps/api/js?key=YOURKEY&libraries=places&language={{ app.request.locale }}"></script>
<script src="{{ asset('/bundles/pugxgeoform/js/google_maps_autocomplete.js') }}"></script>
This time the latitude and longitude fields will be populated via javascript and the server will not make an additional call the the geolocalization API.
⚠ You'll need to get an API key from Google and put it in the above template, replacing
YOURKEY
string.