Developed with love by KnpLabs Hire us for your project!
42

GoogleGeolocationBundle

by dsyph3r

Symfony 2 Bundle for Google Geocoding API

Google Geolocation Bundle for Symfony2

Overview

A Symfony 2 bundle for the
Google Geocoding API
service.

Requirements

  • PHP 5.3+

Dependancies

Installation

  1. Add bundle and Buzz library dependancy to vendor dir:
* Using vendors script

    Add the following to the `deps` file:

        [Buzz]
            git=git://github.com/kriswallsmith/Buzz.git
            target=/Buzz

        [GoogleGeolocationBundle]
            git=git://github.com/dsyph3r/GoogleGeolocationBundle.git
            target=/bundles/Google/GeolocationBundle

    Run the vendors script:

        $ php bin/vendors install

* Using git submodules:

        $ git submodule add git://github.com/kriswallsmith/Buzz.git vendor/Buzz
        $ git submodule add git://github.com/dsyph3r/GoogleGeolocationBundle.git vendor/bundles/Google/GeolocationBundle
  1. Add the Google and Network namespace to your autoloader:

    // app/autoload.php
    $loader->registerNamespaces(array(
        // ..
        'Buzz'      => __DIR__.'/../vendor/Buzz/lib',
        'Google'    => __DIR__.'/../vendor/bundles',
    ));
    
  2. Add bundle to application kernel:

    // app/ApplicationKernel.php
    public function registerBundles()
    {
        return array(
            // ...
            new Google\GeolocationBundle\GoogleGeolocationBundle(),
        );
    }
    

Usage

The bundle provides a service available via the google_geolocation.geolocation_api
identifier.

To retrieve the service from the container:

$geo = $this->get('google_geolocation.geolocation_api');

Basic usage

To find an address:

$geolocationApi = $this->get('google_geolocation.geolocation_api');
$location = $geolocationApi->locateAddress("Wales, UK");

if ($location->getMatches() > 0)
{
    $matches = json_decode($location->getResult(), true);

    // Get address components [city, country, postcode, etc] for 1st match
    $components = $location->getAddressComponents(0);

    // Get LatLng for 2nd match
    $latLng = $location->getLatLng(1);
}

Additional Usage

The service can be used in 2 ways:

  1. Without the caching layer (default)
  2. With the caching layer

Without the caching layer

The service is configured by default to not use the caching layer.

With the caching layer

The caching layer provides a cache of previous requests made to the Google Geocoding API
to reduce the number of requests required for the service. It also allows limiting of
requests made to the service. Both of these features are useful if you heavily use
the Google Geocoding API.

To enable the use of the caching layer you need to configure the service. Update
the config at app/config/config.yml with the following:

services:
    google_geolocation.geolocation_api:
        class:      %google_geolocation.geolocation_api.class%
        calls:
            - [ setEntityManager, [ @doctrine.orm.entity_manager ] ]
            - [ setDailyLimit, [ %google_geolocation.geolocation_api.daily_limit% ] ]
            - [ setCacheLifetime, [ %google_geolocation.geolocation_api.cache_lifetime% ] ]

The defaults for the paramaters are as follows:

google_geolocation.geolocation_api.daily_limit: 2500    # Daily requests
google_geolocation.geolocation_api.cache_lifetime: 24   # Hours

Clearing the cache

The cache should be cleaned periodically to comply with the Google terms
of service (see below)

Run the following task:

$ php app/console google:geolocation:clean-cache

TODO

  • Explore use of Zend Caching layer instead of DB - Zend caching provides a SQLite option which would be useful for some projects.

Google Terms of Service

Please respect the
terms of service (TOS)
specified by Google for use of the Geocoding API.

The Geocoding API service must only be used in conjunction with a Google Map.
The caching feature provided by the bundle is for temporary caching use in order
to enhance the user experience when using Geocoding (This is permitted by the
TOS). You should run the clean cache task periodically to clean up the cache
values. The lifetime of each Geocoding result can be set via the paramater
google_geolocation.geolocation_api.cache_lifetime. By default this is set to 24 hours

Copyright (c) 2011 d.syph.3r@gmail.com

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is furnished
to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
google_geolocation:   []
  • Merge pull request #14 from mapado/master
    By dsyph3r, 1 year ago
  • Merge pull request #17 from dotlabel-chris/patch-1
    By dsyph3r, 1 year ago
  • Update GeolocationApi.php
    By dotlabel-chris, 1 year ago
  • Revert "tmp fix waiting pull request validation"
    By jdeniau, 1 year ago
  • tmp fix waiting pull request validation
    By dallegoet, 1 year ago
  • * fix unserialization when unknown address component type
    By jdeniau, 1 year ago
  • Merge pull request #13 from jbeurel/master
    By dsyph3r, 1 year ago
  • [hotfix] fixes typo on Doctrine lifecycle event
    By jbeurel, 1 year ago
  • Update composer.json for kriswallsmith/buzz, version now >=0.9
    By dsyph3r, 1 year ago
  • Merge pull request #12 from RockBee/master
    By dsyph3r, 1 year ago
  • Changing PreUpdate annotation.
    By hugomn, 1 year ago
  • Merge pull request #6 from chrisixion/master
    By dsyph3r, 2 years ago
  • Fixed 'getResultArray()'. Typo in return variable and changed undeclared variable $location to $this
    By chrisixion, 2 years ago
  • Merge pull request #5 from IamPersistent/master
    By dsyph3r, 2 years ago
  • add composer.json
    By IamPersistent, 2 years ago
  • Merge pull request #4 from b3nelof0n/patch-1
    By dsyph3r, 2 years ago
  • Update README.md
    By b3nelof0n, 2 years ago
  • Merge pull request #3 from ntgd/patch-1
    By dsyph3r, 2 years ago
  • Wrong namespace for Exception class fix
    By ntgd, 2 years ago
  • Adds Lisence
    By dsyph3r, 2 years ago
  • Correction
    By dsyph3r, 2 years ago
  • Readme formatting
    By dsyph3r, 2 years ago
  • Addes Support for Buzz library
    By dsyph3r, 2 years ago
  • Readme updates
    By dsyph3r, 2 years ago
  • Updates to readme
    By dsyph3r, 2 years ago
  • Updates to readme
    By dsyph3r, 2 years ago
  • updates to service
    By dsyph3r, 3 years ago
  • added function to get latlng component from result
    By dsyph3r, 3 years ago
  • added function to get address components from result
    By dsyph3r, 3 years ago
  • formatting in readme
    By dsyph3r, 3 years ago