1<?php 2/* 3 * Copyright (c) 2008-2014 Mark C. Prins <mprins@users.sf.net> 4 * 5 * Permission to use, copy, modify, and distribute this software for any 6 * purpose with or without fee is hereby granted, provided that the above 7 * copyright notice and this permission notice appear in all copies. 8 * 9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 */ 17if (! defined ( 'DOKU_INC' )) 18 define ( 'DOKU_INC', realpath ( dirname ( __FILE__ ) . '/../../' ) . '/' ); 19if (! defined ( 'DOKU_PLUGIN' )) 20 define ( 'DOKU_PLUGIN', DOKU_INC . 'lib/plugins/' ); 21require_once (DOKU_PLUGIN . 'syntax.php'); 22 23/** 24 * DokuWiki Plugin openlayersmap (Syntax Component). 25 * Provides for display of an OpenLayers based map in a wiki page. 26 * 27 * @author Mark Prins 28 */ 29class syntax_plugin_openlayersmap_olmap extends DokuWiki_Syntax_Plugin { 30 31 /** 32 * defaults of the known attributes of the olmap tag. 33 */ 34 private $dflt = array ( 35 'id' => 'olmap', 36 'width' => '550px', 37 'height' => '450px', 38 'lat' => 50.0, 39 'lon' => 5.1, 40 'zoom' => 12, 41 'statusbar' => true, 42 'controls' => true, 43 'poihoverstyle' => false, 44 'baselyr' => 'OpenStreetMap', 45 'gpxfile' => '', 46 'kmlfile' => '', 47 'geojsonfile' => '', 48 'summary' => '' 49 ); 50 51 /** 52 * 53 * @see DokuWiki_Syntax_Plugin::getType() 54 */ 55 function getType() { 56 return 'substition'; 57 } 58 59 /** 60 * 61 * @see DokuWiki_Syntax_Plugin::getPType() 62 */ 63 function getPType() { 64 return 'block'; 65 } 66 67 /** 68 * 69 * @see Doku_Parser_Mode::getSort() 70 */ 71 function getSort() { 72 return 901; 73 } 74 75 /** 76 * 77 * @see Doku_Parser_Mode::connectTo() 78 */ 79 function connectTo($mode) { 80 $this->Lexer->addSpecialPattern ( '<olmap ?[^>\n]*>.*?</olmap>', $mode, 'plugin_openlayersmap_olmap' ); 81 } 82 83 /** 84 * 85 * @see DokuWiki_Syntax_Plugin::handle() 86 */ 87 function handle($match, $state, $pos, &$handler) { 88 // break matched cdata into its components 89 list ( $str_params, $str_points ) = explode ( '>', substr ( $match, 7, - 9 ), 2 ); 90 // get the lat/lon for adding them to the metadata (used by geotag) 91 preg_match ( '(lat[:|=]\"-?\d*\.\d*\")', $match, $mainLat ); 92 preg_match ( '(lon[:|=]\"-?\d*\.\d*\")', $match, $mainLon ); 93 $mainLat = substr ( $mainLat [0], 5, - 1 ); 94 $mainLon = substr ( $mainLon [0], 5, - 1 ); 95 96 $gmap = $this->_extract_params ( $str_params ); 97 $overlay = $this->_extract_points ( $str_points ); 98 $_firstimageID = ''; 99 100 $_nocache = false; 101 // choose maptype based on the specified tag 102 $imgUrl = "{{"; 103 if (stripos ( $gmap ['baselyr'], 'google' ) !== false) { 104 // Google 105 $imgUrl .= $this->_getGoogle ( $gmap, $overlay ); 106 $imgUrl .= "&.png"; 107 } elseif (stripos ( $gmap ['baselyr'], 'bing' ) !== false) { 108 // Bing 109 if (! $this->getConf ( 'bingAPIKey' )) { 110 // in case there is no Bing api key we'll use OSM 111 $_firstimageID = $this->_getStaticOSM ( $gmap, $overlay ); 112 $imgUrl .= $_firstimageID; 113 if ($this->getConf ( 'optionStaticMapGenerator' ) == 'remote') { 114 $imgUrl .= "&.png"; 115 } 116 } else { 117 // seems that Bing doesn't like the DW client, turn off caching 118 $_nocache = true; 119 $imgUrl .= $this->_getBing ( $gmap, $overlay ) . "&.png"; 120 } 121 } elseif (stripos ( $gmap ['baselyr'], 'mapquest' ) !== false) { 122 // MapQuest 123 if (! $this->getConf ( 'mapquestAPIKey' )) { 124 // no API key for MapQuest, use OSM 125 $_firstimageID = $this->_getStaticOSM ( $gmap, $overlay ); 126 $imgUrl .= $_firstimageID; 127 if ($this->getConf ( 'optionStaticMapGenerator' ) == 'remote') { 128 $imgUrl .= "&.png"; 129 } 130 } else { 131 $imgUrl .= $this->_getMapQuest ( $gmap, $overlay ); 132 $imgUrl .= "&.png"; 133 } 134 } else { 135 // default OSM 136 $_firstimageID = $this->_getStaticOSM ( $gmap, $overlay ); 137 $imgUrl .= $_firstimageID; 138 if ($this->getConf ( 'optionStaticMapGenerator' ) == 'remote') { 139 $imgUrl .= "&.png"; 140 } 141 } 142 143 // append dw p_render specific params and render 144 $imgUrl .= "?" . str_replace ( "px", "", $gmap ['width'] ) . "x" . str_replace ( "px", "", $gmap ['height'] ); 145 $imgUrl .= "&nolink"; 146 147 // add nocache option for selected services 148 if ($_nocache) { 149 $imgUrl .= "&nocache"; 150 } 151 152 $imgUrl .= " |".$gmap ['summary'] . " }}"; 153 154 // dbglog($imgUrl,"complete image tags is:"); 155 156 $mapid = $gmap ['id']; 157 // create a javascript parameter string for the map 158 $param = ''; 159 foreach ( $gmap as $key => $val ) { 160 $param .= is_numeric ( $val ) ? "$key: $val, " : "$key: '" . hsc ( $val ) . "', "; 161 } 162 if (! empty ( $param )) { 163 $param = substr ( $param, 0, - 2 ); 164 } 165 unset ( $gmap ['id'] ); 166 167 // create a javascript serialisation of the point data 168 $poi = ''; 169 $poitable = ''; 170 $rowId = 0; 171 if (! empty ( $overlay )) { 172 foreach ( $overlay as $data ) { 173 list ( $lat, $lon, $text, $angle, $opacity, $img ) = $data; 174 $rowId ++; 175 $poi .= ", {lat: $lat, lon: $lon, txt: '$text', angle: $angle, opacity: $opacity, img: '$img', rowId: $rowId}"; 176 $poitable .= ' 177 <tr> 178 <td class="rowId">' . $rowId . '</td> 179 <td class="icon"><img src="' . DOKU_BASE . 'lib/plugins/openlayersmap/icons/' . $img . '" alt="'. substr($img, 0, -4) . $this->getlang('alt_legend_poi').' " /></td> 180 <td class="lat" title="' . $this->getLang ( 'olmapPOIlatTitle' ) . '">' . $lat . '</td> 181 <td class="lon" title="' . $this->getLang ( 'olmapPOIlonTitle' ) . '">' . $lon . '</td> 182 <td class="txt">' . $text . '</td> 183 </tr>'; 184 } 185 $poi = substr ( $poi, 2 ); 186 } 187 if (! empty ( $gmap ['kmlfile'] )) { 188 $poitable .= ' 189 <tr> 190 <td class="rowId"><img src="' . DOKU_BASE . 'lib/plugins/openlayersmap/toolbar/kml_file.png" alt="KML file" /></td> 191 <td class="icon"><img src="' . DOKU_BASE . 'lib/plugins/openlayersmap/toolbar/kml_line.png" alt="' . $this->getlang('alt_legend_kml') .'" /></td> 192 <td class="txt" colspan="3">KML track: ' . $this->getFileName ( $gmap ['kmlfile'] ) . '</td> 193 </tr>'; 194 } 195 if (! empty ( $gmap ['gpxfile'] )) { 196 $poitable .= ' 197 <tr> 198 <td class="rowId"><img src="' . DOKU_BASE . 'lib/plugins/openlayersmap/toolbar/gpx_file.png" alt="GPX file" /></td> 199 <td class="icon"><img src="' . DOKU_BASE . 'lib/plugins/openlayersmap/toolbar/gpx_line.png" alt="' . $this->getlang('alt_legend_gpx') .'" /></td> 200 <td class="txt" colspan="3">GPX track: ' . $this->getFileName ( $gmap ['gpxfile'] ) . '</td> 201 </tr>'; 202 } 203 if (! empty ( $gmap ['geojsonfile'] )) { 204 $poitable .= ' 205 <tr> 206 <td class="rowId"><img src="' . DOKU_BASE . 'lib/plugins/openlayersmap/toolbar/geojson_file.png" alt="GeoJSON file" /></td> 207 <td class="icon"><img src="' . DOKU_BASE . 'lib/plugins/openlayersmap/toolbar/geojson_line.png" alt="' . $this->getlang('alt_legend_geojson') .'" /></td> 208 <td class="txt" colspan="3">GeoJSON track: ' . $this->getFileName ( $gmap ['geojsonfile'] ) . '</td> 209 </tr>'; 210 } 211 212 $js .= "{mapOpts:{" . $param . " },poi:[$poi]};"; 213 // unescape the json 214 $poitable = stripslashes ( $poitable ); 215 216 return array ( 217 $mapid, 218 $js, 219 $mainLat, 220 $mainLon, 221 $poitable, 222 $gmap ['summary'], 223 $imgUrl, 224 $_firstimageID 225 ); 226 } 227 228 /** 229 * 230 * @see DokuWiki_Syntax_Plugin::render() 231 */ 232 function render($mode, &$renderer, $data) { 233 // set to true after external scripts tags are written 234 static $initialised = false; 235 // incremented for each map tag in the page source so we can keep track of each map in this page 236 static $mapnumber = 0; 237 238 // dbglog($data, 'olmap::render() data.'); 239 list ( $mapid, $param, $mainLat, $mainLon, $poitable, $poitabledesc, $staticImgUrl, $_firstimage ) = $data; 240 241 if ($mode == 'xhtml') { 242 $olscript = ''; 243 $olEnable = false; 244 $gscript = ''; 245 $gEnable = $this->getConf ( 'enableGoogle' ); 246 $mqEnable = $this->getConf ( 'enableMapQuest' ); 247 $osmEnable = $this->getConf ( 'enableOSM' ); 248 $enableBing = $this->getConf ( 'enableBing' ); 249 250 $scriptEnable = ''; 251 if (! $initialised) { 252 $initialised = true; 253 // render necessary script tags 254 if ($gEnable) { 255 $gscript = '<script type="text/javascript" src="http://maps.google.com/maps/api/js?v=3.14&sensor=false"></script>'; 256 } 257 $olscript = '<script type="text/javascript" src="' . DOKU_BASE . 'lib/plugins/openlayersmap/lib/OpenLayers.js"></script>'; 258 259 $scriptEnable = '<script type="text/javascript" charset="utf-8">/*<![CDATA[*/'; 260 $scriptEnable .= $olscript ? 'olEnable = true;' : 'olEnable = false;'; 261 $scriptEnable .= 'gEnable = ' . ($gEnable ? 'true' : 'false') . ';'; 262 $scriptEnable .= 'osmEnable = ' . ($osmEnable ? 'true' : 'false') . ';'; 263 $scriptEnable .= 'mqEnable = ' . ($mqEnable ? 'true' : 'false') . ';'; 264 $scriptEnable .= 'bEnable = ' . ($enableBing ? 'true' : 'false') . ';'; 265 $scriptEnable .= 'bApiKey="' . $this->getConf ( 'bingAPIKey' ) . '";'; 266 $scriptEnable .= '/*!]]>*/</script>'; 267 } 268 $renderer->doc .= "$gscript\n$olscript\n$scriptEnable"; 269 $renderer->doc .= '<div class="olMapHelp">' . $this->locale_xhtml ( "help" ) . '</div>'; 270 if ($this->getConf ( 'enableA11y' )) { 271 $renderer->doc .= '<div id="' . $mapid . '-static" class="olStaticMap">' . p_render ( $mode, p_get_instructions ( $staticImgUrl ), $info ) . '</div>'; 272 } 273 $renderer->doc .= '<div id="' . $mapid . '-clearer" class="clearer"><p> </p></div>'; 274 if ($this->getConf ( 'enableA11y' )) { 275 // render a table of the POI for the print and a11y presentation, it is hidden using javascript 276 $renderer->doc .= '<div class="olPOItableSpan" id="' . $mapid . '-table-span"> 277 <table class="olPOItable" id="' . $mapid . '-table"> 278 <caption class="olPOITblCaption">' . $this->getLang ( 'olmapPOItitle' ) . '</caption> 279 <thead class="olPOITblHeader"> 280 <tr> 281 <th class="rowId" scope="col">id</th> 282 <th class="icon" scope="col">' . $this->getLang ( 'olmapPOIicon' ) . '</th> 283 <th class="lat" scope="col" title="' . $this->getLang ( 'olmapPOIlatTitle' ) . '">' . $this->getLang ( 'olmapPOIlat' ) . '</th> 284 <th class="lon" scope="col" title="' . $this->getLang ( 'olmapPOIlonTitle' ) . '">' . $this->getLang ( 'olmapPOIlon' ) . '</th> 285 <th class="txt" scope="col">' . $this->getLang ( 'olmapPOItxt' ) . '</th> 286 </tr> 287 </thead>'; 288 if ($poitabledesc != '') { 289 $renderer->doc .= '<tfoot class="olPOITblFooter"><tr><td colspan="5">' . $poitabledesc . '</td></tr></tfoot>'; 290 } 291 $renderer->doc .= '<tbody class="olPOITblBody">' . $poitable . '</tbody> 292 </table></div>'; 293 } 294 // render inline mapscript parts 295 $renderer->doc .= '<script type="text/javascript" charset="utf-8">/*<![CDATA[*/'; 296 $renderer->doc .= " olMapData[$mapnumber] = $param /*!]]>*/</script>"; 297 $mapnumber ++; 298 return true; 299 } elseif ($mode == 'metadata') { 300 if (! (($this->dflt ['lat'] == $mainLat) && ($thisdflt ['lon'] == $mainLon))) { 301 // render geo metadata, unless they are the default 302 $renderer->meta ['geo'] ['lat'] = $mainLat; 303 $renderer->meta ['geo'] ['lon'] = $mainLon; 304 if ($geophp = &plugin_load ( 'helper', 'geophp' )) { 305 // if we have the geoPHP helper, add the geohash 306 // fails with older php versions.. $renderer->meta['geo']['geohash'] = (new Point($mainLon,$mainLat))->out('geohash'); 307 $p = new Point ( $mainLon, $mainLat ); 308 $renderer->meta ['geo'] ['geohash'] = $p->out ( 'geohash' ); 309 } 310 } 311 312 if (($this->getConf ( 'enableA11y' )) && (! empty ( $_firstimage ))) { 313 // add map local image into relation/firstimage if not already filled and when it is a local image 314 315 global $ID; 316 $rel = p_get_metadata ( $ID, 'relation', METADATA_RENDER_USING_CACHE ); 317 $img = $rel ['firstimage']; 318 if (empty ( $img ) /* || $img == $_firstimage*/){ 319 //dbglog ( $_firstimage, 'olmap::render#rendering image relation metadata for _firstimage as $img was empty or the same.' ); 320 // This seems to never work; the firstimage entry in the .meta file is empty 321 // $renderer->meta['relation']['firstimage'] = $_firstimage; 322 323 // ... and neither does this; the firstimage entry in the .meta file is empty 324 // $relation = array('relation'=>array('firstimage'=>$_firstimage)); 325 // p_set_metadata($ID, $relation, false, false); 326 327 // ... this works 328 $renderer->internalmedia ( $_firstimage, $poitabledesc ); 329 } 330 } 331 return true; 332 } 333 return false; 334 } 335 336 /** 337 * extract parameters for the map from the parameter string 338 * 339 * @param string $str_params 340 * string of key="value" pairs 341 * @return array associative array of parameters key=>value 342 */ 343 private function _extract_params($str_params) { 344 $param = array (); 345 preg_match_all ( '/(\w*)="(.*?)"/us', $str_params, $param, PREG_SET_ORDER ); 346 // parse match for instructions, break into key value pairs 347 $gmap = $this->dflt; 348 foreach ( $param as $kvpair ) { 349 list ( $match, $key, $val ) = $kvpair; 350 $key = strtolower ( $key ); 351 if (isset ( $gmap [$key] )) { 352 if ($key == 'summary') { 353 // preserve case for summary field 354 $gmap [$key] = $val; 355 } elseif ($key == 'id') { 356 // preserve case for id field 357 $gmap [$key] = $val; 358 } else { 359 $gmap [$key] = strtolower ( $val ); 360 } 361 } 362 } 363 return $gmap; 364 } 365 366 /** 367 * extract overlay points for the map from the wiki syntax data 368 * 369 * @param string $str_points 370 * multi-line string of lat,lon,text triplets 371 * @return array multi-dimensional array of lat,lon,text triplets 372 */ 373 private function _extract_points($str_points) { 374 $point = array (); 375 // preg_match_all('/^([+-]?[0-9].*?),\s*([+-]?[0-9].*?),(.*?),(.*?),(.*?),(.*)$/um', $str_points, $point, PREG_SET_ORDER); 376 /* 377 * group 1: ([+-]?[0-9]+(?:\.[0-9]*)?) group 2: ([+-]?[0-9]+(?:\.[0-9]*)?) group 3: (.*?) group 4: (.*?) group 5: (.*?) group 6: (.*) 378 */ 379 preg_match_all ( '/^([+-]?[0-9]+(?:\.[0-9]*)?),\s*([+-]?[0-9]+(?:\.[0-9]*)?),(.*?),(.*?),(.*?),(.*)$/um', $str_points, $point, PREG_SET_ORDER ); 380 // create poi array 381 $overlay = array (); 382 foreach ( $point as $pt ) { 383 list ( $match, $lat, $lon, $angle, $opacity, $img, $text ) = $pt; 384 $lat = is_numeric ( $lat ) ? $lat : 0; 385 $lon = is_numeric ( $lon ) ? $lon : 0; 386 $angle = is_numeric ( $angle ) ? $angle : 0; 387 $opacity = is_numeric ( $opacity ) ? $opacity : 0.8; 388 // TODO validate using exist & set up default img? 389 $img = trim ( $img ); 390 $text = p_get_instructions ( $text ); 391 // dbg ( $text ); 392 $text = p_render ( "xhtml", $text, $info ); 393 // dbg ( $text ); 394 $text = addslashes ( str_replace ( "\n", "", $text ) ); 395 $overlay [] = array ( 396 $lat, 397 $lon, 398 $text, 399 $angle, 400 $opacity, 401 $img 402 ); 403 } 404 return $overlay; 405 } 406 407 /** 408 * Create a MapQuest static map API image url. 409 * 410 * @param array $gmap 411 * @param array $overlay 412 */ 413 private function _getMapQuest($gmap, $overlay) { 414 $sUrl = $this->getConf ( 'iconUrlOverload' ); 415 if (! $sUrl) { 416 $sUrl = DOKU_URL; 417 } 418 switch ($gmap ['baselyr']) { 419 case 'mapquest hybrid' : 420 $maptype = 'hyb'; 421 break; 422 case 'mapquest sat' : 423 // because sat coverage is very limited use 'hyb' instead of 'sat' so we don't get a blank map 424 $maptype = 'hyb'; 425 break; 426 case 'mapquest road' : 427 default : 428 $maptype = 'map'; 429 break; 430 } 431 $imgUrl = "http://open.mapquestapi.com/staticmap/v4/getmap?declutter=true&"; 432 if (count ( $overlay ) < 1) { 433 $imgUrl .= "?center=" . $gmap ['lat'] . "," . $gmap ['lon']; 434 // max level for mapquest is 16 435 if ($gmap ['zoom'] > 16) { 436 $imgUrl .= "&zoom=16"; 437 } else { 438 $imgUrl .= "&zoom=" . $gmap ['zoom']; 439 } 440 } 441 // use bestfit instead of center/zoom, needs upperleft/lowerright corners 442 // $bbox=$this->_calcBBOX($overlay, $gmap['lat'], $gmap['lon']); 443 // $imgUrl .= "bestfit=".$bbox['minlat'].",".$bbox['maxlon'].",".$bbox['maxlat'].",".$bbox['minlon']; 444 445 // TODO declutter option works well for square maps but not for rectangular, maybe compensate for that or compensate the mbr.. 446 $imgUrl .= "&size=" . str_replace ( "px", "", $gmap ['width'] ) . "," . str_replace ( "px", "", $gmap ['height'] ); 447 448 // TODO mapquest allows using one image url with a multiplier $NUMBER eg: 449 // $NUMBER = 2 450 // $imgUrl .= DOKU_URL."/".DOKU_PLUGIN."/".getPluginName()."/icons/".$img.",$NUMBER,C,".$lat1.",".$lon1.",0,0,0,0,C,".$lat2.",".$lon2.",0,0,0,0"; 451 if (! empty ( $overlay )) { 452 $imgUrl .= "&xis="; 453 foreach ( $overlay as $data ) { 454 list ( $lat, $lon, $text, $angle, $opacity, $img ) = $data; 455 // $imgUrl .= $sUrl."lib/plugins/openlayersmap/icons/".$img.",1,C,".$lat.",".$lon.",0,0,0,0,"; 456 $imgUrl .= $sUrl . "lib/plugins/openlayersmap/icons/" . $img . ",1,C," . $lat . "," . $lon . ","; 457 } 458 $imgUrl = substr ( $imgUrl, 0, - 1 ); 459 } 460 $imgUrl .= "&imageType=png&type=" . $maptype; 461 $imgUrl .= "&key=".$this->getConf ( 'mapquestAPIKey' ); 462 // dbglog($imgUrl,'syntax_plugin_openlayersmap_olmap::_getMapQuest: MapQuest image url is:'); 463 return $imgUrl; 464 } 465 466 /** 467 * Create a Google maps static image url w/ the poi. 468 * 469 * @param array $gmap 470 * @param array $overlay 471 */ 472 private function _getGoogle($gmap, $overlay) { 473 $sUrl = $this->getConf ( 'iconUrlOverload' ); 474 if (! $sUrl) { 475 $sUrl = DOKU_URL; 476 } 477 switch ($gmap ['baselyr']) { 478 case 'google hybrid' : 479 $maptype = 'hybrid'; 480 break; 481 case 'google sat' : 482 $maptype = 'satellite'; 483 break; 484 case 'terrain' : 485 case 'google relief' : 486 $maptype = 'terrain'; 487 break; 488 case 'google road' : 489 default : 490 $maptype = 'roadmap'; 491 break; 492 } 493 // TODO maybe use viewport / visible instead of center/zoom, 494 // see: https://developers.google.com/maps/documentation/staticmaps/index#Viewports 495 // http://maps.google.com/maps/api/staticmap?center=51.565690,5.456756&zoom=16&size=600x400&markers=icon:http://wild-water.nl/dokuwiki/lib/plugins/openlayersmap/icons/marker.png|label:1|51.565690,5.456756&markers=icon:http://wild-water.nl/dokuwiki/lib/plugins/openlayersmap/icons/marker-blue.png|51.566197,5.458966|label:2&markers=icon:http://wild-water.nl/dokuwiki/lib/plugins/openlayersmap/icons/parking.png|51.567177,5.457909|label:3&markers=icon:http://wild-water.nl/dokuwiki/lib/plugins/openlayersmap/icons/parking.png|51.566283,5.457330|label:4&markers=icon:http://wild-water.nl/dokuwiki/lib/plugins/openlayersmap/icons/parking.png|51.565630,5.457695|label:5&sensor=false&format=png&maptype=roadmap 496 $imgUrl = "http://maps.googleapis.com/maps/api/staticmap?sensor=false"; 497 $imgUrl .= "&size=" . str_replace ( "px", "", $gmap ['width'] ) . "x" . str_replace ( "px", "", $gmap ['height'] ); 498 //if (!$this->getConf( 'autoZoomMap')) { // no need for center & zoom params } 499 $imgUrl .= "¢er=" . $gmap ['lat'] . "," . $gmap ['lon']; 500 // max is 21 (== building scale), but that's overkill.. 501 if ($gmap ['zoom'] > 17) { 502 $imgUrl .= "&zoom=17"; 503 } else { 504 $imgUrl .= "&zoom=" . $gmap ['zoom']; 505 } 506 if (! empty ( $overlay )) { 507 $rowId = 0; 508 foreach ( $overlay as $data ) { 509 list ( $lat, $lon, $text, $angle, $opacity, $img ) = $data; 510 $imgUrl .= "&markers=icon%3a" . $sUrl . "lib/plugins/openlayersmap/icons/" . $img . "%7c" . $lat . "," . $lon . "%7clabel%3a" . ++ $rowId; 511 } 512 } 513 $imgUrl .= "&format=png&maptype=" . $maptype; 514 global $conf; 515 $imgUrl .= "&language=" . $conf ['lang']; 516 if ($this->getConf( 'googleAPIkey' )) { 517 $imgUrl .= "&key=" . $this->getConf( 'googleAPIkey' ); 518 } 519 // dbglog($imgUrl,'syntax_plugin_openlayersmap_olmap::_getGoogle: Google image url is:'); 520 return $imgUrl; 521 } 522 523 /** 524 * Create a Bing maps static image url w/ the poi. 525 * 526 * @param array $gmap 527 * @param array $overlay 528 */ 529 private function _getBing($gmap, $overlay) { 530 switch ($gmap ['baselyr']) { 531 case 've hybrid' : 532 case 'bing hybrid' : 533 $maptype = 'AerialWithLabels'; 534 break; 535 case 've sat' : 536 case 'bing sat' : 537 $maptype = 'Aerial'; 538 break; 539 case 've normal' : 540 case 've road' : 541 case 've' : 542 case 'bing road' : 543 default : 544 $maptype = 'Road'; 545 break; 546 } 547 $imgUrl = "http://dev.virtualearth.net/REST/v1/Imagery/Map/" . $maptype;// . "/"; 548 if ($this->getConf ( 'autoZoomMap' )) { 549 $bbox = $this->_calcBBOX ( $overlay, $gmap ['lat'], $gmap ['lon'] ); 550 //$imgUrl .= "?ma=" . $bbox ['minlat'] . "," . $bbox ['minlon'] . "," . $bbox ['maxlat'] . "," . $bbox ['maxlon']; 551 $imgUrl .= "?ma=" . $bbox ['minlat'] . "%2C" . $bbox ['minlon'] . "%2C" . $bbox ['maxlat'] . "%2C" . $bbox ['maxlon']; 552 $imgUrl .= "&dcl=1"; 553 } 554 if (strpos ( $imgUrl, "?" ) === false) 555 $imgUrl .= "?"; 556 557 //$imgUrl .= "&ms=" . str_replace ( "px", "", $gmap ['width'] ) . "," . str_replace ( "px", "", $gmap ['height'] ); 558 $imgUrl .= "&ms=" . str_replace ( "px", "", $gmap ['width'] ) . "%2C" . str_replace ( "px", "", $gmap ['height'] ); 559 $imgUrl .= "&key=" . $this->getConf ( 'bingAPIKey' ); 560 if (! empty ( $overlay )) { 561 $rowId = 0; 562 foreach ( $overlay as $data ) { 563 list ( $lat, $lon, $text, $angle, $opacity, $img ) = $data; 564 // TODO icon style lookup, see: http://msdn.microsoft.com/en-us/library/ff701719.aspx for iconStyle 565 $iconStyle = 32; 566 $rowId ++; 567 // NOTE: the max number of pushpins is 18! or we have to use POST (http://msdn.microsoft.com/en-us/library/ff701724.aspx) 568 if ($rowId == 18) { 569 break; 570 } 571 //$imgUrl .= "&pp=$lat,$lon;$iconStyle;$rowId"; 572 $imgUrl .= "&pp=$lat%2C$lon%3B$iconStyle%3B$rowId"; 573 574 } 575 } 576 global $conf; 577 $imgUrl .= "&fmt=png"; 578 $imgUrl .= "&c=" . $conf ['lang']; 579 // dbglog($imgUrl,'syntax_plugin_openlayersmap_olmap::_getBing: bing image url is:'); 580 return $imgUrl; 581 } 582 583 /** 584 * Create a static OSM map image url w/ the poi from http://staticmap.openstreetmap.de (staticMapLite) 585 * use http://staticmap.openstreetmap.de "staticMapLite" or a local version 586 * 587 * @param array $gmap 588 * @param array $overlay 589 * 590 * @todo implementation for http://ojw.dev.openstreetmap.org/StaticMapDev/ 591 */ 592 private function _getStaticOSM($gmap, $overlay) { 593 global $conf; 594 595 if ($this->getConf ( 'optionStaticMapGenerator' ) == 'local') { 596 // using local basemap composer 597 if (! $myMap = &plugin_load ( 'helper', 'openlayersmap_staticmap' )) { 598 dbglog ( $myMap, 'syntax_plugin_openlayersmap_olmap::_getStaticOSM: openlayersmap_staticmap plugin is not available.' ); 599 } 600 if (! $geophp = &plugin_load ( 'helper', 'geophp' )) { 601 dbglog ( $geophp, 'syntax_plugin_openlayersmap_olmap::_getStaticOSM: geophp plugin is not available.' ); 602 } 603 $size = str_replace ( "px", "", $gmap ['width'] ) . "x" . str_replace ( "px", "", $gmap ['height'] ); 604 605 $markers = ''; 606 if (! empty ( $overlay )) { 607 foreach ( $overlay as $data ) { 608 list ( $lat, $lon, $text, $angle, $opacity, $img ) = $data; 609 $iconStyle = substr ( $img, 0, strlen ( $img ) - 4 ); 610 $markers [] = array ( 611 'lat' => $lat, 612 'lon' => $lon, 613 'type' => $iconStyle 614 ); 615 } 616 } 617 618 switch ($gmap ['baselyr']) { 619 case 'mapnik' : 620 case 'openstreetmap' : 621 $maptype = 'openstreetmap'; 622 break; 623 case 'transport' : 624 $maptype = 'transport'; 625 break; 626 case 'landscape' : 627 $maptype = 'landscape'; 628 break; 629 case 'cycle map' : 630 $maptype = 'cycle'; 631 break; 632 case 'hike and bike map' : 633 $maptype = 'hikeandbike'; 634 break; 635 case 'mapquest hybrid' : 636 case 'mapquest road' : 637 case 'mapquest sat' : 638 $maptype = 'mapquest'; 639 break; 640 default : 641 $maptype = ''; 642 break; 643 } 644 645 $result = $myMap->getMap ( $gmap ['lat'], $gmap ['lon'], $gmap ['zoom'], $size, $maptype, $markers, $gmap ['gpxfile'], $gmap ['kmlfile'], $gmap ['geojsonfile'] ); 646 } else { 647 // using external basemap composer 648 649 // http://staticmap.openstreetmap.de/staticmap.php?center=47.000622235634,10.117187497601&zoom=5&size=500x350 650 // &markers=48.999812532766,8.3593749976708,lightblue1|43.154850037315,17.499999997306,lightblue1|49.487527053077,10.820312497573,ltblu-pushpin|47.951071133739,15.917968747369,ol-marker|47.921629720114,18.027343747285,ol-marker-gold|47.951071133739,19.257812497236,ol-marker-blue|47.180141361692,19.257812497236,ol-marker-green 651 $imgUrl = "http://staticmap.openstreetmap.de/staticmap.php"; 652 $imgUrl .= "?center=" . $gmap ['lat'] . "," . $gmap ['lon']; 653 $imgUrl .= "&size=" . str_replace ( "px", "", $gmap ['width'] ) . "x" . str_replace ( "px", "", $gmap ['height'] ); 654 655 if ($gmap ['zoom'] > 16) { 656 // actually this could even be 18, but that seems overkill 657 $imgUrl .= "&zoom=16"; 658 } else { 659 $imgUrl .= "&zoom=" . $gmap ['zoom']; 660 } 661 662 if (! empty ( $overlay )) { 663 $rowId = 0; 664 $imgUrl .= "&markers="; 665 foreach ( $overlay as $data ) { 666 list ( $lat, $lon, $text, $angle, $opacity, $img ) = $data; 667 $rowId ++; 668 $iconStyle = "lightblue$rowId"; 669 $imgUrl .= "$lat,$lon,$iconStyle%7c"; 670 } 671 $imgUrl = substr ( $imgUrl, 0, - 3 ); 672 } 673 674 $result = $imgUrl; 675 } 676 // dbglog ( $result, 'syntax_plugin_openlayersmap_olmap::_getStaticOSM: osm image url is:' ); 677 return $result; 678 } 679 680 /** 681 * Calculate the minimum bbox for a start location + poi. 682 * 683 * @param array $overlay 684 * multi-dimensional array of array($lat, $lon, $text, $angle, $opacity, $img) 685 * @param float $lat 686 * latitude for map center 687 * @param float $lon 688 * longitude for map center 689 * @return multitype:float array describing the mbr and center point 690 */ 691 private function _calcBBOX($overlay, $lat, $lon) { 692 $lats [] = $lat; 693 $lons [] = $lon; 694 foreach ( $overlay as $data ) { 695 list ( $lat, $lon, $text, $angle, $opacity, $img ) = $data; 696 $lats [] = $lat; 697 $lons [] = $lon; 698 } 699 sort ( $lats ); 700 sort ( $lons ); 701 // TODO: make edge/wrap around cases work 702 $centerlat = $lats [0] + ($lats [count ( $lats ) - 1] - $lats [0]); 703 $centerlon = $lons [0] + ($lons [count ( $lats ) - 1] - $lons [0]); 704 return array ( 705 'minlat' => $lats [0], 706 'minlon' => $lons [0], 707 'maxlat' => $lats [count ( $lats ) - 1], 708 'maxlon' => $lons [count ( $lats ) - 1], 709 'centerlat' => $centerlat, 710 'centerlon' => $centerlon 711 ); 712 } 713 714 /** 715 * Figures out the base filename of a media path. 716 * 717 * @param String $mediaLink 718 */ 719 private function getFileName($mediaLink) { 720 $mediaLink = str_replace ( '[[', '', $mediaLink ); 721 $mediaLink = str_replace ( ']]', '', $mediaLink ); 722 $mediaLink = substr ( $mediaLink, 0, - 4 ); 723 $parts = explode ( ':', $mediaLink ); 724 $mediaLink = end ( $parts ); 725 return str_replace ( '_', ' ', $mediaLink ); 726 } 727}