Address field with geolocation
- Silverstripe CMS
^4 || ^5
- "jeroendesloovere/geolocation-php-api": "1.3.*"
composer require "thewebmen/silverstripe-addressfield" "dev-master"
Add the Google Maps API key:
TheWebmen\Addressfield\Forms\GooglePlacesField:
maps_api_key: 'API_KEY'
Or add the following variable to your .env
:
WDVLP_ADDRESSFIELD_MAPS_API_KEY='API_KEY'
Add all db fields to the db array:
private static $db = [
'City' => 'Varchar',
'Country' => 'Varchar',
'ZipCode' => 'Varchar',
'Street' => 'Varchar',
'StreetNumber' => 'Varchar',
'Latitude' => 'Varchar',
'Longitude' => 'Varchar'
];
Then add the field like any other field
public function updateCMSFields(FieldList $fields)
{
$fields->addFieldToTab('Root.Address', new AddressField($this->owner));
}
See the addressfield class for all constructor options.
The module comes with a distance helper to calculate the distance between two latitude/longitudes, example usage:
$list = ObjectWithLatitudeLongitude::get();
$list = TheWebmen\Addressfield\Helpers\DistanceHelper::addDistance($list, '52.2112', '5.9699');
$list = $list->sort('Distance');
foreach($list as $item){
var_dump($item->Distance);
}
- Improve documentation