Examples

Examples – various code examples

Search for all cities named Paris

<?php
require_once 'Services/GeoNames.php';

$geo = new Services_GeoNames();

$cities = $geo->search(array('name_equals' => 'Paris'));
echo 
"List of cities named Paris:\n";
foreach(
$cities as $city) {
    
printf(" - %s (%s)\n", $city->name, $city->countryName);
}
echo 
"\n";

?>

Find all postal codes near by Toulouse in a radius of 10km

<?php

require_once 'Services/GeoNames.php';

$geo = new Services_GeoNames();

$postalCodes = $geo->findNearbyPostalCodes(array(
    
'lat'     => 43.606,
    
'lng'     => 1.444,
    
'radius'  => 10, // 10km
    
'maxRows' => 100
));
echo 
"List of postal codes near by Toulouse in a radius of 10km:\n";
foreach (
$postalCodes as $code) {
    
printf(" - %s (%s)\n", $code->postalCode, $code->placeName);
}
echo 
"\n";

?>

List all countries and capitals in spanish

<?php
require_once 'Services/GeoNames.php';

$geo = new Services_GeoNames();
$countries = $geo->countryInfo(array('lang' => 'es'));
echo 
"List of all countries in spanish language:\n";
foreach (
$countries as $country) {
    
printf(" - %s (capital: %s)\n", $country->countryName, $country->capital);
}
echo 
"\n";

?>

List neightbours countries of France

<?php
require_once 'Services/GeoNames.php';

$geo = new Services_GeoNames();

// retrieve the geonameId if we don't know it
$array     = $geo->countryInfo(array('country' => 'FR'));
$geonameId = $array[0]->geonameId;

$neighbours = $geo->neighbours(array('geonameId' => $geonameId));
echo 
"Neighbours of France are:\n";
foreach (
$neighbours as $neighbour) {
    
printf(" - %s\n", $neighbour->countryName);
}

?>