PHP code example of apacheborys / location-bundle
1. Go to this page and download the library: Download apacheborys/location-bundle 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/ */
apacheborys / location-bundle example snippets
$database = new \Symfony\Component\Cache\Adapter\FilesystemAdapter();
$dbConfig = new \ApacheBorys\Location\Model\DBConfig();
$locationBundle = new \ApacheBorys\Location\Location($database, $dbConfig);
$headers = [
'Accept-language' => 'en'
];
/* query for Kiev city, Ukraine */
$query = [
'format' => 'geocodejson',
'osm_ids' => 'R421866',
'polygon_geojson' => 1,
'addressdetails' => 1,
];
$request = new \http\Client\Request('GET', 'https://nominatim.openstreetmap.org/lookup?' . http_build_query($query), $headers);
/** @var \Psr\Http\Message\ResponseInterface $response */
$response = new \Http\Client\HttpClient($request);
$rawGeoCodeJson = json_decode((string) $response->getBody());
$query['format'] = 'geojson';
unset($query['polygon_geojson'], $query['addressdetails']);
$request = new \http\Client\Request('GET', 'https://nominatim.openstreetmap.org/lookup?' . http_build_query($query), $headers);
$response = new \Http\Client\HttpClient($request);
$rawGeoJson = json_decode((string) $response->getBody());
$rawGeoCodeJson['features'][0]['properties']['common']['bbox'] = $rawGeoJson['features'][0]['bbox'];
$rawGeoCodeJson['features'][0]['properties']['common']['postcode'] = $rawGeoJson['features'][0]['properties']['address']['postcode'];
$rawGeoCodeJson['features'][0]['properties']['geocoding']['country_code'] = $rawGeoJson['features'][0]['properties']['address']['country_code'];
$locationBundle->addPlace(mapRawDataToPlace($rawGeoCodeJson));
private function mapRawDataToPlace(array $rawData): \ApacheBorys\Location\Model\Place
{
$root = $rawData['features'][0];
$polygons = [];
foreach ($root['geometry']['coordinates'] as $rawPolygon) {
$tempPolygon = new \ApacheBorys\Location\Model\Polygon();
foreach ($rawPolygon as $coordinates) {
$tempPolygon->addCoordinates(new \ApacheBorys\Location\Model\Coordinates($coordinates[0], $coordinates[1]));
}
$polygons[] = $tempPolygon;
}
$addresses = [];
foreach ($root['properties'] as $locale => $rawAddress) {
if ('common' === $locale) {
continue;
}
$addresses[$locale] = $this->mapRawDataToAddress($rawAddress, $locale);
}
return new \ApacheBorys\Location\Model\Place(
$addresses,
$polygons,
\ApacheBorys\Location\Model\Place::DEFAULT_LOCALE,
$root['properties']['common']['postcode'],
null,
$rawData['geocoding']['attribution'],
new \ApacheBorys\Location\Model\Bounds(
$root['properties']['common']['bbox'][0],
$root['properties']['common']['bbox'][1],
$root['properties']['common']['bbox'][2],
$root['properties']['common']['bbox'][3]
)
);
}
private function mapRawDataToAddress(array $rawData, string $locale): \ApacheBorys\Location\Model\Address
{
$adminLevels = [];
foreach ($rawData['geocoding']['admin'] as $adminLevel => $name) {
$level = (int) substr($adminLevel, 5);
$adminLevels[$level] = new \ApacheBorys\Location\Model\AdminLevel($level, $name);
}
return new \ApacheBorys\Location\Model\Address(
$locale,
new \ApacheBorys\Location\Model\AdminLevelCollection($adminLevels),
$rawData['geocoding']['housenumber'] ?? '',
$rawData['geocoding']['street'] ?? '',
$rawData['geocoding']['state'] ?? '',
$rawData['geocoding']['city'] ?? '',
new \ApacheBorys\Location\Model\Country($rawData['geocoding']['country'], $rawData['geocoding']['country_code'])
);
}
$address = $locationBundle->reverseQuery(
new \ApacheBorys\Location\Query\ReverseQuery(new \ApacheBorys\Location\Model\Coordinates(50.4422519, 30.5423135))
);
$address = $locationBundle->geocodeQuery(new \ApacheBorys\Location\Query\GeocodeQuery('Kyiv, Ukraine'));
$distance = $this->location->distance(
new \ApacheBorys\Location\Model\Coordinates(30.520620, 50.455414, 172.6),
new \ApacheBorys\Location\Model\Coordinates(30.557294, 50.434596, 190.8)
);
$neighbours = $this->location->findTouchedPlaces(
$originalPlace,
$maxDistanceToBorder,
$specificPlaces
);