nblum/silverstripe-geocodefield

This package is abandoned and no longer maintained. No replacement package was suggested.

Silverstripe field for fetching geo data from google maps api for any addresses

Installs: 2 420

Dependents: 0

Suggesters: 0

Security: 0

Stars: 4

Watchers: 1

Forks: 3

Open Issues: 0

Type:silverstripe-vendormodule

2.2.0 2019-10-27 08:05 UTC

This package is auto-updated.

Last update: 2023-01-24 15:47:18 UTC


README

Fetches the geo position (lon,lat) from google maps api. Can be used as free address input field or referenced to other address fields in form

Requirements

  • Silverstripe 4.* or Silverstripe 3.*(use tag 0.3.0 for v3.x support)

Installation

Composer

  • composer require "nblum/silverstripe-geocodefield"

Manual

  • Download/Clone module in SilverStripe root directory

Usage

Configuration:

You may need to provide an api key from google (Geolocation API)

Nblum\Geocodefield\Forms\GeoCodeField:
  google_api_key: 'your_google_maps_api_ke'

...or try to geocode with Nominatim from OpenStreetMap https://wiki.openstreetmap.org/wiki/Nominatim

Nblum\Geocodefield\Forms\GeoCodeField:
  custom_geocoder: 'osm'

Basic Example:

    class MyPage extends Page {
    
        private static $db = array(
            'Geodata' => \Nblum\Geocodefield\Forms\Json::class
        );
    
        public function getCMSFields() {
            $fields = parent::getCMSFields();
    
            //creates a GeoCodeField field
            $fields->addFieldToTab('Root.Main', new \Nblum\Geocodefield\Forms\GeoCodeField('Geodata'));
    
            return $fields;
        }
    }
    

Example with referenced address fields:

    class MyPage extends Page {
    
        private static $db = array(
            'Street' => 'Varchar',
            'City' => 'Varchar',
            'Geodata' => 'Json'
        );
    
        public function getCMSFields() {
            $fields = parent::getCMSFields();
    
            $fields->addFieldToTab('Root.Main', new TextField('Street'));
            $fields->addFieldToTab('Root.Main', new TextField('City'));
    
            //creates a GeoCodeField field
            $field = new \Nblum\Geocodefield\Forms\GeoCodeField('Geodata', 'Geo Position');
            $field->addAddressReference('Street');
            $field->addAddressReference('City');
            $field->setAddressNotEditable();
            $fields->addFieldToTab('Root.Main', $field);
    
            return $fields;
        }
    }
    

Write lon/lat values to separate db columns

    class MyPage extends Page {
    
        private static $db = array(
            'Lat' => 'Varchar',
            'Lon' => 'Varchar',
            'GeoData' => 'Json'
        );
        
        public function getCMSFields() {
            //...
        }
        
        protected function onBeforeWrite()
        {
            parent::onBeforeWrite();
            
            //get current values and update some custom fields
            $parts = json_decode($this->getField('GeoData'));
            $this->setField('Lat', $parts->lat);
            $this->setField('Lon', $parts->lon);
        }
    }