Widget:MapTest/JS

var cursorIcon = L.icon({               iconUrl: 'http://sotawiki.net/images/2/29/Cursor-down.png',                iconSize: [46, 46], // size of the icon                iconAnchor: [23, 46], // point of the icon which will correspond to marker's location                popupAnchor: [0, -30] // point from which the popup should open relative to the iconAnchor            });

var maps = document.getElementsByClassName("sotamap");

for(var i=0; i< maps.length; i++) {   var mapElem = maps[i]; var sceneid = 0; var continentid = 1; var centerX = 150; var centerY = 150 var zoom = 1; var hidezoom = 0; var hideattribution = 0; if (mapElem.hasAttribute('data-continentid')) {       continentid = Number(mapElem.dataset.continentid); }   if (mapElem.hasAttribute('data-sceneid')) {       sceneid = Number(mapElem.dataset.sceneid); }   if (mapElem.hasAttribute('data-centerX')) {       centerX = Number(mapElem.dataset.centerx); }   if (mapElem.hasAttribute('data-centerY')) {       centerY = Number(mapElem.dataset.centery); }   if (mapElem.hasAttribute('data-zoom')) {       zoom = Number(mapElem.dataset.zoom); }   if (mapElem.hasAttribute('data-hidezoom')) {       hidezoom = Number(mapElem.dataset.hidezoom); }   if (mapElem.hasAttribute('data-hideattribution')) {       hideattribution = Number(mapElem.dataset.hideattribution); }   var mapLoc = 'http://fenyx4.com/sota/map/' + continentid + '/{z}/{x}/{y}.png'; if(sceneid != 0) {       mapLoc = 'http://fenyx4.com/sota/map/' + continentid + '/scenes/' + sceneid + '/{z}/{x}/{y}.png'; }   var center = GetTransformedCoordinates(continentid, sceneid, [centerY, 0, centerX]); //var divid = "sotamap"+i; //mapElem.id = divid; var map = L.map(mapElem, {       crs:  	L.CRS.Simple,        zoom: 6,        minZoom: 1,        maxZoom: 6,        continuousWorld: true,        noWrap: true,        tms: true,        zoomControl:hidezoom != 1    }).setView(center, zoom); L.tileLayer(mapLoc, {       zoom: 6,        minZoom: 1,        maxZoom: 6,        continuousWorld: true,        noWrap: true,        tms: true    }).addTo(map); if(hideattribution == 1) {       map.attributionControl.setPrefix(''); }

var markerArray = GetMarkerArray(mapElem,continentid,sceneid); for (var markerIdx in markerArray) { markerArray[markerIdx].addTo(map); }   //Add layer groups var baseMaps = {}; var overlayMaps = {};

var layerControl = L.control.layers(baseMaps, overlayMaps); var addLayerControl = false; //Find all the groups and then add all the markers that are in them for (var j = 0; j < mapElem.childNodes.length; j++) { if (mapElem.childNodes[j].className != undefined &&            mapElem.childNodes[j].className.indexOf("map-group") > -1) { addLayerControl = true; var gElem = mapElem.childNodes[j]; var markerArr = GetMarkerArray(gElem, continentid,sceneid);

var resultLayerGroup = L.layerGroup(markerArr); layerControl.addOverlay(resultLayerGroup, gElem.dataset.name); resultLayerGroup.addTo(map); }           }    if(addLayerControl) {       layerControl.addTo(map); } }

function GetMarkerArray(parentElem, continentid) { var markerArr = []; for (var j = 0; j < parentElem.childNodes.length; j++) { if (parentElem.childNodes[j].className != undefined &&            parentElem.childNodes[j].className.indexOf("map-marker") > -1) { var mElem = parentElem.childNodes[j];

var coords = $.csv.toArrays(mElem.dataset.coordinate, {               separator:';', // sets a custom field separator character            }); var point; if(sceneid > 0) {               point = [coords[0][3], 0, coords[0][2]] }           else {               point = [coords[0][2], 0, coords[0][1]]; }                      var marker = L.marker(GetTransformedCoordinates(continentid, sceneid, point),{                title: mElem.dataset.name,                icon: cursorIcon}); marker.bindPopup("" + mElem.dataset.name + "") .on('click', marker.openPopup.bind(marker )); markerArr[markerArr.length] = marker;

}           }    return markerArr; }

function GetTransformedCoordinates(continentid, sceneid, point) { if(sceneid > 0) {   return _(this).mapScene(continentid, sceneid, point); } else {   return _(this).mapContinent(continentid, point); } }