cowegis/contao-geocoder

Geocoder integration in Contao CMS

1.0.1 2024-09-17 09:20 UTC

README

Build Status Version License Downloads

This extension integrates the Geocoder PHP library into Contao CMS. It's designed for other extensions to use a common geocoder implementation.

Features

  • Geocoder service for other extensions
  • Built in support for nominatim and google maps
  • Extandable for other providers
  • Database driven configuration of providers in Contao backend
  • Application driven configuration of providers
  • API endpoint for geocode queries

Requirements

  • Contao ^4.13 || ^5.3
  • PHP ^8.2

Installation

 composer require cowegis/contao-geocoder ^1.0.0
 # Optional for Nominatim support
 composer require geocoder-php/nominatim-provider ^5.5
 # Optional for Google Maps support
 composer require geocoder-php/google-maps-provider ^4.6

Usage

Configuration

Optional application configuration

# app/config/config.yml

cowegis_contao_geocoder:
    providers:
      foo:
        title: "Foo Geocoder"
        type: "google_maps"
        google_api_key: "ABC"
      bar:
        title: "Bar Geocoder"
        type: "nominatim"
    default_provider: "bar"

Code example

<?php
 
use Cowegis\ContaoGeocoder\Provider\Geocoder;

final class MyService
{
    private $geocoder;
    
    public function __construct(Geocoder $geocoder)
    {
        $this->geocoder = $geocoder;
    }
    
    public function geocode(string $address) : \Geocoder\Location
    {
        return $this->geocoder
            // Optional use a specific geocoder. Otherwise the default provider is used 
            ->using('foo')
            ->geocodeQuery(\Geocoder\Query\GeocodeQuery::create($address))
            ->first();
    }
}

License

This extension is licensed under LGPL-3.0-or-later