1/**
2 * DokuWiki Plugin Googlemaps3
3 *
4 * @license		GPL 3 (https://www.gnu.org/licenses/gpl-3.0.html)
5 * @author		Bernard Condrau <bernard@condrau.com>
6 * @version		2021-05-12, for Google Maps v3 API and DokuWiki Hogfather
7 * @see			https://www.dokuwiki.org/plugin:googlemaps3
8 * @see			https://www.dokuwiki.org/plugin:googlemaps
9 *
10 * Complete rewrite of Christopher Smith's Google Maps Plugin from 2008 with additional functionality
11 * script.js	javascript map initialisation
12 */
13
14// initialise all maps, corresponding markers, and kml
15function initMap() {
16	var nodes = document.body.getElementsByTagName('div');
17
18	var i=0;
19	for (var j=0; j<nodes.length; j++) {
20		if (nodes[j].className.match(/\bgooglemaps3\b/)) {
21			googlemaps3[i++].node = nodes[j];
22		}
23	}
24	for (i=0; i<googlemaps3.length; i++) {
25		googlemaps3[i].map = new google.maps.Map(googlemaps3[i].node, {
26			center: {lat: googlemaps3[i].lat, lng: googlemaps3[i].lng},
27			zoom: googlemaps3[i].zoom,
28			mapTypeId: googlemaps3[i].type,
29			disableDefaultUI: googlemaps3[i].disableDefaultUI,
30			zoomControl: googlemaps3[i].zoomControl,
31			mapTypeControl: googlemaps3[i].mapTypeControl,
32			scaleControl: googlemaps3[i].scaleControl,
33			streetViewControl: googlemaps3[i].streetViewControl,
34			rotateControl: googlemaps3[i].rotateControl,
35			fullscreenControl: googlemaps3[i].fullscreenControl,
36		});
37		if (googlemaps3[i].address != '') {
38			pantoMapAddress(googlemaps3[i].map,googlemaps3[i].address);
39		}
40		if (googlemaps3[i].marker && googlemaps3[i].marker.length > 0) {
41			for (j=0; j<googlemaps3[i].marker.length; j++) if (googlemaps3[i].marker[j].lat=='address') {
42				attachAddressMarker(googlemaps3[i].map,googlemaps3[i].marker[j]);
43			} else {
44				attachMarker(googlemaps3[i].map,googlemaps3[i].marker[j]);
45			}
46		}
47		if (googlemaps3[i].kml != 'off') {
48			const georssLayer = new google.maps.KmlLayer({url: googlemaps3[i].kml, map: googlemaps3[i].map,});
49		}
50	}
51	function attachMarker(map, options, position) {
52		// location from latlng coordinates
53		if (position == null) {
54			position = {lat: options.lat, lng: options.lng};
55			origin = options.lat+','+options.lng;
56		// location from address
57		} else {
58			origin = options.lng;
59		}
60		const marker = new google.maps.Marker({
61			position: position,
62			map: map,
63			title: options.title,
64			icon: options.icon,
65		});
66		if (options.img || options.info || options.dir) {
67			markerInfo =
68				'<div id="googlemaps3marker'+options.markerID+'" class="googlemaps3 markerinfo"'+(options.width ? ' style="width: '+options.width+'"' : '')+'>' +
69					(options.title ? '<h3>'+options.title+'</h3>' : '') +
70					(options.img ? '<img src="'+options.img+'" style="max-width: 100%">' : '') +
71					(options.info ? '<p>'+options.info+'</p>' : '') +
72					(options.dir ? '<p><a href="https://www.google.com/maps/dir/?api=1&destination='+origin+'" target="_blank">'+options.dir+'</a></p>' : '') +
73				'</div>';
74			const infoWindow = new google.maps.InfoWindow({content: markerInfo,});
75			marker.addListener('click', () => {
76				infoWindow.open(map, marker);
77			});
78		}
79	}
80	function attachAddressMarker(map, options) {
81		const geocoder = new google.maps.Geocoder();
82		geocoder.geocode({'address': options.lng}, function(results, status) {
83			if (status=='OK') {
84				attachMarker(map, options, results[0].geometry.location);
85			} else {
86				console.log('Googlemaps3 Plugin: geocode failed, status='+status);
87			}
88		});
89	}
90	function pantoMapAddress(map, address) {
91		const geocoder = new google.maps.Geocoder();
92		geocoder.geocode({'address': address}, function(results, status) {
93			if (status=='OK') {
94				map.setCenter(results[0].geometry.location);
95			} else {
96				console.log('Googlemaps3 Plugin: geocode failed, status='+status);
97			}
98		});
99	}
100}
101