Wallogit.com
2017 © Pedro Peláez
GiBiLogic Elements - Geocoding
Often we find ourselves writing the same code for different projects, over and over again., (*1)
We got bored, so we decided to collect all these bits and pieces of code in ready-to-use packages., (*2)
This package contains useful classes for working with geographical coordinates., (*3)
It uses Google's geocoding service; you can find more informations on its official documentation., (*4)
Add this package to the composer.json of your application with the console command:, (*5)
composer require gibilogic/element-geocoding
Or, if you are using the composer.phar version, use the console command:, (*6)
php composer.phar require gibilogic/element-geocoding
Use the Point class to manage geographical points with latitude and longitude:, (*7)
$milan = new Point(45.464161, 9.190336); $rome = new Point(41.893056, 12.482778);
Use the Route class to manage relation between two points:, (*8)
$route = new Route($milan, $rome); $distance = $route->getDistance();
Route instances can also be compared by using the compareTo method:, (*9)
$milanRomeRoute = new Route($milan, $rome); $milanTurinRoute = new Route($milan, $turin); $comparison = $milanRomeRoute->compareTo($milanTurinRoute);
Use the geocodeAddress method of the GoogleGeocodeService to get a Point instance from an address:, (*10)
$point = $googleGeocodeService->geocodeAddress('via Aldo Moro 48, 25124 Brescia, Italy');
Add and implement the GeocodeableInterface to existing classes:, (*11)
class Address implements GeocodeableInterface
{
protected $address;
protected $zipCode;
protected $city;
protected $province;
protected $latitude;
protected $longitude;
// ...
public function getAddressForGeocoding()
{
return sprintf('%s, %s %s (%s), Italy',
$this->address,
$this->zipCode,
$this->city,
$this->province
);
}
public function getCoordinates()
{
return new Point($this->latitude, $this->longitude);
}
public function setCoordinates(Point $point)
{
$this->latitude = $point->getLatitude();
$this->longitude = $point->getLongitude();
}
}
Then use the geocode method of the GoogleGeocodeService:, (*12)
$point = $googleGeocodeService->geocode($address);
You can contribute to the growth of this library in a lot of different ways:, (*13)
See the attached license file., (*14)