]> git.mxchange.org Git - friendica-addons.git/blobdiff - openstreetmap/openstreetmap.php
Merge pull request #164 from tobiasd/20131119
[friendica-addons.git] / openstreetmap / openstreetmap.php
index d76414c440c98539caf1289a0e584ee085047266..ccb177ab92dfac8aac87cc12de1d333000c5c555 100755 (executable)
@@ -1,42 +1,73 @@
 <?php
 /**
- * Name: Open Street Map
- * Description: Use openstreetmap.org for displaying locations.
- * Version: 1.0
+ * Name: OpenStreetMap
+ * Description: Use OpenStreetMap for displaying locations.  After activation the post location just beneath your avatar in your posts will link to openstreetmap.
+ * Version: 1.1
  * Author: Mike Macgirvin <http://macgirvin.com/profile/mike>
- * 
+ * Author: Klaus Weidenbach
  *
  */
 
-
 function openstreetmap_install() {
-
        register_hook('render_location', 'addon/openstreetmap/openstreetmap.php', 'openstreetmap_location');
+       register_hook('page_header', 'addon/openstreetmap/openstreetmap.php', 'openstreetmap_alterheader');\r
 
        logger("installed openstreetmap");
 }
 
-
 function openstreetmap_uninstall() {
-
        unregister_hook('render_location', 'addon/openstreetmap/openstreetmap.php', 'openstreetmap_location');
+       unregister_hook('page_header', 'addon/openstreetmap/openstreetmap.php', 'openstreetmap_alterheader');
 
        logger("removed openstreetmap");
 }
 
-
+function openstreetmap_alterheader($a, &$navHtml) {
+       $addScriptTag='<script type="text/javascript" src="' . $a->get_baseurl() . '/addon/openstreetmap/openstreetmap.js' . '"></script>' . "\r\n";\r
+       $a->page['htmlhead'] .= $addScriptTag;
+}
 
 function openstreetmap_location($a, &$item) {
 
+       //
+
+       if(! (strlen($item['location']) || strlen($item['coord'])))
+               return;
+
+       /*
+        * Get the configuration variables from the .htconfig file.
+       */
+       $tmsserver = get_config('openstreetmap','tmsserver');
+       if(! $tmsserver)
+               $tmsserver = 'http://openstreetmap.org';
+       $zoom = get_config('openstreetmap','zoom');
+       if(! $zoom)
+               $zoom = 17;
+
        $location = '';
        $coord = '';
 
-       $location = (($item['location']) ? '<a target="map" title="' . $item['location'] . '" href="http://www.openstreetmap.org/?q=' . urlencode($item['location']) . '">' . $item['location'] . '</a>' : '');
+       
+       if($item['location'] && !$item['coord'] && true){ //if only a location is given, find the lat-lon
+               $geo_account='demo';
+\r
+               $s = fetch_url('http://api.geonames.org/search?maxRows=1&fuzzy=0.8&q=' . $item['location']  . '&username=' . $geo_account);\r
+\r
+               if($s){\r
+                       $xml = parse_xml_string($s);\r
+\r
+                       if($xml->geoname->lat && $xml->geoname->lng){\r
+                               $item['coord'] = $xml->geoname->lat . ' ' . $xml->geoname->lng;
+                       }\r
+               }
+       }
+
+       $location = (($item['location']) ? '<a target="map" title="' . $item['location'] . '" href="'.$tmsserver.'?q=' . urlencode($item['location']) . '">' . $item['location'] . '</a>' : '');
 
        if($item['coord']) {
                $coords = explode(' ', $item['coord']);
                if(count($coords) > 1) {
-                       $coord = '<a target="map" title="' . $item['coord'] . '" href="http://www.openstreetmap.org/?lat=' . urlencode($coords[0]) . '&lon=' . urlencode($coords[1]) . '&zoom=10">' . $item['coord'] . '</a>' ;
+                       $coord = '<a target="map" class="OSMMapLink" title="' . $item['coord'] . '" href="'.$tmsserver.'?lat=' . urlencode($coords[0]) . '&lon=' . urlencode($coords[1]) . '&zoom='.$zoom.'"> Map </a>' ;
                }
        }
        if(strlen($coord)) {
@@ -49,3 +80,26 @@ function openstreetmap_location($a, &$item) {
        return;
 }
 
+
+function openstreetmap_plugin_admin (&$a, &$o) {
+       $t = get_markup_template( "admin.tpl", "addon/openstreetmap/" );
+       $tmsserver = get_config('openstreetmap','tmsserver');
+       if(! $tmsserver)
+               $tmsserver = 'http://openstreetmap.org';
+       $zoom = get_config('openstreetmap','zoom');
+       if(! $zoom)
+               $zoom = 17;
+
+       $o = replace_macros( $t, array(
+                       '$submit' => t('Save Settings'),
+                       '$tmsserver' => array('tmsserver', t('Tile Server URL'), $tmsserver, t('A list of <a href="http://wiki.openstreetmap.org/wiki/TMS" target="_blank">public tile servers</a>')),
+                       '$zoom' => array('zoom', t('Default zoom'), $zoom, t('The default zoom level. (1:world, 18:highest)')),
+       ));
+}
+function openstreetmap_plugin_admin_post (&$a) {
+       $url = ((x($_POST, 'tmsserver')) ? notags(trim($_POST['tmsserver'])) : '');
+       $zoom = ((x($_POST, 'zoom')) ? intval(trim($_POST['zoom'])) : '17');
+       set_config('openstreetmap', 'tmsserver', $url);
+       set_config('openstreetmap', 'zoom', $zoom);
+       info( t('Settings updated.'). EOL);
+}