Skip to content

mortenson/geocoder

Repository files navigation

Geocoder

This is the Geocoder module for Drupal 7. This is a complete rewrite of the code, based on the Geocoder PHP library.

Features

  • Solid API based on Geocoder PHP library,
  • Geocode and Reverse Geocode using one or multiple providers,
  • Results can be dumped into multiple formats,
  • Submodule Geocoder Field: provides Drupal fields widgets and formatters, with even more options,
  • Submodule Geocoder Services: provides a Geocoding and reverse geocoding service through the contrib module Services,
  • File geocoding, Addressfield integration, caching enabled by default.

Requirements

Installation

  • Install the contrib module Service Container which is now a requirement.
  • Install the contrib module Composer Manager.
  • Read the documentation of Composer Manager to install dependencies. Basically with drush: drush dl composer-8.x, drush composer-json-rebuild, drush composer-manager install.
  • Enable the module.

This modules needs external libraries to work. Those libraries can be installed anywhere but to avoid messing with files and duplicates we're using the module Composer Manager. Composer manager will install all those libraries in 'sites/all/libraries/composer' and they will be available in Drupal without the need to include a the autoload.php file, it's automatically done for you. Please, read carefully the documentation, make sure you have all the requirements on your system and everything should be ok.

Links

API

Get a list of all available plugins

$plugins = \Drupal\geocoder\Geocoder::getPlugins();

Get a list of available Provider plugins only

$providers = \Drupal\geocoder\Geocoder::getPlugins('Provider');

Get a list of available Dumper plugins only

$dumpers = \Drupal\geocoder\Geocoder::getPlugins('Dumper');

Geocode a string

$plugins = array('geonames', 'googlemaps', 'bingmaps');
$address = '1600 Amphitheatre Parkway Mountain View, CA 94043';
$options = array(
  'geonames' => array(), // array of options
  'googlemaps' => array(), // array of options
  'bingmaps' => array(), // array of options
);

$addressCollection = \Drupal\geocoder\Geocoder::geocode($plugins, $address, $options);
// or
$addressCollection = geocode($plugins, $address, $options);

Reverse geocode coordinates

$plugins = array('freegeoip', 'geonames', 'googlemaps', 'bingmaps');
$address = '1600 Amphitheatre Parkway Mountain View, CA 94043';
$options = array(
  'freegeoip' => array(), // array of options
  'geonames' => array(), // array of options
  'googlemaps' => array(), // array of options
  'bingmaps' => array(), // array of options
);

$addressCollection = \Drupal\geocoder\Geocoder::reverse($plugins, $address, $options);
// or
$addressCollection = reverse($plugins, $address, $options);

Return format

Both Geocoder::geocode() and Geocoder::reverse() and both reverse() and geocode() returns the same object: Geocoder\Model\AddressCollection, which is itself composed of Geocoder\Model\Address.

You can transform those objects into arrays. Example:

$plugins = array('geonames', 'googlemaps', 'bingmaps');
$address = '1600 Amphitheatre Parkway Mountain View, CA 94043';
$options = array(
  'geonames' => array(), // array of options
  'googlemaps' => array(), // array of options
  'bingmaps' => array(), // array of options
);

$addressCollection = \Drupal\geocoder\Geocoder::geocode($plugins, $address, $options);
$address_array = $addressCollection->first()->toArray();

// You can play a bit more with the API

$addressCollection = \Drupal\geocoder\Geocoder::geocode($plugins, $address, $options);
$latitude = $addressCollection->first()->getCoordinates()->getLatitude();
$longitude = $addressCollection->first()->getCoordinates()->getLongitude();

You can also convert these to different formats using the Dumper plugins. Get the list of available Dumper by doing:

\Drupal\geocoder\Geocoder::getPlugins('Dumper')

Here's an example on how to use a Dumper

$plugins = array('geonames', 'googlemaps', 'bingmaps');
$address = '1600 Amphitheatre Parkway Mountain View, CA 94043';

$addressCollection = \Drupal\geocoder\Geocoder::geocode($plugins, $address);
$geojson = \Drupal\geocoder\Geocoder::getPlugin('Dumper', 'geojson')->dump($addressCollection->first());

There's also a dumper for GeoPHP, here's how to use it

$plugins = array('geonames', 'googlemaps', 'bingmaps');
$address = '1600 Amphitheatre Parkway Mountain View, CA 94043';

$addressCollection = \Drupal\geocoder\Geocoder::geocode($plugins, $address);
$geometry = \Drupal\geocoder\Geocoder::getPlugin('Dumper', 'geometry')->dump($addressCollection->first());

About

Rewrite of the Geocoder module

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages