PHP code example of nblum / silverstripe-geocodefield

1. Go to this page and download the library: Download nblum/silverstripe-geocodefield library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

nblum / silverstripe-geocodefield example snippets



    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;
        }
    }
    


    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;
        }
    }
    


    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);
        }
    }