PHP Classes

ah Map Helper: Parse URLs of map services and get their location

Recommend this page to a friend!
  Info   View files Example   Demos   View files View files (4)   DownloadInstall with Composer Download .zip   Reputation   Support forum (1)   Blog    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 173 All time: 8,800 This week: 129Up
Version License PHP version Categories
ah-map-helper 1.0GNU General Publi...5PHP 5, Geography, Parsers


This class can parse URLs of map services and get their location.

It can take URLs of maps from Google Maps, Yandex and Open Street map and extract the location coordinates and other parameters.

The class can also regenerate the URLs of map pages of any the supported services given the location coordinates and zoom level.

Innovation Award
PHP Programming Innovation award nominee
May 2016
Number 14
There are several types of mapping applications on the Web that can show specific locations given a URL that represents that location. However, each application uses different URL parameters to represent the location coordinates.

This class provides a general solution to extract location coordinates from map URLs of Google Maps, Yandex maps and OpenStreetMap.

It can also also do the opposite, i.e. given the coordinates of a location it can generate URLs to show that location in the different mapping sites that it supports.

Manuel Lemos
Picture of Axel Hahn
  Performance   Level  
Name: Axel Hahn <contact>
Classes: 5 packages by
Country: Switzerland Switzerland
Age: 52
All time rank: 267826 in Switzerland Switzerland
Week rank: 195 Up5 in Switzerland Switzerland Up
Innovation award
Innovation award
Nominee: 2x


= "Tester for map position helper (Php class)";
$aUrls = array(

// ------------------------------------------------------------------
// ------------------------------------------------------------------

function getOutput($sUrl = false) {

    if (!
$sUrl) {
$oMaphelper = new ahmaphelper();
$aPos = $oMaphelper->getPos($sUrl);
$sUrlList = '';
    if (
$aPos) {

. print_r($oMaphelper->getUrls(), 1)

$aFixed = $oMaphelper->fixPosition();
        if (
array_key_exists('_orig', $aFixed)) {
$sUrlList.='<h3>Fix position values</h3>'
. 'What you see in the resultset above is: we did not get links to all providers.<br><br>'
. 'BUT: There is a '
. 'healing function that creates compatible values. '
. 'The key _warnings contains info messages, '
. 'what was fixed.<br>'
. '<pre>$oMaphelper-><strong>fixPosition</strong>();<br>'
. print_r($aFixed, 1)
. 'The values were fixed. Let\'s re-run the method getUrls:'
. '<pre>$oMaphelper-><strong>getUrls</strong>();<br>'
. print_r($oMaphelper->getUrls(), 1)
        } else {
. 'OK, we got links of all providers. If not: you should know that there is a '
. 'healing function that creates compatible values. '
. '<pre>$oMaphelper-><strong>fixPosition</strong>();</pre>'
$sUrlList.='<h3>link generation with position data</h3>'
. '<p>'
. 'With latitude, longitude and zoom you can generate links too.<br>'
. 'To get links to all providers:'
. '</p>'
. '<pre>$oMaphelper-><strong>generateUrls</strong>(' . $aPos['lat'] . ', ' . $aPos['lon'] . ', ' . $aPos['zoom'] . ')<br>'
. print_r($oMaphelper->generateUrls($aPos['lat'], $aPos['lon'], $aPos['zoom']), 1)
. '<p>'
. '... or just one provider<br>'
. '</p>'
. '<pre>$oMaphelper-><strong>generateUrl</strong>("google", ' . $aPos['lat'] . ', ' . $aPos['lon'] . ', ' . $aPos['zoom'] . ')<br>'
. print_r($oMaphelper->generateUrl("google", $aPos['lat'], $aPos['lon'], $aPos['zoom']), 1)
. ''
'<h2>Output</h2><div class="output">'
. '<strong>URL:</strong> <a href="' . $sUrl . '" target="_blank">' . $sUrl . '</a>'
. '<div style="margin-left: 2em;">'
. '<br><h3>Get the position</h3>'
. '<pre>$oMaphelper-><strong>getPos</strong>("' . $sUrl . '")<br>' . print_r($aPos, 1) . '</pre><br>'
. ($aPos ? '<h3>Urls that point to the same position</h3>' . $sUrlList . '<br>' : 'ERROR: No position was detected. The url cannot be parsed.' )
. '</div>'
        <title><?php echo $sTitle; ?></title>
            a{color: #33f;}
            body{background: #f8f8f8; color:#334; font-family: verdana, arial; font-size: 1em;}
            h1{color:#889; font-size: 200%;}
            h2{color:#99a; margin: 1em 0 0;font-size: 300%;}
            h3{color:#aab; margin: 0;font-size: 200%;}
            pre{background: #fff; border: 1px solid #ddd; padding: 0.5em;}
            pre strong{color:#222; background: #def;}
            input{border: 0px solid #ccc; }
                list-style: none;
                counter-increment: item;
                margin-bottom: 1em;
                content: counter(item);
                font-size: 200%;
                border: 0px solid #555; border-radius: 50%;
                background: #99a;
                margin: 0 0.5em 0.5em 0em;
                padding: 0 0.3em;
            #eUrl{border: 1px solid #ccc; color:#567; padding: 0.5em; font-size: 110%; box-shadow: 0 0 1em #ccc inset ;}
            .content{width: 1000px; margin: 1em auto 3em; padding: 1em; box-shadow: 0 0 4em #ccc; border: 1px solid #fff;}
            .infobox{border: 2px dotted #ccd; padding: 1em; background: #f0f4f8; color:#889;}
            .output{background: #f0f0f0; border: 1px solid #ccc; padding: 1em; margin-bottom: 1em;}
            .footer{background: #f0f0f0; margin-top: 2em; padding: 0.5em; color:#aab;}
            .submit, .reset{
                font-size: 130%;padding: 0.4em;border: 1px solid rgba(0,0,0,0.3); border-radius: 0.3em;
                color: rgba(0,0,0,0.5);
            .submit{background: #8c8; }
            .reset{background: #f88; padding-left: 0.6em; padding-right: 0.6em;}
        <div class="content">
            <div class="infobox">
                <h1>Tester for map position helper (Php class)</h1>
                    This helper class can parse urls of a map services
                    Google maps, Openstreetmap and Yandex. It fetches
                    the position longitude and latitude from an url.<br>
                    It generates urls with the same position with other
                    map services.
            <h2>Try it yourself!</h2>
                    <a href="" target="_blank">Google maps</a> or
                    <a href="" target="_blank">Open street map</a> or
                    <a href="" target="_blank">Yandex map</a>,</li>
                <li>Zoom in, go to any position and then</li>
                <li>Copy the url and paste it below.</li>

// ------------------------------------------------------------------
            // OUTPUT
            // ------------------------------------------------------------------

$sUrl = array_key_exists('url', $_GET) && $_GET['url'] ? strip_tags($_GET['url']) : '';

. '<form method="GET" action="?" style="margin-left: 6em;">
                <!--<label for="eUrl">Paste your url here:</label><br>-->
                <input type="text" id="eUrl" name="url" size="90" value="'
. $sUrl . '" />
                <input class="submit" type="submit" value="Go">'
. ($sUrl ? ' <input type="button" class="reset" onclick="location.href=\'?\';" value=" x "/>' : '')
. getOutput($sUrl) . '
              <p>Click one :-)<br>
            foreach (
$aUrls as $sUrl2) {
'<a href="?url=' . urlencode($sUrl2) . '">' . $sUrl2 . '</a><br>';
// echo getOutput($sUrl);

            Source: <a href="">Github</a>
            Author: Axel Hahn (<a href=""></a>)
            <div class="footer">


------------------------------------------------------------------------------- ahmaphelper map position helper (Php class) This helper class can parse urls of a map services Google maps, Openstreetmap and Yandex. It fetches the position longitude and latitude from an url. It generates urls with the same position with other map services. Licence GPL 3.0 author: Axel Hahn see DEMO DOCS ------------------------------------------------------------------------------- 2016-04-17 1.0 first public release 2016-05-25 1.1 added options minzoom; new methods getUrls() + fixPosition() 2017-01-15 1.2 added support for regional maps (provider map1eu) ------------------------------------------------------------------------------- USAGE: require_once 'ahmaphelper.class.php'; $oMaphelper = new ahmaphelper(); (1) get a position from url: go to a map provider: Zoom in, go to any position and copy the url $aPos = $oMaphelper->getPos($sUrl); (2) generate urls with the same position print_r($oMaphelper->getUrls()); (3) fix position data to be compatible to all map providers. print_r($oMaphelper->fixPosition()); print_r($oMaphelper->getUrls()); (4) manually generate links with position data and zoom level (returns an array): print_r($oMaphelper->generateUrls($aPos['lat'], $aPos['lon'], $aPos['zoom'])); (5) manually generate link with position data and zoom level to a given provider (returns a string): echo $oMaphelper->generateUrl([provider], $aPos['lat'], $aPos['lon'], $aPos['zoom']); echo $oMaphelper->generateUrls("google", $aPos['lat'], $aPos['lon'], $aPos['zoom']); (6) helper functions: generate url with the same position to a specific provider $oMaphelper->getProviders() ... returns names of known providers $oMaphelper->getMinZoom() ... returns lowest maxzoom level of all providers -------------------------------------------------------------------------------

  demo maphelperExternal page  
  Files folder image Files  
File Role Description
Plain text file ahmaphelper.class.php Class Class source
Accessible without login Plain text file ahmaphelper.maps.php Aux. Auxiliary script
Accessible without login Plain text file demo.php Example Example script
Accessible without login Plain text file readme.txt Doc. Documentation

 Version Control Unique User Downloads Download Rankings  
This week:0
All time:8,800
This week:129Up