PHP code example of farzai / geonames
1. Go to this page and download the library: Download farzai/geonames 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/ */
farzai / geonames example snippets
$client = new MongoDB\Client('mongodb://localhost:27017');
$collection = $client->geonames->gazetteer;
// Find all places within 5km of Bangkok
$result = $collection->find([
'location' => [
'$near' => [
'$geometry' => [
'type' => 'Point',
'coordinates' => [100.51667, 13.75] // [longitude, latitude]
],
'$maxDistance' => 5000 // 5km in meters
]
]
]);
foreach ($result as $place) {
echo $place['name'] . ' - ' . $place['feature_code'] . PHP_EOL;
}
$client = new MongoDB\Client('mongodb://localhost:27017');
$collection = $client->geonames->postal_codes;
// Find all postal codes in Bangkok, Thailand
$result = $collection->find([
'country_code' => 'TH',
'admin_name1' => 'Bangkok'
]);
foreach ($result as $postalCode) {
echo $postalCode['postal_code'] . ' - ' . $postalCode['place_name'] . PHP_EOL;
}
use Farzai\Geonames\Exceptions\GeonamesException;
try {
// Download or convert operations
} catch (GeonamesException $e) {
echo "Error: " . $e->getMessage();
}