MapDisplayer = {} //Maps displayer MapDisplayer.Map = function(htmlContainer, htmlSideBar, pathContainer, searchAroundButton) { this.htmlContainer = htmlContainer; this.pathContainer = pathContainer; this.htmlSideBar = htmlSideBar; this.searchAroundButton = searchAroundButton; this.searchAddressesAround = 1; this.isBrowserComptaible = GBrowserIsCompatible(); // ====== Create a Client Geocoder ====== this.geoCoder = new GClientGeocoder(new GGeocodeCache()); } MapDisplayer.Map.prototype.init = function(initialGeoLocalizableBean) { if (!this._initialized) { this.initialGeoLocalizableBean = initialGeoLocalizableBean; this._initialized = true; window.oMapDisplayer = this; } } MapDisplayer.Map.prototype.switchSearchAround = function(bSearchAddressAround) { var mustContinue = 1; if (bSearchAddressAround == 1) { this.searchAddressesAround = bSearchAddressAround; mustContinue = 0; removeClass(this.searchAroundButton, "selected"); } else { if (bSearchAddressAround == 0) { this.searchAddressesAround = bSearchAddressAround; mustContinue = 0; addClass(this.searchAroundButton, "selected"); } } if (mustContinue == 1) { this.searchAddressesAround = (this.searchAddressesAround==1)?0:1; if (this.searchAddressesAround==1) { removeClass(this.searchAroundButton, "selected"); } else { addClass(this.searchAroundButton, "selected"); } } } function hasClass(ele,cls) { return ele.className.match(new RegExp('(\\s|^)'+cls+'(\\s|$)')); } function addClass(ele,cls) { if (!this.hasClass(ele,cls)) ele.className += " "+cls; } function removeClass(ele,cls) { if (hasClass(ele,cls)) { var reg = new RegExp('(\\s|^)'+cls+'(\\s|$)'); ele.className=ele.className.replace(reg,' '); } } function hidePanel(oPanel) { oPanel.style.height = "20px"; } MapDisplayer.Map.prototype.draw = function() { this.GMap = null; this.initialGeoLocalizableMarker; this.geoLocalizableMarkers = []; //var getDirections = false; this.poly; this.initialPoint; this.userPoint; this.jsondata = ""; this.nbDirectionsDone = 0; this.indexDestination = 0; this.directionsToGet = []; this.directionsGetters = []; this.delay = 10; this.markersToOptimize = []; //Initialize map this.GMap = new GMap(this.htmlContainer); //GKeyboardHandler(this.GMap); this.GMap.displayerObject = this; //GMap options //this.GMap.addControl(smallMapControl, new GControlPosition(G_ANCHOR_BOTTOM_RIGHT)); this.GMap.addControl(new GSmallMapControl()); this.GMap.addControl(new GMapTypeControl()); this.GMap.setCenter(new GLatLng(this.initialGeoLocalizableBean.latitude,this.initialGeoLocalizableBean.longitude), 15); this.GMap.enableScrollWheelZoom(); //GMap bounds var bounds = this.GMap.getBounds(); this._getGeoLocalizableListArround(bounds); GEvent.addListener(this.GMap, "dragend", function(){ if (window.oMapDisplayer.searchAddressesAround == 1) { this.displayerObject._getGeoLocalizableListArround(this.getBounds()); } }); GEvent.addListener(this.GMap, "zoomend", function(){ if (window.oMapDisplayer.searchAddressesAround == 1) { this.displayerObject._getGeoLocalizableListArround(this.getBounds()); } }); $(document).mousemove(function(e){ var accordion = document.getElementById("accordion"); if (accordion.style.zIndex == "999") { if (e.pageX < 300) { window.oMapDisplayer.htmlContainer.style.zIndex = "-999"; accordion.style.zIndex = "999"; } else { window.oMapDisplayer.htmlContainer.style.zIndex = "999"; accordion.style.zIndex = "-999"; } } else { if (e.pageX < 300 && e.pageY < 200) { window.oMapDisplayer.htmlContainer.style.zIndex = "-999"; accordion.style.zIndex = "999"; } else { window.oMapDisplayer.htmlContainer.style.zIndex = "999"; accordion.style.zIndex = "-999"; } } }); //Draw the initial Marker this._plotMainMarker(); this.initialGeoLocalizableMarker.openInfoWindowHtml(this.initialGeoLocalizableBean.htmlToolTip); this.gdir=new GDirections(null, this.pathContainer); GEvent.addListener(this.gdir,"error", function() { var code = this.getStatus().code; var reason="Code "+code; if (MapDisplayer.Map.reasons[code]) { reason = "Code "+code +" : "+ MapDisplayer.Map.reasons[code]; } alert("Failed to obtain directions, "+reason); }); GEvent.addListener(this.gdir, "load", function() { if (window.oMapDisplayer.poly) window.oMapDisplayer.GMap.removeOverlay(window.oMapDisplayer.poly); window.oMapDisplayer.poly = this.getPolyline(); window.oMapDisplayer.GMap.addOverlay(window.oMapDisplayer.poly); }); var content = document.createTextNode("address : " + this.initialGeoLocalizableBean.gMapFormattedAddress); this.htmlContainer.appendChild(content); } MapDisplayer.Map.prototype.directions = function() { if(this._howManyMarkersSelected() > 20){ alert("Il y a trop d'entreprises pour calculer une itinéraire.\nVeuillez réduire le nombre d'entreprises affichées."); return; } //var goForDirections = false; this.initialPoint = this.initialGeoLocalizableMarker.getPoint(); //if the user specified an address geolocalize it and then get directions if($("#from").val() != '' ){ this._geolocalizeUserAddress(); }else{ this._getOptimizedDirections(); } } MapDisplayer.Map.prototype._howManyMarkersSelected = function() { var nb = 0; for (var i=0; i < this.geoLocalizableMarkers.length; i++) { var aMarker = this.geoLocalizableMarkers[i]; if (aMarker.sideBarCheckBox.checked == true) { nb++; } } return nb; } MapDisplayer.Map.prototype._getGeoLocalizableListArround = function(bounds){ var entLaMin = bounds.getSouthWest().lat(); var entLoMin = bounds.getSouthWest().lng(); var entLaMax = bounds.getNorthEast().lat(); var entLoMax = bounds.getNorthEast().lng(); var actionToCall = ""; actionToCall = GeoLocalizableBeans.GeoLocalizeBean.TYPES[this.initialGeoLocalizableBean.type]; if (actionToCall != '') { this.GMap.clearOverlays(); $.getJSON(window.webAppName + "/" + actionToCall + ".fl?id=" + this.initialGeoLocalizableBean.id + "&EntLaMin="+parseFloat(entLaMin) + "&EntLoMin="+parseFloat(entLoMin) + "&EntLaMax="+parseFloat(entLaMax) + "&EntLoMax="+parseFloat(entLoMax) + "&la=" + this.initialGeoLocalizableBean.latitude + "&lo=" + this.initialGeoLocalizableBean.longitude, function(data) { var displayResult = true; if(typeof data.markers != 'undefined'){ if(data.markers.length > 20){ if(window.confirm("Votre niveau de zoom ramène ["+data.markers.length+"] entreprises.\nVoulez-vous afficher quand même les résultats?") == false){ window.oMapDisplayer.switchSearchAround(0); displayResult = false; } } } if(displayResult){ var oMapDislayer = window.oMapDisplayer; i=0; oMapDislayer.geoLocalizableMarkers = []; oMapDislayer._plotMainMarker(); $.each(data.markers, function(index,object){ //Initialize the geoLocalizableBean from the JSON return var aGeoLocalizableObject = new GeoLocalizableBeans.GeoLocalizeBean(object.id); aGeoLocalizableObject.init(object.type, object.latitude, object.longitude, object.label, object.address1, object.address2, object.address3, object.zipCode, object.city, object.country, object.htmlToolTip); var point = new GLatLng(aGeoLocalizableObject.latitude, aGeoLocalizableObject.longitude); var marker = oMapDislayer._createMarker(point, aGeoLocalizableObject); oMapDislayer.GMap.addOverlay(marker); }); oMapDislayer.htmlSideBar.appendChild(oMapDislayer._drawSideBar()); $("#accordion").accordion( "resize" ); } if (window.oMapDisplayer.poly) window.oMapDisplayer.GMap.addOverlay(window.oMapDisplayer.poly); //if(getDirections == true){ directions()}; } ); } } MapDisplayer.Map.prototype._drawSideBar = function() { //clear the sidebar if (!this.initialhtmlSideBarContent) { this.initialhtmlSideBarContent = this.htmlSideBar.innerHTML; } this.htmlSideBar.innerHTML = this.initialhtmlSideBarContent; this.switchSearchAround(this.searchAddressesAround=0?0:1); //and recreate it var sideBar = document.createElement("table"); var tr = document.createElement("tr"); var td = document.createElement("td"); //create the "All" checkbox var aCheckBox = document.createElement("input"); aCheckBox.type = "checkbox"; aCheckBox.checked = true; aCheckBox.onclick = function () {window.oMapDisplayer._showHideAllMarkers(this)}; td.appendChild(aCheckBox); tr.appendChild(td); td = document.createElement("td"); var span = document.createElement("span"); span.className = "dataLabel"; span.appendChild(document.createTextNode("Tous")); td.appendChild(span); tr.appendChild(td); sideBar.appendChild(tr); //create a line for each marker to display for (var i=0; i < this.geoLocalizableMarkers.length; i++) { var aMarker = this.geoLocalizableMarkers[i]; var aGeoLocalizableObject = aMarker.geoLocalizableObject; tr = document.createElement("tr"); td = document.createElement("td"); td.appendChild(aMarker.sideBarCheckBox); tr.appendChild(td); td = document.createElement("td"); var a = document.createElement("a"); a.href="#"; a.marker = aMarker; a.onclick = function() { this.marker.openInfoWindowHtml(this.marker.geoLocalizableObject.htmlToolTip); }; a.appendChild(document.createTextNode(aGeoLocalizableObject.label)); td.appendChild(a); tr.appendChild(td); sideBar.appendChild(tr); } //return the sidebar object return sideBar; } MapDisplayer.Map.prototype._createMarker = function(point,aGeoLocalizableObject) { var marker = new GMarker(point, {zIndexProcess:this._importanceOrder}); marker.importance = 1; marker.geoLocalizableObject = aGeoLocalizableObject; //on click : show the htmlToolTip GEvent.addListener(marker, "click", function() { marker.openInfoWindowHtml(this.geoLocalizableObject.htmlToolTip); }); //create the sidebar checkbox and attach it to the marker var aCheckBox = document.createElement("input"); aCheckBox.type = "checkbox"; aCheckBox.id = marker.geoLocalizableObject.id; aCheckBox.name = marker.geoLocalizableObject.id; aCheckBox.className = "entInput"; aCheckBox.checked = true; aCheckBox.marker = marker; aCheckBox.onclick = function () { window.oMapDisplayer._showHideMarker(this.marker); }; marker.sideBarCheckBox = aCheckBox; //update marker list this.geoLocalizableMarkers.push(marker); //return the created marker return marker; } MapDisplayer.Map.prototype._showHideAllMarkers = function(allCheckBox) { for (var i=0; i < this.geoLocalizableMarkers.length; i++) { var aMarker = this.geoLocalizableMarkers[i]; aMarker.sideBarCheckBox.checked = allCheckBox.checked; if (allCheckBox.checked == true) { this.GMap.addOverlay(aMarker); } else { this.GMap.removeOverlay(aMarker); } } } MapDisplayer.Map.prototype._showHideMarker = function(aMarker){ if (this.poly) this.GMap.removeOverlay(this.poly); //get the input checkbox var theCheckBox = document.getElementById(aMarker.geoLocalizableObject.id); if($(theCheckBox).is(':checked')){ this.GMap.addOverlay(aMarker); } else { this.GMap.removeOverlay(aMarker); } } MapDisplayer.Map.prototype._importanceOrder = function(marker) { return GOverlay.getZIndex(marker.getPoint().lat()) + marker.importance*1000000; } MapDisplayer.Map.prototype._plotMainMarker = function(){ var myicon = new GIcon(G_DEFAULT_ICON); myicon.sprite = {image:window.webAppName + "/icons/sprites.png", top:34*5}; var point = new GLatLng(this.initialGeoLocalizableBean.latitude,this.initialGeoLocalizableBean.longitude); this.initialGeoLocalizableMarker = new GMarker(point, {icon:myicon,zIndexProcess:this._importanceOrder}); this.initialGeoLocalizableMarker.importance = 2; this.initialGeoLocalizableMarker.geoLocalizableObject = this.initialGeoLocalizableBean; GEvent.addListener(this.initialGeoLocalizableMarker, "click", function() { this.openInfoWindowHtml(this.geoLocalizableObject.htmlToolTip); }); this.GMap.addOverlay(this.initialGeoLocalizableMarker); } //Itinéraire methods MapDisplayer.Map.prototype._getOptimizedDirections = function(){ this.nbDirectionsDone = 0; this.indexDestination = 0; this.jsondata = ""; this.directionsToGet = []; this.directionsGetters = []; this.delay = 10; this.markersToOptimize = []; if($("#from").val()!=''){ for (var i=0; i < this.geoLocalizableMarkers.length; i++) { var aMarker = this.geoLocalizableMarkers[i]; if (aMarker.sideBarCheckBox.checked == true) { this.markersToOptimize.push(aMarker); } } this.markersToOptimize.push(this.initialGeoLocalizableMarker); } //Get all the distance between each points of the selected Markers //getDirectionFor2Points(); for( var index = -1; index < this.markersToOptimize.length; ++index){ for( var j = 0; j < this.markersToOptimize.length; ++j){ if(index != j){ this._getDirectionFor2Points(index, j); } } } this.directionsGetters[0].load(this.directionsToGet[0]); //setTimeout("getStatus();", 1000); //getDirection = true; $("#loading").attr("style","display: block"); this.pathContainer.innerHTML = ""; } //Get the distance between each points and append them into the JSON data MapDisplayer.Map.prototype._getDirectionFor2Points = function(initial, destination){ if(initial == -1){ if($("#from").val() != ''){ a = "from: @" + this.userPoint.toUrlValue(6) + " to: @" + this.markersToOptimize[destination].getPoint().toUrlValue(6); } else { a = "from: @" + this.initialPoint.toUrlValue(6) + " to: @" + this.markersToOptimize[destination].getPoint().toUrlValue(6); } } else { a = "from: @" + this.markersToOptimize[initial].getPoint().toUrlValue(6) + " to: @" + this.markersToOptimize[destination].getPoint().toUrlValue(6); } var directions = new GDirections(); GEvent.addListener(directions,"load",function() { if(window.oMapDisplayer.nbDirectionsDone > 0){ window.oMapDisplayer.jsondata += ", "; } window.oMapDisplayer.jsondata += "{\"initial\": "+initial+", \"dest\": "+destination+", \"distance\": "+this.getDistance().meters+", \"duration\": "+this.getDuration().seconds+"}"; window.oMapDisplayer.nbDirectionsDone++; if(window.oMapDisplayer.nbDirectionsDone < window.oMapDisplayer.directionsGetters.length){ setTimeout("window.oMapDisplayer.directionsGetters[window.oMapDisplayer.nbDirectionsDone].load(window.oMapDisplayer.directionsToGet[window.oMapDisplayer.nbDirectionsDone])",window.oMapDisplayer.delay); }else{ window.oMapDisplayer.jsondata = "["+window.oMapDisplayer.jsondata+"]"; window.oMapDisplayer._sendDirectionsForOptimization(window.oMapDisplayer.jsondata); } }); GEvent.addListener(directions,"error",function() { if(directions.getStatus().code == G_GEO_TOO_MANY_QUERIES){ this.delay = this.delay+10; setTimeout("window.oMapDisplayer.directionsGetters[window.oMapDisplayer.nbDirectionsDone].load(window.oMapDisplayer.directionsToGet[window.oMapDisplayer.nbDirectionsDone])",window.oMapDisplayer.delay); }else{ alert(MapDisplayer.Map.reasons[directions.getStatus().code]); } }); this.directionsToGet.push(a); this.directionsGetters.push(directions); } MapDisplayer.Map.prototype._sendDirectionsForOptimization = function(jsondata){ $.ajax({ data: jsondata, type: "POST", url: window.webAppName + '/enterprise_get_optimized_directions.fl?size='+this.nbDirectionsDone, timeout: 20000, contentType: "application/json;charset=utf-8", dataType: 'json', async: false, success: function(data){ $("#loading").attr("style","display: none"); //map.clearOverlay(); if($("#from").val() != ''){ var a = "from: "+$("#from").val()+ " @" + window.oMapDisplayer.userPoint.toUrlValue(6); window.oMapDisplayer._plotDirectionMarker(MapDisplayer.Map.alphabet[0], window.oMapDisplayer.userPoint.lat(), window.oMapDisplayer.userPoint.lng()); }else{ var a = "from: " + window.oMapDisplayer.initialGeoLocalizableBean.label + " - " + window.oMapDisplayer.initialGeoLocalizableBean.gMapFormattedAddress + " @" + window.oMapDisplayer.initialGeoLocalizableMarker.getPoint().toUrlValue(6); window.oMapDisplayer._plotDirectionMarker(MapDisplayer.Map.alphabet[0], window.oMapDisplayer.initialGeoLocalizableMarker.getPoint().lat(), window.oMapDisplayer.initialGeoLocalizableMarker.getPoint().lng()); } var b = ""; $.each(data.markers, function(index,object){ if(object.id > -1){ window.oMapDisplayer._plotDirectionMarker(MapDisplayer.Map.alphabet[index], window.oMapDisplayer.markersToOptimize[object.id].getPoint().lat(), window.oMapDisplayer.markersToOptimize[object.id].getPoint().lng()); b += " to: "+ window.oMapDisplayer.markersToOptimize[object.id].geoLocalizableObject.label + " - " + window.oMapDisplayer.markersToOptimize[object.id].geoLocalizableObject.gMapFormattedAddress + " @" + window.oMapDisplayer.markersToOptimize[object.id].getPoint().toUrlValue(6); } }); window.oMapDisplayer.gdir.load(a+b, {getPolyline:true}); //getDirections = true; } }); } MapDisplayer.Map.prototype._geolocalizeUserAddress = function(){ this.geoCoder.getLocations($("#from").val(), function (result) { // If that was successful if (result.Status.code == G_GEO_SUCCESS) { // Lets assume that the first marker is the one we want var p = result.Placemark[0].Point.coordinates; var lat=p[1]; var lng=p[0]; window.oMapDisplayer.userPoint = new GLatLng(lat, lng); window.oMapDisplayer._getOptimizedDirections(); } // ====== Decode the error status ====== else { alert(MapDisplayer.Map.reasons[result.Status.code]); } }); } MapDisplayer.Map.prototype._plotDirectionMarker = function(letter, lat, lng){ var myDirectionIcon = new GIcon(G_DEFAULT_ICON); myDirectionIcon.image = "http://maps.google.com/mapfiles/marker" + letter + ".png"; var directionPoint = new GLatLng(lat,lng); var directionMarker = new GMarker(directionPoint, {icon:myDirectionIcon,zIndexProcess:this._importanceOrder}); directionMarker.importance = 2; this.GMap.addOverlay(directionMarker); } //====== Array for decoding the failure codes ====== MapDisplayer.Map.reasons=[]; MapDisplayer.Map.reasons[G_GEO_SUCCESS] = "Success"; MapDisplayer.Map.reasons[G_GEO_MISSING_ADDRESS] = "Missing Address: The address was either missing or had no value."; MapDisplayer.Map.reasons[G_GEO_UNKNOWN_ADDRESS] = "Unknown Address: No corresponding geographic location could be found for the specified address."; MapDisplayer.Map.reasons[G_GEO_UNAVAILABLE_ADDRESS]= "Unavailable Address: The geocode for the given address cannot be returned due to legal or contractual reasons."; MapDisplayer.Map.reasons[G_GEO_BAD_KEY] = "Bad Key: The API key is either invalid or does not match the domain for which it was given"; MapDisplayer.Map.reasons[G_GEO_TOO_MANY_QUERIES] = "Too Many Queries: The daily geocoding quota for this site has been exceeded."; MapDisplayer.Map.reasons[G_GEO_SERVER_ERROR] = "Server error: The geocoding request could not be successfully processed."; MapDisplayer.Map.reasons[G_GEO_BAD_REQUEST] = "A directions request could not be successfully parsed."; MapDisplayer.Map.reasons[G_GEO_MISSING_QUERY] = "No query was specified in the input."; MapDisplayer.Map.reasons[G_GEO_UNKNOWN_DIRECTIONS] = "The GDirections object could not compute directions between the points."; MapDisplayer.Map.alphabet = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T'];