PHP Classes

OpenStreetMap Nominatim PHP Class: Search addresses with the OSM Nominatim API

Recommend this page to a friend!
  Info   View files Example   View files View files (2)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 287 This week: 3All time: 7,527 This week: 44Up
Version License PHP version Categories
osmsearch 1.0.0GNU General Publi...5PHP 5, Web services, Geography
Description 

Author

This class can search addresses with the OSM Nominatim API.

It can send HTTP requests to the OpenStreetMap Nominatim API Web server to perform several operations. Currently it can:

- Search for the geographic location of an address given the street name, city, post code and country
- Search for the street address of the geographic given the latitude and longitude coordinates of the location, as well related information of that location like nearby attractions, sports centers, restaurant, etc.. when possible.

Innovation Award
PHP Programming Innovation award winner
February 2020
Winner
OpenStreetMap is a free alternative to other well known solutions for presenting maps on Web pages, as well inquire about locations on the map.

This class provides means to search for addresses and their geographic locations by calling the OSM Nominatim API.

Manuel Lemos
Picture of Stefan Kientzler
  Performance   Level  
Name: Stefan Kientzler is available for providing paid consulting. Contact Stefan Kientzler .
Classes: 18 packages by
Country: Germany Germany
Age: 56
All time rank: 73147 in Germany Germany
Week rank: 17 Up1 in Germany Germany Up
Innovation award
Innovation award
Nominee: 11x

Winner: 6x

Example

<?php
use lib\OSMap\OSMapNominatim;
require_once
'lib/OSMap/OSMapNominatim.php';

    echo
'<!DOCTYPE html>' . PHP_EOL;
    echo
'<html>' . PHP_EOL;
    echo
'<head>' . PHP_EOL;
    echo
'<meta charset="ISO-8859-1">' . PHP_EOL;
    echo
'<title>OSMapNominatim test</title>' . PHP_EOL;
    echo
'</head>' . PHP_EOL;
    echo
'<body>' . PHP_EOL;
   
   
/**
     * similar code may executed in a ajax call from a form to complete address/contact information
     * NOTE, call it by onclick() on button or somthing else and NOT in a onchange() handler
     * for auto complete
     * See Nominatim usage policy
     * @link https://operations.osmfoundation.org/policies/nominatim
     */

   
$oOSMap = new OSMapNominatim();
   
$oOSMap->setEncoding('Windows-1252');
   
   
$oOSMap->setStr('10 Downing Street');
   
$oOSMap->setCity('London');

   
$oOSMap->setStreetFormat(OSMapNominatim::STREET_NR_NAME);
   
    if (
$oOSMap->searchAddress()) {
        echo
'<h2>Result</h2>' . PHP_EOL;
        echo
'<h3>Full Address:</h3>' . PHP_EOL;
        echo
$oOSMap->getStr() . '<br/>' . PHP_EOL;
        echo
$oOSMap->getPostcode() . ' ' . $oOSMap->getCity() . '<br/>' . PHP_EOL;
        echo
$oOSMap->getCountry() . ' (' . $oOSMap->getRegion() . ')<br/>' . PHP_EOL;
       
$strInfo = $oOSMap->getInfo();
        if (
strlen($strInfo) > 0) {
            echo
'<h3>Information:</h3>' . PHP_EOL;
            echo
$strInfo . '<br/>' . PHP_EOL;
        }
        echo
'<h3>Location:</h3>' . PHP_EOL;
        echo
'latitude: ' . $oOSMap->getLatitude() . '<br/>' . PHP_EOL;
        echo
'longitude: ' . $oOSMap->getLongitude() . '<br/>' . PHP_EOL;
        echo
'location (DDD): ' . $oOSMap->getLocation(OSMapNominatim::FORMAT_DDD) . '<br/>' . PHP_EOL;
        echo
'location (DMM): ' . $oOSMap->getLocation(OSMapNominatim::FORMAT_DMM) . '<br/>' . PHP_EOL;
        echo
'location (DMS): ' . $oOSMap->getLocation(OSMapNominatim::FORMAT_DMS) . '<br/>' . PHP_EOL;
    }
   
   
$oOSMap->reset();
   
$oOSMap->setLatitude('40.4522186');
   
$oOSMap->setLongitude('-3.6889077');
   
// same effect: $oOSMap->setLocation('40.4522186, -3.6889077');
   
$oOSMap->setStreetFormat(OSMapNominatim::STREET_NAME_NR, ',');
   
    if (
$oOSMap->searchLocation()) {
        echo
'<h2>Result</h2>' . PHP_EOL;
        echo
'<h3>Full Address:</h3>' . PHP_EOL;
        echo
$oOSMap->getStr() . '<br/>' . PHP_EOL;
        echo
$oOSMap->getPostcode() . ' ' . $oOSMap->getCity() . '<br/>' . PHP_EOL;
        echo
$oOSMap->getCountry() . ' (' . $oOSMap->getRegion() . ')<br/>' . PHP_EOL;
       
$strInfo = $oOSMap->getInfo();
        if (
strlen($strInfo) > 0) {
            echo
'<h3>Location:</h3>' . PHP_EOL;
            echo
$strInfo . '<br/>' . PHP_EOL;
        }
        echo
'<h3>Location:</h3>' . PHP_EOL;
        echo
'latitude: ' . $oOSMap->getLatitude() . '<br/>' . PHP_EOL;
        echo
'longitude: ' . $oOSMap->getLongitude() . '<br/>' . PHP_EOL;
        echo
'location (DDD): ' . $oOSMap->getLocation(OSMapNominatim::FORMAT_DDD) . '<br/>' . PHP_EOL;
        echo
'location (DMM): ' . $oOSMap->getLocation(OSMapNominatim::FORMAT_DMM) . '<br/>' . PHP_EOL;
        echo
'location (DMS): ' . $oOSMap->getLocation(OSMapNominatim::FORMAT_DMS) . '<br/>' . PHP_EOL;
    }
   
    echo
'</body>' . PHP_EOL;
    echo
'</html>' . PHP_EOL;
   


  Files folder image Files  
File Role Description
Files folder imagelib (1 directory)
Accessible without login Plain text file SearchTest.php Example example search

  Files folder image Files  /  lib  
File Role Description
Files folder imageOSMap (1 file)

  Files folder image Files  /  lib  /  OSMap  
File Role Description
  Plain text file OSMapNominatim.php Class single class in package

 Version Control Reuses Unique User Downloads Download Rankings  
 0%1
Total:287
This week:3
All time:7,527
This week:44Up