dev-master
9999999-devSymfony GMap bundle
MIT
The Requires
- php >=5.3.2
google maps gmap
Symfony GMap bundle
In progress : This bundle is under active developement, new features will be regularly appended., (*1)
It works : This bundle is under Test Driven Developement control, so every features mentioned below will really work., (*2)
Current implementation : Just include (for now) the following services :, (*3)
Current branch : Full refactoring with a better approach., (*4)
In order to use this bundle you have to install it, and then optionally test it (dont forget to post issues on github!)., (*5)
Add the sources to your bundles directory - from the root directory of your project, paste the following command :, (*6)
git submodule add git@github.com:alephnullplex/GMapBundle.git vendor/bundles/GMapBundle
Register the bundle in your with the auto loader and AppKernel:, (*7)
// app/autoload.php in registerNamespaces() 'GMapBundle' => __DIR__.'/../vendor/bundles', // app/AppKernel.php in registerBundles() new GMapBundle\GMapBundle(),
Register the bundle config in your app config file - for example, add the following minimalist code in your app/config/config.yml
file :, (*8)
g_map: config: ~
You're done !, (*9)
Register the routing. Add the following code in your app/config/routing_dev.yml
file - this will
register the routes in your dev region only :, (*10)
_Tests_GMapBundle: resource: "@GMapBundle/Resources/config/routing.yml"
Run the tests with phpunit, the following tests are available :, (*11)
phpunit --c app/ vendor/bundles/GMapBundle/Tests/ServiceTests.php phpunit --c app/ vendor/bundles/GMapBundle/Tests/PolylineEncoderTests.php phpunit --c app/ vendor/bundles/GMapBundle/Tests/GeocoderTests.php phpunit --c app/ vendor/bundles/GMapBundle/Tests/ElevationTests.php
This bundle offers a new service accessible from your controller. To access the service, simply use the following code :, (*12)
$gmap = $this->get('gmap');
These services are subject to a query limit of 2,500 geolocation requests per day (for each service, I suppose)., (*13)
Each webservice takes these 4 common parameters (cant be overrided by request) :, (*14)
url : ~ // the webservice URL format : 'json' // the internal response format, for now only 'json' is implemented formatter : ~ // the result formatter class for one result collection : ~ // the collection result fomatter class
Each webservice also comes with his own set of parameters wich can be setted : - in the config (the defaults values) ; - for each request (as an associative array)., (*15)
Formatter
objectCollection
objectCollection
objects are Iterable and, for convenience, Formatter
objects are Iterable too.Collection
object give you Formatter
objects.Formatter
and Collection
objects with appropriate methods.This service is used to get latitude / longitude point from an address and vice-versa. It can also be used to normalize and parse addresses. For more informations, look at the Google webservice documentation., (*16)
Simple to use, here are the examples :, (*17)
// get the geocode object from an address (wich is really dirty) $home = $gmap->geocode('12 rU hipOLYte lBAs 75009 fR'); // get the geocode object from a latitude / longitude array $home = $gmap->geocode(array(48.8772535, 2.3397612)); // get the geocode object from a latitude / longitude string $home = $gmap->geocode('48.8772535, 2.3397612');
As second parameter, you can provide an associative array of options :, (*18)
More explanations of these options in Google's documentation., (*19)
Many requests get you a collection, you can filter the address by type :, (*20)
// get the 'street_address' type (the more precise) // just one address of this type, you get a `Formatter` object $home = $home->filter('street_address');
The geocode method returns a Geocode object wich comes with several methods to get the data you want., (*21)
Get the position of an address, (*22)
$home = $this->get('gmap')->geocode('12 Rue Hippolyte Lebas 75009 France')->filter('street_address'); // get the latitude $lat = $home->getLat(); // 48.8772535 // get the longitude $lng = $home->getLng(); // 2.3397612 // get both as string $str = $home->getLatLng(); // '48.8772535, 2.3397612' // get both as an array $arr = $home->getLatLng(true); // array(48.8772535, 2.3397612)
Get an address from a position, (*23)
$home = $this->get('gmap')->geocode('48.8772535, 2.3397612')->filter('street_address'); // get the *normalized* address as string $str = $home->getAddress(); // 12 Rue Hippolyte Lebas, 75009 Paris, France
Normalize an address, (*24)
// a dirty address is inputed by a user $home = $this->get('gmap')->geocode('12 rU hipOLYte lBAs 75009 fR')->filter('street_address'); // get the *normalized* address $str = $home->getAddress(); // 12 Rue Hippolyte Lebas, 75009 Paris, France
Address components, (*25)
$home = $this->get('gmap')->geocode('12 Rue Hippolyte Lebas 75009 France')->filter('street_address'); // get the number $str = $home->getAddressComponent('street_number'); // '12' // get the city $str = $home->getAddressComponent('locality'); // 'Paris' // get the region (for France) $str = $home->getAddressComponent('administrative_area_level_1'); // 'Ile-de-France' // get the zip code $str = $home->getAddressComponent('postal_code'); // '75009' // get a sublocality $str = $home->getAddressComponent('sublocality'); // '9Ăšme Arrondissement Paris'
And so on ... full list of components available in Google's documentation. If a component has several values, it returns an array., (*26)
In addition, getAddressComponent method take a 2nd boolean argument, wich setted to true get you the short name. For example :, (*27)
// get the country short name $str = $home->getAddressComponent('country', true); // 'FR' // get the region (for France) short name $str = $home->getAddressComponent('administrative_area_level_1', true); // 'IDF'
3 more options come with this webservice, an example in YAML format :, (*28)
gmap.options: geocoder: // the 4 common parameters bounds : ~ # default bounds option for each requests (see above) region : ~ # default region option for each requests (see above) language : ~ # default language option for each requests (see above) sensor : false // whether the browser has GPS functionalities
This service is used to get the elevations from a list of pointd (lat/lng).
The Formatter
result object has 4 methods :, (*29)
getLat()
: returns the latitudegetLng()
: returns the longitudegetLatLng()
: returns an arraygetElevation()
: returns the elevation (float), (*30)
$elevations = $this->get('gmap')->elevation($points);, (*31)
There is just one parameter :, (*32)
gmap.options: elevation: // the 4 common parameters sensor : false // whether the browser has GPS functionalities
This service is used in background by other services to compress a list of lat/lng points. It's accessible in the controller with th following method :, (*33)
$encoded = $this->get('gmap')->encodePolyline($polyline);
Where $polyline is an array of points, each points is an array of 2 values : lat and lng ; and $encoded is an associative array with 2 keys : 'points' (the encoded points) and 'levels' (the encoded levels). Setting up your config :, (*34)
Some options are available, here is an exemple with the YML format :, (*35)
gmap.options: polyline_encoder: accuracy: 5 # should not be changed ! levels: 4 # the levels number (called numLevels in the Google's documentation) zoom: 3 # the zoom factor endpoints: true # indicate if endpoints should be forced
You can read more about in this stuff in Google's documentation., (*36)
Symfony GMap bundle
MIT
google maps gmap