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