//Script che gestisce la form di ricerca, il plotting e la descrizione del percorso sulla mappa di Google

    var map;
    var gdir;
    
    function initialize() {
      if (GBrowserIsCompatible()) {      
        map = new GMap2(document.getElementById("map_canvas"));
        gdir = new GDirections(map, document.getElementById("directions"));
        GEvent.addListener(gdir, "addoverlay", onGDirectionsLoad);
        GEvent.addListener(gdir, "error", handleErrors);
    		map.addControl(new GScaleControl());
    		map.enableScrollWheelZoom();
    		map.enableDoubleClickZoom();
    		map.setMapType(G_NORMAL_MAP);
        map.addControl(new GSmallMapControl());
        map.addControl(new GMapTypeControl());
        gdir.clear();        
      }
    }
    
    //Parametri: indirizzo di partenza, indirizzo di arrivo, lingua per la descrizione del percorso (it_ITA, en_ENG)
    function setDirections(fromAddress, toAddress, language) {
      gdir.clear();
      gdir.load("from: " + fromAddress + " to: " + toAddress + "@45.49216598595865, 12.242740191993713", { "locale": language });              
    }

	function handleErrors(){
   if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
     alert("No corresponding geographic location could be found for one of the specified addresses. This may be due to the fact that the address is relatively new, or it may be incorrect. Errore n* " + gdir.getStatus().code);
   else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)
     alert("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known. Errore n* " + gdir.getStatus().code);
   else if (gdir.getStatus().code == G_GEO_MISSING_QUERY)
     alert("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input. Errore n* " + gdir.getStatus().code);
   else if (gdir.getStatus().code == G_GEO_BAD_KEY)
     alert("The given key is either invalid or does not match the domain for which it was given. Errore n* " + gdir.getStatus().code);
   else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
     alert("A directions request could not be successfully parsed. Errore n* " + gdir.getStatus().code);
   else alert("An unknown error occurred.");
    }

  function onGDirectionsLoad(){ 
   var poly = gdir.getPolyline();
   if (poly.getVertexCount() > 10000) {  //cambia il vaolore del numero dei vertici
     alert("Questo itinerario ha troppi vertici");
     return;
   }
   var baseUrl = "http://maps.google.com/staticmap?";

   var params = [];
   params.push("center=" + map.getCenter().lat().toFixed(6) + "," + map.getCenter().lng().toFixed(6));

   var markersArray = [];
   markersArray.push(poly.getVertex(0).toUrlValue(5) + ",reda");
   markersArray.push(poly.getVertex(poly.getVertexCount()-1).toUrlValue(5) + ",greenb");
  
   params.push("markers=" + markersArray.join("|"));

   var polyParams = "rgba:0x0000FF80,weight:50|";
   var polyLatLngs = [];
   for (var j = 0; j < poly.getVertexCount(); j++) {
     polyLatLngs.push(poly.getVertex(j).lat().toFixed(5) + "," + poly.getVertex(j).lng().toFixed(5));
   }
  }

S
