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);
}

?>
getting started with the Services_GeoNames package (Previous) Services_Google (Next)
Last updated: Sat, 16 Feb 2019 — Download Documentation
Do you think that something on this page is wrong? Please file a bug report.
View this page in:
  • English

User Notes:

Note by: support@healingrooms.com
A username is now required to be included.

Here is a sample username line, involving the username 'Bob':

$geo = new Services_GeoNames('Bob');


That value can also be used later in the query:

$cities = $geo->timezone(array(
'lat' => $latitude,
'lng' => $longitude,
'username' => 'Bob',
));

The generation of a query string can also involve the username:

$sql_askgeo_code_universal = "http://api.geonames.org/timezone?";
$sql_askgeo_code = $sql_askgeo_code_universal . 'lat=' . $latitude . '&amp;lng=' . $longitude;
$sql_askgeo_code .= '&amp;username=Bob';