PHP code example of a2nt / silverstripe-mapboxfield

1. Go to this page and download the library: Download a2nt/silverstripe-mapboxfield 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/ */

    

a2nt / silverstripe-mapboxfield example snippets



class MyDataObjectMapPin extends DataObject
{
    private static $extensions = [
        Addressable::class,
        MarkerExtension::class,
    ];
}

class MyPage extends \Page
{
    private static $db = [
        'MapZoom' => 'Int',
    ];
    
    public function MapPins()
    {
        return MyDataObjectMapPin::get();
    }

    public function getGeoJSON()
    {
        $pins = [];
        foreach ($this->MapPins() as $pin) {
            $pins[] = $pin->getGeo();
        }
        return json_encode([
            'type' => 'MarkerCollection',
            'features' => $pins
        ]);
    }
}