PHP code example of clesson-de / silverstripe-geocoding
1. Go to this page and download the library: Download clesson-de/silverstripe-geocoding 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/ */
clesson-de / silverstripe-geocoding example snippets
use Clesson\Silverstripe\Geocoding\Forms\MapField;
public function getCMSFields(): FieldList
{
$fields = parent::getCMSFields();
$fields->removeByName(['LocationLatitude', 'LocationLongitude']);
/** @var MapField $locationField */
$locationField = MapField::create('Location', $this->fieldLabel('Location'));
$locationField->setValue($this->dbObject('Location'));
// Optional: configure zoom levels
$locationField->setZoomLevel(8); // Default zoom when no marker (1-20)
$locationField->setZoomLevelWithMarker(16); // Zoom when marker exists (street-level)
$fields->addFieldToTab('Root.Main', $locationField);
return $fields;
}
$location = $myRecord->dbObject('Location');
if ($location->exists()) {
echo $location->getLatitude(); // float, e.g. 52.5163
echo $location->getLongitude(); // float, e.g. 13.3777
echo $location->Nice(); // "52.5163, 13.3777"
}
// Set values
$location->setLatitude(52.5163);
$location->setLongitude(13.3777);
$myRecord->write();
use Clesson\Geocoding\Constants\GeocodingServiceType;
use Clesson\Geocoding\Models\GeocodingService;
use Clesson\Geocoding\Services\OpenStreetMapGeocodingService;
use Clesson\Geocoding\Services\GoogleGeocodingService;
// Load the first active OpenStreetMap service
$serviceRecord = GeocodingService::get()
->filter(['ServiceType' => GeocodingServiceType::OPEN_STREET_MAP, 'Active' => true])
->first();
$service = new OpenStreetMapGeocodingService($serviceRecord);