var paramArr = []; var paramInfoArr = []; var latLngArr = []; var localSearch = new GlocalSearch(); var thisMap = ''; var temp = ''; function map(){ /* properties */ this.divId = ''; this.divObj = ''; this.mapWidth = '600'; this.mapHeight = '400'; this.dimenTypeH = 'px'; this.dimenTypeW = 'px'; this.divBg = 'buttonShadow'; this.mapObj = ''; this.mapType = '1'; //1 = simple point ploting,2 = tracking,3 = fencing this.paramArr = paramArr; this.paramInfoArr = paramInfoArr; // extra information like dates etc this.latLngArr = latLngArr; this.showCP = true; //enable - disable control panel at bottom of map this.postcodeSearch = true; this.allowSave = true; this.showInfo = true; this.showInfoClick = false; this.showInfoOnLoad = false; this.allowDraging = true; this.customIcon = true; this.customNumberIcon = false; this.customNum = 0; this.temp = ''; this.polyObj = ''; this.postCode = ''; this.zoomLevel = 12; this.incModuleName = ''; this.addressDetails = ''; this.showLiveAddr = false; /* properties */ /* functions */ this.printMap = printMap; this.initGoogleMap = initGoogleMap; this.loadGeoPoints = loadGeoPoints; this.addMarker = addMarker; this.addCustomMarker = addCustomMarker; this.drawLine = drawLine; this.drawFence = drawFence; this.controlPanel = controlPanel; this.clearMap = clearMap; this.shiftPoint = shiftPoint; this.joinPoints = joinPoints; this.addNewMarker = addNewMarker; this.saveToDB = saveToDB; this.searchPC = searchPC; this.popupOnload = popupOnload; this.showLoc = showLoc; this.loadLocationHTML = loadLocationHTML; this.popupOnSearch = popupOnSearch; /* functions */ } function initGoogleMap(){ if(typeof GBrowserIsCompatible == 'function'){ GmapDiv = document.createElement("div"); GmapDiv.style.height = (this.showCP?this.mapHeight - 30:this.mapHeight) + 'px'; this.divObj.appendChild(GmapDiv); this.mapObj = new GMap2(GmapDiv); thisMap = this.mapObj; thisClass = this; this.clearMap(); with(this.mapObj){ //addMapType(G_SATELLITE_3D_MAP); //addControl(new GSmallMapControl()); //enableScrollWheelZoom(); //setCenter(new GLatLng('51.4929','-0.1624'),this.zoomLevel); setUIToDefault(); } this.mapObj.disableScrollWheelZoom(); if(this.allowSave){ GEvent.addListener(this.mapObj,"click",function(overlay,point){ thisClass.addNewMarker(point); }); } }else{ alert('Google map support files are required'); } } function clearMap(){ thisMap.clearOverlays(); this.latLngArr = []; this.polyObj = ''; } function printMap(){ if(this.divId){ this.divObj = $(this.divId); this.divObj.innerHTML = ''; this.divObj.style.width = this.mapWidth + this.dimenTypeW; this.divObj.style.height = this.mapHeight + this.dimenTypeH; this.divObj.style.backgroundColor = this.divBg; this.divObj.style.border = "1px solid buttonShadow"; this.divObj.style.overflow = 'hidden'; this.initGoogleMap(); this.loadGeoPoints(); if(this.latLngArr){ //this.mapObj.setCenter(this.latLngArr[0],this.zoomLevel); if(this.showInfoOnLoad){ var extraInfo = (this.paramInfoArr[i]) ? this.paramInfoArr[i] : ""; this.popupOnload(extraInfo); } } switch(this.mapType.toString()){ case '2': this.drawLine(); break; case '3': this.drawFence(); break; } this.allowSave?this.showCP = true:''; if(this.showCP){ this.controlPanel(); } } } function popupOnload(extraInfo){ infoDiv = document.createElement("div"); infoDiv.setAttribute('id','infoBox'); infoDiv.style.height = '100px'; infoDiv.style.width = '200px'; infoDiv.style.color = '#013888'; infoDiv.style.textAlign = 'left'; infoDiv.style.lineHeight = '17px'; infoDiv.style.fontSize = '13px'; infoDiv.style.fontFamily = 'Tahoma, Arial, Helvetica'; txtObj = document.createTextNode('Fetching location...'); infoDiv.appendChild(txtObj); this.mapObj.openInfoWindow(this.mapObj.getCenter(),infoDiv); var split_data = (this.mapObj.getCenter().toString()).replace("(","").replace(")","").split(','); var rObj = new addObj(); rObj.lat = parseFloat(split_data[0]); rObj.longi = parseFloat(split_data[1]); rObj.type = 'div'; rObj.id = 'infoBox'; rObj.extraInfo = extraInfo; if (this.showLiveAddr) { loadLocationHTML(rObj); }else{ getAddressLatLong(rObj); } } function popupOnSearch(point,extraInfo){ infoDiv = document.createElement("div"); infoDiv.setAttribute('id','infoBox'); infoDiv.id = 'infoBox'; infoDiv.style.height = '120px'; infoDiv.style.width = '220px'; infoDiv.style.color = '#013888'; infoDiv.style.textAlign = 'left'; infoDiv.style.lineHeight = '17px'; infoDiv.style.fontSize = '13px'; infoDiv.style.fontFamily = 'Arial, Helvetica'; txtObj = document.createTextNode('Fetching location...'); infoDiv.appendChild(txtObj); this.mapObj.openInfoWindow(this.mapObj.getCenter(),infoDiv); var split_data = (point.toString()).replace("(","").replace(")","").split(','); if (this.addressDetails.length>0) { infoDiv.innerHTML = this.addressDetails; }else{ var rObj = new addObj(); rObj.lat = parseFloat(split_data[0]); rObj.longi = parseFloat(split_data[1]); rObj.type = 'div'; rObj.id = 'infoBox'; rObj.extraInfo = extraInfo; if (this.showLiveAddr) { loadLocationHTML(rObj); }else{ getAddressLatLong(rObj); } } } function $(id){ return document.getElementById(id); } function loadGeoPoints() { var jP = this.customNum; var xLen = this.paramArr.length; for(i=0;i0) { infoDiv.innerHTML = this.addressDetails; }else{ var rObj = new addObj(); rObj.lat = parseFloat(split_data[0]); rObj.longi = parseFloat(split_data[1]); rObj.type = 'div'; rObj.id = 'infoBox'; rObj.extraInfo = mObj.extraInfo; if (this.showLiveAddr) { loadLocationHTML(rObj); }else{ getAddressLatLong(rObj); } } } function joinPoints(){ switch(this.mapType.toString()){ case '2': this.drawLine(); break; case '3': this.drawFence(); break; } } function drawLine(){ if(this.latLngArr){ this.polyObj = new GPolyline(this.latLngArr,'#ff0000',4); this.mapObj.addOverlay(this.polyObj); } } function drawFence(){ if(this.latLngArr){ this.latLngArr.push(this.latLngArr[0]); this.polyObj = new GPolygon(this.latLngArr,'#ff0000',1,1,'#ff0000','0.2') this.mapObj.addOverlay(this.polyObj); this.latLngArr.pop(); } } function controlPanel(){ thisClass = this; //add the panel for buttons panelDiv = document.createElement("div"); panelDiv.style.backgroundColor = 'buttonFace'; panelDiv.style.height = '20px'; panelDiv.style.padding = '5px'; if(this.postcodeSearch){ var searchTxt = document.createElement("input"); searchTxt.setAttribute('type','textbox'); searchTxt.setAttribute('id','maptxt_pc_search'); //searchTxt.style.border = '1px inset buttonShadow'; searchTxt.style.width = '65px'; searchTxt.style.padding = '2px'; panelDiv.appendChild(searchTxt); var searchBtn = document.createElement("input"); searchBtn.setAttribute('type','button'); searchBtn.setAttribute('id','mapbtn_pc_search'); searchBtn.setAttribute('value','search postcode'); //searchBtn.style.border = '1px outset buttonShadow'; searchBtn.onclick = function (e){ thisClass.searchPC(); } panelDiv.appendChild(searchBtn); if(this.allowSave){ var resetBtn = document.createElement("input"); resetBtn.setAttribute('type','button'); resetBtn.setAttribute('value','remove markers'); //resetBtn.style.border = '1px outset buttonShadow'; resetBtn.onclick = function (e){ thisClass.clearMap(); } panelDiv.appendChild(resetBtn); var searchBtn = document.createElement("input"); searchBtn.setAttribute('type','button'); searchBtn.setAttribute('value','join markers'); //searchBtn.style.border = '1px outset buttonShadow'; searchBtn.onclick = function (e){ thisClass.joinPoints(); } panelDiv.appendChild(searchBtn); var searchBtn = document.createElement("input"); searchBtn.setAttribute('type','button'); searchBtn.setAttribute('value','save boundary'); //searchBtn.style.border = '1px outset buttonShadow'; searchBtn.onclick = function (e){ thisClass.saveToDB(); } //panelDiv.appendChild(searchBtn); } } this.divObj.appendChild(panelDiv); } function searchPC(){ var txtPcObj = $('maptxt_pc_search'); var sTxt = (this.postCode) ? this.postCode : txtPcObj.value; if(sTxt){ usePointFromPostcode(sTxt,function(point){ thisMap.setCenter(point,14); thisClass.addMarker(point); thisClass.popupOnSearch(point); }); } } function usePointFromPostcode(postcode, callbackFunction){ localSearch.setSearchCompleteCallback(null, function() { if (localSearch.results[0]) { var resultLat = localSearch.results[0].lat; var resultLng = localSearch.results[0].lng; var point = new GLatLng(resultLat,resultLng); callbackFunction(point); }else{ alert("Postcode not found!"); } }); localSearch.execute(postcode + ", UK"); } function saveToDB(){ if(typeof ajaxVars == 'undefined'){ alert('Ajax support files required'); return false; }else{ //this code is only for saving boundary points and static to buddiBoundary module. //check if boundary is created. if(this.latLngArr.length<= 0){ alert("Please mark points on the map."); return false; } //check if there are more then 8 points on map. if(this.latLngArr.length > 8){ alert("Please mark less than 9 points on map to continue."); return false; } //check if buddi wearer list is created if(globalBuddiSelectedList.length<= 0){ alert("Atleast one buddi needs to be selected to proceed"); return false; } //validate boundaryName var boundaryName = document.getElementById('boundary_name'); if(boundaryName.value.replace(/ /g, '') == ''){ alert("Please enter zone name"); return false; } var fullTimeRadio = document.getElementById('fullTime'); var timeType = ''; if(fullTimeRadio.checked) timeType = document.getElementById('fullTime').value; else timeType = document.getElementById('partTime').value; var chkdString = ''; var weekArr = new Array(); weekArr[1] = 'm'; weekArr[2] = 't'; weekArr[3] = 'w'; weekArr[4] = 't'; weekArr[5] = 'f'; weekArr[6] = 's'; weekArr[7] = 's'; var isValid = 0; for(i=1;i<=7;i++){ var elmnt = document.getElementById('weekDay'+i); if(elmnt.checked){ chkdString +=weekArr[i].toUpperCase(); isValid = 1; }else{ chkdString +=weekArr[i].toLowerCase(); } } //validate weekdays. if(timeType == 'PARTTIME_FENCE' && isValid == 0){ alert("Please select a week day"); return false; } var startHour = document.getElementById('start_hour').value; var startMin = document.getElementById('start_min').value; var endHour = document.getElementById('end_hour').value; var endMin = document.getElementById('end_min').value; if(timeType == 'PARTTIME_FENCE'){ //validate time //start time is equal to end time. if(parseInt(startHour) == parseInt(endHour) && parseInt(startMin) == parseInt(endMin)){ alert("Please enter valid start time and end time."); return false; } //start time is less then end time if((parseInt(startHour) > parseInt(endHour)) || (parseInt(startHour) == parseInt(endHour) && parseInt(startMin) > parseInt(endMin))){ alert("Start time is greater than end time."); return false; } } //boundary type ie inclusion or exclusion boundary var boundary_type = document.getElementById('boundary_type').value; //center point of map var center_lat_long = this.mapObj.getCenter().toString(); //zoom level var mapZoomLevel = this.mapObj.getZoom(); mapZoomLevel = (mapZoomLevel >0)?mapZoomLevel:16; //get fence status ie turnoff/turnon var fenceStatus = ''; if(document.getElementById('turned_on').checked) fenceStatus = 'FENCE_TURN_ON'; else fenceStatus = 'FENCE_TURN_OFF'; //save the boundary along with details var ajaxVar = new ajaxVars(); ajaxVar.incModule = this.incModuleName; ajaxVar.vars['action'] = "phpArray"; ajaxVar.vars['func'] = "saveFenceINC"; ajaxVar.vars['boundaryName'] = boundaryName.value; ajaxVar.vars['timeType'] = timeType; ajaxVar.vars['chkdString'] = chkdString; ajaxVar.vars['startHour'] = startHour; ajaxVar.vars['startMin'] = startMin; ajaxVar.vars['endHour'] = endHour; ajaxVar.vars['endMin'] = endMin; ajaxVar.vars['zoom'] = mapZoomLevel; ajaxVar.vars['boundary_type'] = boundary_type; ajaxVar.vars['center_point'] = center_lat_long; var latLongDbArr = new Array(); for(var k=0;k'; infoObj.innerHTML = (rObj.extraInfo) ? infoObj.innerHTML + 'at ' + rObj.extraInfo + '' : infoObj.innerHTML; } break; } } } function getGoogleAddr(rObj) { geocoder = new GClientGeocoder(); geocoder.rObj = rObj; geocoder.getLocations(new GLatLng(rObj.lat,rObj.longi),function(addresses){ if(addresses.Status.code != 200){ }else{ var tmpAddr = addresses.Placemark[0].address + '
' + addresses.Placemark[1].address; var ajaxVar = new ajaxVars(); ajaxVar.vars['action'] = "db_fetch_assoc"; ajaxVar.vars['sql_type'] = "select"; ajaxVar.vars['sql_what'] = "lat, longi, addr "; ajaxVar.vars['sql_from'] = "gMap "; ajaxVar.vars['sql_where'] = "lat = '" + rObj.lat + "' and longi = '" + rObj.longi + "'"; ajaxVar.vars['sql_limit'] = "limit 0,1 "; ajaxVar.noLayer = 0; ajaxVar.fnVars['longi'] = rObj.longi; ajaxVar.fnVars['lat'] = rObj.lat; ajaxVar.fnVars['addr'] = tmpAddr; ajaxVar.fnVars['rObj'] = rObj; ajaxVar.gotoFunction = 'insAddressLatLong'; ajaxVar.AjaxPostSQL(); } }); } function insAddressLatLong(dataArr,fnVars){ if (dataArr == 'empty'){ var ajaxVar = new ajaxVars(); ajaxVar.vars['action'] = 'update'; ajaxVar.dbVars = "lat,longi,addr"; ajaxVar.vars['table'] = 'gMap'; ajaxVar.vars['lat'] = fnVars['lat']; ajaxVar.vars['longi'] = fnVars['longi']; ajaxVar.vars['addr'] = fnVars['addr']; ajaxVar.vars['created_on_autoNow'] = ''; ajaxVar.noLayer = 0; ajaxVar.fnVars['rObj'] = fnVars['rObj']; ajaxVar.gotoFunction = 'refetchAdd'; ajaxVar.AjaxPostSQL(); } } function refetchAdd(dataArr,fnVars){ getAddressLatLong(fnVars['rObj']); } function googleNap(naptime){ naptime = naptime * 1000; var sleeping = true; var now = new Date(); var alarm; var startingMSeconds = now.getTime(); while(sleeping){ alarm = new Date(); alarmMSeconds = alarm.getTime(); if(alarmMSeconds - startingMSeconds > naptime){ sleeping = false; } } } function loadLocationHTML(rObj) { geocoder = new GClientGeocoder(); geocoder.rObj = rObj; geocoder.getLocations(new GLatLng(rObj.lat,rObj.longi),function(addresses){ if(addresses.Status.code != 200){ }else{ var tmpAddr = 'Approximately near:
' + addresses.Placemark[0].address + '
' + addresses.Placemark[1].address + '
'; tmpAddr = (rObj.extraInfo) ? tmpAddr + 'at ' + rObj.extraInfo + '' : tmpAddr; if(rObj.id){ switch(rObj.type){ case 'div': var infoObj = $(rObj.id); if(infoObj) infoObj.innerHTML = tmpAddr; break; case 'parentDiv': var infoObj = parent.document.getElementById(rObj.id); if(infoObj) infoObj.innerHTML = tmpAddr; break; } } } }); }