xref: /plugin/openlayersmap/script.js (revision 2a5c9dbfd2b5fc70300d709a4bdec43d8e68b1ce)
1/*
2 * Copyright (c) 2008-2011 Mark C. Prins <mc.prins@gmail.com>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16
17/**
18 * @fileoverview Javascript voor OpenLayers plugin.
19 *
20 * @requires {lib/OpenLayers.js} or other full openlayers build
21 * @author Mark C. Prins <mc.prins@gmail.com>
22 *
23 */
24
25/**
26 * Openlayers selectcontrol.
27 *
28 * @type {OpenLayers.Control.SelectFeature}
29 * @private
30 */
31var selectControl,
32/**
33 * Openlayers bounds used for managing the map extent.
34 *
35 * @type {OpenLayers.Bounds}
36 * @private
37 */
38extent;
39
40/**
41 * handle feature select event.
42 *
43 * @param {OpenLayers.Feature.Vector}
44 *            the selected feature
45 */
46function onFeatureSelect(feature) {
47	var selectedFeature = feature;
48	// 'this' is selectFeature control
49	var pPos = selectedFeature.geometry.getBounds().getCenterLonLat();
50	// != OpenLayers.Geometry.Point
51	if (selectedFeature.geometry.CLASS_NAME === "OpenLayers.Geometry.LineString") {
52		try {
53			// for lines make the popup show at the cursor position
54			pPos = feature.layer.map
55					.getLonLatFromViewPortPx(this.handlers.feature.evt.xy);
56		} catch (anErr) {
57			OpenLayers.Console
58					.warn("unable to get event position; reverting to boundingbox center.");
59			pPos = selectedFeature.geometry.getBounds().getCenterLonLat();
60		}
61	}
62
63	var pContent = "";
64	if (feature.data.rowId !== undefined) {
65		pContent += "<div style=''>" + feature.data.rowId + ": </div>";
66	}
67	if (feature.data.name !== undefined) {
68		pContent += "<div style=''>" + feature.data.name + "</div>";
69	}
70	if (feature.data.ele !== undefined) {
71		pContent += "<div style=''>elevation: " + feature.data.ele + "</div>";
72	}
73	if (feature.data.type !== undefined) {
74		pContent += "<div style=''>" + feature.data.type + "></div>";
75	}
76	if (feature.data.time !== undefined) {
77		pContent += "<div style=''>time: " + feature.data.time + "</div>";
78	}
79	if (feature.data.description !== undefined) {
80		pContent += "<div style=''>" + feature.data.description + "</div>";
81	}
82
83	if (pContent.length > 0) {
84		// only show when there is something to show...
85		var popup = new OpenLayers.Popup.FramedCloud("olPopup", pPos, null,
86				pContent, null, true, function() {
87					selectControl.unselect(selectedFeature);
88				});
89		feature.popup = popup;
90		feature.layer.map.addPopup(popup);
91	}
92}
93
94/**
95 * handle feature unselect event. remove & destroy the popup.
96 *
97 * @param {OpenLayers.Feature.Vector}
98 *            the un-selected feature
99 */
100function onFeatureUnselect(feature) {
101	if (feature.popup !== null) {
102		feature.layer.map.removePopup(feature.popup);
103		feature.popup.destroy();
104		feature.popup = null;
105	}
106}
107
108/** init. */
109function olInit() {
110	var _i = 0;
111	// hide the table(s) with POI by giving it a print only style
112	var tbls = getElementsByClass('olPOItableSpan', null, null);
113	for (_i = 0; _i < tbls.length; _i++) {
114		// tbls[i].style.display = 'none';
115		tbls[_i].className += ' olPrintOnly';
116	}
117	// hide the static map image(s) by giving it a print only style
118	var statImgs = getElementsByClass('olStaticMap', null, null);
119	for (_i = 0; _i < statImgs.length; _i++) {
120		// statImgs[i].style.display = 'none';
121		statImgs[_i].className += ' olPrintOnly';
122	}
123}
124
125/**
126 * creates a DocumentFragment to insert into the dom.
127 *
128 * @param mapid
129 *            id for the map div
130 * @param width
131 *            width for the map div
132 * @param height
133 *            height for the map div
134 * @returns a {DocumentFragment} element that can be injected into the dom
135 */
136function olCreateMaptag(mapid, width, height) {
137	var mEl = '<div id="'
138			+ mapid
139			+ '-olContainer" class="olContainer olWebOnly">'
140			+ '<div id="'
141			+ mapid
142			+ '-olToolbar" class="olToolbar"></div>'
143			+ '<div class="clearer"></div>'
144			+ '<div id="'
145			+ mapid
146			+ '" style="width:'
147			+ width
148			+ ';height:'
149			+ height
150			+ ';" class="olMap"></div>'
151			+ '<div id="'
152			+ mapid
153			+ '-olStatusBar" class="olStatusBarContainer">'
154			+ '<div id="'
155			+ mapid
156			+ '-statusbar-scale" class="olStatusBar olStatusBarScale">scale</div>'
157			+ '<div id="'
158			+ mapid
159			+ '-statusbar-link" class="olStatusBar olStatusBarPermalink"><a href="" id="'
160			+ mapid
161			+ '-statusbar-link-ref">map link</a></div>'
162			+ '<div id="'
163			+ mapid
164			+ '-statusbar-mouseposition" class="olStatusBar olStatusBarMouseposition"></div>'
165			+ '<div id="'
166			+ mapid
167			+ '-statusbar-projection" class="olStatusBar olStatusBarProjection">proj</div>'
168			+ '<div id="' + mapid
169			+ '-statusbar-text" class="olStatusBar olStatusBarText">txt</div>'
170			+ '</div>\n</div>',
171	// fragment
172	frag = document.createDocumentFragment(),
173	// temp node
174	temp = document.createElement('div');
175	temp.innerHTML = mEl;
176	while (temp.firstChild) {
177		frag.appendChild(temp.firstChild);
178	}
179	return frag;
180}
181
182/**
183 * create the map based on the params given.
184 *
185 * @param {Object}mapOpts
186 *            MapOptions hash {id:'olmap', width:500px, height:500px,
187 *            lat:6710200, lon:506500, zoom:13, toolbar:1, statusbar:1,
188 *            controls:1, poihoverstyle:1, baselyr:'', kmlfile:'', gpxfile:'',
189 *            summary:''}
190 * @param {Array}OLmapPOI
191 *            array with POI's [ {lat:6710300,lon:506000,txt:'instap
192 *            punt',angle:180,opacity:.9,img:'', rowId:n},... ]);
193 *
194 */
195function createMap(mapOpts, OLmapPOI) {
196	if (!olEnable) {
197		return;
198	}
199	var DocBase = DOKU_BASE;
200
201	OpenLayers.IMAGE_RELOAD_ATTEMPTS = 4;
202	OpenLayers.Util.onImageLoadErrorColor = 'pink';
203	OpenLayers.Util.onImageLoadError = function() {
204		/* transparent gif */
205		// IE 8 complains w/ stack overflow... this.src =
206		// "data:image/gif;base64,R0lGODlhAQABAAD/ACwAAAAAAQABAAACADs=";
207		this.src = DocBase + "lib/plugins/openlayersmap/lib/img/blank.gif";
208	};
209
210	// OpenLayers.Layer.Vector.prototype.renderers = ["SVG2", "VML", "Canvas"];
211
212	// find map element location
213	var cleartag = document.getElementById(mapOpts.id + '-clearer');
214	if (cleartag === null) {
215		return;
216	}
217	// create map element and add to document
218	var fragment = olCreateMaptag(mapOpts.id, mapOpts.width, mapOpts.height);
219	cleartag.parentNode.insertBefore(fragment, cleartag);
220
221	/** dynamic map extent. */
222	var extent = new OpenLayers.Bounds(),
223
224	/** map. */
225	m = new OpenLayers.Map(mapOpts.id, {
226		projection : new OpenLayers.Projection('EPSG:900913'),
227		displayProjection : new OpenLayers.Projection('EPSG:4326'),
228		units : 'm',
229		maxResolution : 156543.0339,
230		maxExtent : new OpenLayers.Bounds(-20037508.34, -20037508.34,
231				20037508.34, 20037508.34),
232		numZoomLevels : 19,
233		// panDuration : 100,
234		controls : [ /* new OpenLayers.Control.LoadingPanel(), */
235		new OpenLayers.Control.KeyboardDefaults(),
236				new OpenLayers.Control.Navigation({
237					dragPanOptions : {
238						enableKinetic : true
239					}
240				}), new OpenLayers.Control.ScaleLine({
241					geodesic : true
242				}) ],
243		theme : null
244	});
245	if (osmEnable) {
246		/* add OSM map layers */
247		m.addLayer(new OpenLayers.Layer.OSM("OpenStreetMap"), {
248			transitionEffect : 'resize',
249			visibility : false
250		});
251
252		m
253				.addLayer(
254						new OpenLayers.Layer.OSM(
255								"t@h",
256								[
257										"http://a.tah.openstreetmap.org/Tiles/tile/${z}/${x}/${y}.png",
258										"http://b.tah.openstreetmap.org/Tiles/tile/${z}/${x}/${y}.png",
259										"http://c.tah.openstreetmap.org/Tiles/tile/${z}/${x}/${y}.png" ]),
260						{
261							transitionEffect : 'resize',
262							visibility : false
263						});
264
265		m
266				.addLayer(
267						new OpenLayers.Layer.OSM(
268								"cycle map",
269								[
270										// "http://andy.sandbox.cloudmade.com/tiles/cycle/${z}/${x}/${y}.png",
271										"http://a.tile.opencyclemap.org/cycle/${z}/${x}/${y}.png",
272										"http://b.tile.opencyclemap.org/cycle/${z}/${x}/${y}.png",
273										"http://c.tile.opencyclemap.org/cycle/${z}/${x}/${y}.png" ]),
274						{
275							transitionEffect : 'resize',
276							attribution : 'Data CC-By-SA <a href="http://openstreetmap.org/" target="_blank">OpenStreetMap</a>, '
277									+ 'Tiles <a href="http://opencyclemap.org/" target="_blank">OpenCycleMap</a>'
278									+ '<img src="http://opencyclemap.org/favicon.ico" heigth="16" width="16"/>',
279							visibility : false
280						});
281
282		m
283				.addLayer(new OpenLayers.Layer.OSM(
284						"cloudmade map",
285						"http://tile.cloudmade.com/2f59745a6b525b4ebdb100891d5b6711/3/256/${z}/${x}/${y}.png",
286						{
287							transitionEffect : 'resize',
288							visibility : false
289						}));
290
291		m.addLayer(new OpenLayers.Layer.OSM("hike and bike map",
292				"http://toolserver.org/tiles/hikebike/${z}/${x}/${y}.png", {
293					transitionEffect : 'resize',
294					visibility : false
295				}));
296	}
297	/*
298	 * add MapQuest map layers, see:
299	 * http://developer.mapquest.com/web/products/open/map
300	 */
301	if (mqEnable) {
302		m
303				.addLayer(new OpenLayers.Layer.OSM(
304						"mapquest road",
305						[
306								"http://otile1.mqcdn.com/tiles/1.0.0/osm/${z}/${x}/${y}.png",
307								"http://otile2.mqcdn.com/tiles/1.0.0/osm/${z}/${x}/${y}.png",
308								"http://otile3.mqcdn.com/tiles/1.0.0/osm/${z}/${x}/${y}.png",
309								"http://otile4.mqcdn.com/tiles/1.0.0/osm/${z}/${x}/${y}.png" ],
310						{
311							transitionEffect : 'resize',
312							attribution : 'Data CC-By-SA <a href="http://openstreetmap.org/" target="_blank">OpenStreetMap</a>, '
313									+ 'Tiles <a href="http://www.mapquest.com/" target="_blank">MapQuest</a>'
314									+ '<img src="http://developer.mapquest.com/content/osm/mq_logo.png" heigth="14" width="14" alt="logo"/>',
315							visibility : false
316						}));
317		// note that global coverage is provided at zoom levels 0-11. Zoom
318		// Levels 12+ are provided only in the United States (lower 48).
319		m
320				.addLayer(new OpenLayers.Layer.OSM(
321						"mapquest aerial",
322						[
323								"http://oatile1.mqcdn.com/naip/${z}/${x}/${y}.jpg",
324								"http://oatile2.mqcdn.com/naip/${z}/${x}/${y}.jpg",
325								"http://oatile3.mqcdn.com/naip/${z}/${x}/${y}.jpg",
326								"http://oatile4.mqcdn.com/naip/${z}/${x}/${y}.jpg" ],
327						{
328							transitionEffect : 'resize',
329							attribution : 'Tiles Courtesy of <a href="http://www.mapquest.com/" target="_blank">MapQuest</a>'
330									+ '<img src="http://developer.mapquest.com/content/osm/mq_logo.png" heigth="16" width="16">',
331							visibility : false,
332							numZoomLevels : 12
333						}));
334	}
335
336	/* open aerial map layers */
337	/*
338	 * turn this off; project is asleep:
339	 * https://sourceforge.net/tracker/?func=detail&aid=2897327&group_id=239475&atid=1110186
340	 * m.addLayer(new OpenLayers.Layer.XYZ("OpenAerialMap",
341	 * "http://tile.openaerialmap.org/tiles/1.0.0/openaerialmap-900913/${z}/${x}/${y}.jpg",
342	 * {name: "OpenStreetMap", attribution: "Data CC-By by <a
343	 * href='http://www.openaerialmap.org/licensing/'>OpenAerialMap</a>",
344	 * sphericalMercator: true, transitionEffect: 'resize'} ));
345	 */
346
347	if (gEnable) {
348		/* load google maps */
349		try {
350			m.addLayer(new OpenLayers.Layer.Google("google relief", {
351				type : google.maps.MapTypeId.TERRAIN,
352				// transitionEffect : 'resize',
353				numZoomLevels : 16,
354				animationEnabled : true,
355				visibility : false
356			}));
357			m.addLayer(new OpenLayers.Layer.Google("google sat", {
358				type : google.maps.MapTypeId.SATELLITE,
359				// transitionEffect : 'resize',
360				// numZoomLevels : 22,
361				animationEnabled : true,
362				visibility : false
363			}));
364			m.addLayer(new OpenLayers.Layer.Google("google hybrid", {
365				type : google.maps.MapTypeId.HYBRID,
366				// transitionEffect : 'resize',
367				// numZoomLevels : 20,
368				animationEnabled : true,
369				visibility : false
370			}));
371			m.addLayer(new OpenLayers.Layer.Google("google road", {
372				// transitionEffect : 'resize',
373				// numZoomLevels : 20,
374				animationEnabled : true,
375				visibility : false
376			}));
377		} catch (ol_err1) {
378			Openlayers.Console.userError('Error loading Google maps' + ol_err1);
379		}
380	}
381
382	// if (yEnable) {
383	// try {
384	// m.addLayer(new OpenLayers.Layer.Yahoo("yahoo", {
385	// 'type' : YAHOO_MAP_HYB,
386	// 'sphericalMercator' : true,
387	// transitionEffect : resize
388	// }));
389	// } catch (ol_err2) {
390	// }
391	// }
392
393	if (veEnable) {
394		try {
395			m.addLayer(new OpenLayers.Layer.VirtualEarth("ve", {
396				type : VEMapStyle.Hybrid,
397				sphericalMercator : true,
398				transitionEffect : 'resize',
399				visibility : false
400			}));
401		} catch (ol_err3) {
402			Openlayers.Console.userError('Error loading Virtual Earth maps: '
403					+ ol_err3);
404		}
405	}
406
407	if (bEnable && bApiKey !== '') {
408		try {
409			/* add Bing tiles */
410			m.addLayer(new OpenLayers.Layer.Bing({
411				key : bApiKey,
412				type : "Road",
413				name : 'bing road',
414				transitionEffect : 'resize',
415				visibility : false
416			}));
417			m.addLayer(new OpenLayers.Layer.Bing({
418				key : bApiKey,
419				type : "Aerial",
420				name : 'bing sat',
421				transitionEffect : 'resize',
422				visibility : false
423			}));
424			m.addLayer(new OpenLayers.Layer.Bing({
425				key : bApiKey,
426				type : "AerialWithLabels",
427				name : "bing hybrid",
428				transitionEffect : 'resize',
429				visibility : false
430			}));
431		} catch (ol_errBing) {
432			Openlayers.Console.userError('Error loading Bing maps: '
433					+ ol_errBing);
434		}
435	}
436
437	m.setCenter(new OpenLayers.LonLat(mapOpts.lon, mapOpts.lat).transform(
438			m.displayProjection, m.projection), mapOpts.zoom);
439	extent.extend(m.getExtent());
440
441	// change/set alternative baselyr
442	try {
443		m.setBaseLayer(((m.getLayersByName(mapOpts.baselyr))[0]));
444	} catch (ol_err4) {
445		m.setBaseLayer(m.layers[0]);
446	}
447
448	if (mapOpts.controls === 1) {
449		/* add base controls to map */
450		m.addControl(new OpenLayers.Control.LayerSwitcher({
451			roundedCorner : false,
452			roundedCornerColor : null
453		}));
454		m.addControl(new OpenLayers.Control.PanZoomBar());
455		m.addControl(new OpenLayers.Control.Graticule({
456			visible : false
457		}));
458
459		// add hillshade, since this is off by default only add when we have a
460		// layerswitcher
461		m.addLayer(new OpenLayers.Layer.OSM("Hillshade",
462				"http://toolserver.org/~cmarqu/hill/${z}/${x}/${y}.png", {
463					transitionEffect : 'resize',
464					isBaseLayer : false,
465					transparent : true,
466					visibility : false,
467					displayOutsideMaxExtent : true,
468					attribution : ''
469				}));
470
471		m.addControl(new OpenLayers.Control.OverviewMap({
472			size : new OpenLayers.Size(140, 140),
473			mapOptions : {
474				theme : null
475			},
476			layers : [ m.baseLayer.clone() ],
477			minRectSize : 10
478		}));
479	}
480
481	if (mapOpts.statusbar === 1) {
482		// statusbar control: permalink
483		m.addControl(new OpenLayers.Control.Permalink(mapOpts.id
484				+ '-statusbar-link-ref'));
485		// statusbar control: mouse pos.
486		// TODO kijken naar afronding met aNumber.toFixed(0)
487		m.addControl(new OpenLayers.Control.MousePosition({
488			'div' : OpenLayers.Util.getElement(mapOpts.id
489					+ '-statusbar-mouseposition')
490		}));
491		// statusbar control: scale
492		m.addControl(new OpenLayers.Control.Scale(mapOpts.id
493				+ '-statusbar-scale'));
494		// statusbar control: attribution
495		m.addControl(new OpenLayers.Control.Attribution({
496			'div' : OpenLayers.Util.getElement(mapOpts.id + '-statusbar-text')
497		}));
498		// statusbar control: projection
499		OpenLayers.Util.getElement(mapOpts.id + '-statusbar-projection').innerHTML = m.displayProjection;
500	} else {
501		OpenLayers.Util.getElement(mapOpts.id + '-olStatusBar').display = 'none';
502	}
503
504	if (mapOpts.toolbar === 1) {
505		// add buttons + panel
506		var zoomin = new OpenLayers.Control.ZoomBox({
507			title : "Zoom in"
508		}), /**/zoomout = new OpenLayers.Control.ZoomBox({
509			out : true,
510			title : "Zoom uit",
511			displayClass : "olControlZoomOut"
512		}), /**/pan = new OpenLayers.Control.DragPan({
513			title : "Verschuif"
514		}), /* do "nothing" button... */info = new OpenLayers.Control.Button({
515			type : OpenLayers.Control.TYPE_TOOL,
516			displayClass : "olControlFeatureInfo"
517		/* , trigger : selectControl.activate() */
518		}), /* navigation history */
519		nav = new OpenLayers.Control.NavigationHistory();
520		m.addControl(nav);
521		var panel = new OpenLayers.Control.Panel({
522			defaultControl : pan,
523			displayClass : "olToolbar",
524			"div" : OpenLayers.Util.getElement(mapOpts.id + "-olToolbar")
525		});
526		panel
527				.addControls([ zoomin, zoomout, pan, info, nav.next,
528						nav.previous ]);
529		// panel.addControls([ nav.next, nav.previous ]);
530		m.addControl(panel);
531	} else {
532		OpenLayers.Util.getElement(mapOpts.id + '-olToolbar').display = 'none';
533	}
534
535	if (OLmapPOI.length > 0) {
536		var markers = new OpenLayers.Layer.Vector(
537				"POI",
538				{
539					styleMap : new OpenLayers.StyleMap(
540							{
541								"default" : {
542									externalGraphic : "${img}",
543									graphicHeight : 16,
544									graphicWidth : 16,
545									graphicXOffset : 0,
546									graphicYOffset : -8,
547									graphicOpacity : "${opacity}",
548									rotation : "${angle}",
549									backgroundGraphic : DocBase
550											+ "lib/plugins/openlayersmap/icons/marker_shadow.png",
551									backgroundXOffset : 0,
552									backgroundYOffset : -4,
553									backgroundRotation : "${angle}",
554									pointRadius : 10,
555									labelXOffset : 8,
556									labelYOffset : 8,
557									labelAlign : "lb",
558									label : "${label}",
559									// fontColor : "",
560									fontFamily : "monospace",
561									fontSize : "12px",
562									fontWeight : "bold"
563								},
564								"select" : {
565									cursor : "crosshair",
566									externalGraphic : DocBase
567											+ "lib/plugins/openlayersmap/icons/marker-red.png",
568									graphicHeight : 16,
569									graphicWidth : 16,
570									graphicXOffset : 0,
571									graphicYOffset : -8,
572									graphicOpacity : 1.0,
573									rotation : "${angle}"
574								}
575							}),
576					isBaseLayer : false,
577					rendererOptions : {
578						yOrdering : true
579					}
580				});
581
582		m.addLayer(markers);
583		var features = [];
584		var lonLat;
585		for ( var j = 0; j < OLmapPOI.length; j++) {
586			var feat = new OpenLayers.Feature.Vector(
587					new OpenLayers.Geometry.Point(OLmapPOI[j].lon,
588							OLmapPOI[j].lat).transform(m.displayProjection,
589							m.projection), {
590						angle : OLmapPOI[j].angle,
591						opacity : OLmapPOI[j].opacity,
592						img : DocBase + "lib/plugins/openlayersmap/icons/"
593								+ OLmapPOI[j].img,
594						label : OLmapPOI[j].rowId
595					});
596			feat.data = {
597				name : OLmapPOI[j].txt,
598				rowId : OLmapPOI[j].rowId
599			};
600			features.push(feat);
601		}
602		markers.addFeatures(features);
603		extent.extend(markers.getDataExtent());
604		m.zoomToExtent(extent);
605	}
606
607	/* GPX layer */
608	if (mapOpts.gpxfile.length > 0) {
609		var layerGPX = new OpenLayers.Layer.GML("GPS route", DocBase
610				+ "lib/exe/fetch.php?media=" + mapOpts.gpxfile, {
611			format : OpenLayers.Format.GPX,
612			formatOptions : {
613				extractWaypoints : true,
614				extractTracks : true,
615				extractStyles : true,
616				extractAttributes : true,
617				handleHeight : true,
618				maxDepth : 3
619			},
620			style : {
621				strokeColor : "#0000FF",
622				strokeWidth : 3,
623				strokeOpacity : 0.7,
624				pointRadius : 4,
625				fillColor : "#0099FF",
626				fillOpacity : 0.7
627			/*
628			 * , label:"${name}"
629			 */},
630			projection : new OpenLayers.Projection("EPSG:4326")
631		});
632		m.addLayer(layerGPX);
633		layerGPX.events.register('loadend', m, function() {
634			extent.extend(layerGPX.getDataExtent());
635			m.zoomToExtent(extent);
636		});
637
638	}
639
640	/* KML layer */
641	if (mapOpts.kmlfile.length > 0) {
642		var layerKML = new OpenLayers.Layer.GML("KML file", DocBase
643				+ "lib/exe/fetch.php?media=" + mapOpts.kmlfile, {
644			format : OpenLayers.Format.KML,
645			formatOptions : {
646				extractStyles : true,
647				extractAttributes : true,
648				maxDepth : 3
649			},
650			style : {
651				label : "${name}"
652			},
653			projection : new OpenLayers.Projection("EPSG:4326")
654		});
655		m.addLayer(layerKML);
656		layerKML.events.register('loadend', m, function() {
657			extent.extend(layerKML.getDataExtent());
658			m.zoomToExtent(extent);
659		});
660	}
661
662	// selectcontrol for layers
663	if ((m.getLayersByClass('OpenLayers.Layer.GML').length > 0)
664			|| m.getLayersByClass('OpenLayers.Layer.Vector').length > 0) {
665		selectControl = new OpenLayers.Control.SelectFeature((m
666				.getLayersByClass('OpenLayers.Layer.Vector')).concat(m
667				.getLayersByClass('OpenLayers.Layer.GML')), {
668			hover : mapOpts.poihoverstyle,
669			onSelect : onFeatureSelect,
670			onUnselect : onFeatureUnselect
671		});
672		m.addControl(selectControl);
673		selectControl.activate();
674	}
675
676	return m;
677}
678
679/**
680 * ol api flag.
681 *
682 * @type {Boolean}
683 */
684var olEnable = false,
685/**
686 * MapQuest tiles flag.
687 *
688 * @type {Boolean}
689 */
690mqEnable = false,
691/**
692 * google map api flag.
693 *
694 * @type {Boolean}
695 */
696gEnable = false,
697/**
698 * virtual earth map api flag.
699 *
700 * @type {Boolean}
701 */
702veEnable = false,
703/**
704 * Bing tiles flag.
705 *
706 * @type {Boolean}
707 */
708bEnable = false,
709/**
710 * Bing API key.
711 *
712 * @type {String}
713 */
714bApiKey = '',
715/**
716 * OSM tiles flag.
717 *
718 * @type {Boolean}
719 */
720osmEnable = true;
721/**
722 * yahoo map api flag.
723 *
724 * @type {Boolean}
725 */
726// yEnable = false;
727/* register olInit to run with onload event. */
728addInitEvent(olInit);
729