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: 171 All time: 8,805 This week: 132Up
Version License PHP version Categories
ah-map-helper 1.0GNU General Publi...5PHP 5, Geography, Parsers
Description 

Author

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: 51
All time rank: 268126 in Switzerland Switzerland
Week rank: 371 Up3 in Switzerland Switzerland Up
Innovation award
Innovation award
Nominee: 2x

Example

<?php
$sTitle
= "Tester for map position helper (Php class)";
$aUrls = array(
   
"http://www.openstreetmap.org/#map=13/46.9384/7.4585",
   
"https://www.yandex.ru/maps/99/munich/?ll=11.555422%2C48.085201&z=10",
   
"https://www.google.ch/maps/@46.9457237,7.4302919,14z",
   
"https://www.google.com.br/maps/@-22.7405789,-47.3313609,20.25z",
);
require_once
'ahmaphelper.class.php';

// ------------------------------------------------------------------
// FUNCTIONS
// ------------------------------------------------------------------

function getOutput($sUrl = false) {

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


       
$sUrlList.='
                <pre>$oMaphelper-><strong>getUrls</strong>();<br>'
               
. print_r($oMaphelper->getUrls(), 1)
                .
'</pre>';

       
$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)
                    .
'</pre>'
           
;
           
$sUrlList.='<br>'
                   
. 'The values were fixed. Let\'s re-run the method getUrls:'
                   
. '<pre>$oMaphelper-><strong>getUrls</strong>();<br>'
                   
. print_r($oMaphelper->getUrls(), 1)
                    .
'</pre>';
        } else {
           
$sUrlList.=''
                   
. '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)
                .
'</pre>'
               
. '<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)
                .
'</pre>'
               
. ''
       
;
    }
    return
'<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>'
           
. '</div>'
   
;
}
?>
<!DOCTYPE HTML>
<html>
    <head>
        <title><?php echo $sTitle; ?></title>
        <style>
            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; }
            ol>li{
                list-style: none;
                counter-increment: item;
                margin-bottom: 1em;
            }
            ol>li::before{
                content: counter(item);
                font-size: 200%;
                border: 0px solid #555; border-radius: 50%;
                background: #99a;
                color:#dde;
                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;}
        </style>
    </head>
    <body>
        <div class="content">
            <div class="infobox">
                <h1>Tester for map position helper (Php class)</h1>
                <p>
                    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>
                    <br>
                    It generates urls with the same position with other
                    map services.
                </p>
            </div>
            <h2>Try it yourself!</h2>
            <ol>
                <li>Open
                    <a href="http://maps.google.com/" target="_blank">Google maps</a> or
                    <a href="http://www.openstreetmap.org" target="_blank">Open street map</a> or
                    <a href="https://yandex.ru/maps/" 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>
            </ol>

            <?php
           
// ------------------------------------------------------------------
            // OUTPUT
            // ------------------------------------------------------------------

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

            echo
''
           
. '<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 "/>' : '')
            .
'
              </form>
   
            '
. getOutput($sUrl) . '
              <h2>Examples</h2>
              <p>Click one :-)<br>
            '
;
            foreach (
$aUrls as $sUrl2) {
                echo
'<a href="?url=' . urlencode($sUrl2) . '">' . $sUrl2 . '</a><br>';
               
// echo getOutput($sUrl);
           
}
           
?>

            <br>
            <strong>Ressources</strong><br>
            Source: <a href="https://github.com/axelhahn/ahmaphelper">Github</a>
            /
            Author: Axel Hahn (<a href="http://www.axel-hahn.de/">www.axel-hahn.de</a>)
        </p>
            <div class="footer">
                www.axel-hahn.de
            </div>
    </div>
</body></html>


Details

------------------------------------------------------------------------------- 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 https://www.axel-hahn.de see DEMO https://www.axel-hahn.de/demos/maphelper/ DOCS https://www.axel-hahn.de/docs/ahmaphelper/index.htm ------------------------------------------------------------------------------- 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: https://www.google.ch/maps http://www.openstreetmap.org/ https://yandex.ru/maps/ 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  
 100%
Total:171
This week:0
All time:8,805
This week:132Up