Silverstripe field for fetching geo data from google maps api for any addresses
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
composer require "nblum/silverstripe-geocodefield"
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);
}
}
Module rating system helping users find modules that are well supported. For more on how the rating system works visit Module standards
Score not correct? Let us know there is a problem