 
  function goMap24() {
    Map24.loadApi( ["core_api", "wrapper_api"] , map24ApiLoaded );
  }
  
   function mapClicked( e ){
    document.getElementById("coordinates").innerHTML = document.getElementById("coordinates").innerHTML+e.Coordinate.Longitude+"|";      
	document.getElementById("coordinates2").innerHTML = document.getElementById("coordinates2").innerHTML+e.Coordinate.Latitude+"|";
	e.stop();    
  }
  
  function map24ApiLoaded(){
    Map24.MapApplication.init( { NodeName: "mapaarribar" } ); 
	Map24.MapApplication.Map.addListener( "Map24.Event.MapClick", mapClicked );    
	
	delimita_longs="64.90992592592465|64.72103703703578|64.54325925925786|64.49288888888759|64.55066666666516|64.5106666666652|64.35140740740598|64.08251851851719|64.06844444444277|63.60474074073909|63.426962962961625|63.52474074073916|63.76103703703575|63.68474074073947|63.34029629629504|63.09807407407243|62.6336296296281|63.050666666665165|62.72029629629469|62.36103703703566|62.61140740740575";
	delimita_lats="2463.8208545084744|2464.2256002711865|2464.2118714576272|2464.2688206101697|2464.3069562033897|2464.460515525424|2464.550515525424|2464.2800070508474|2464.2256002711865|2464.198142644068|2464.364413830509|2464.394922305085|2464.6628884067795|2464.7503460338985|2464.7859392542377|2464.908481627119|2464.5891595932207|2464.1620409491524|2464.140176542373|2464.017125694915|2463.6678036610174";
	myPolyline = new Map24.Polyline({Longitudes: delimita_longs,Latitudes: delimita_lats,Color: "#f00" });
	myPolyline.setWidth(1);
	myPolyline.commit();
	
	myLoc = new Map24.Location({
      Longitude: 63.450666666665256,
      Latitude: 2463.8544138305087,
      Description: "This is a plain tooltip for the location which is visible on the interactive map.",
	  TooltipContent: "Patronat de Turisme",
      TooltipLayout: Map24.MapObject.LAYOUT_PLAIN,
      TooltipSize: Map24.MapObject.SIZE_M,
      LogoURL: "http://www.nadiua.com/app/webroot/phpthumb/images/icono_ajuntament.gif"
	  
    });
	myLoc.commit();
	
	
	Map24.MapApplication.center( {Longitude:63.450666666665256, Latitude:2463.8544138305087, MinimumWidth: 2500} );
  }
    
  var geocoder = null;
  var router = null;
  var routePoints = [];
  var routeID = null;
  
  function startRouting(){
    //Retrieve start and destination of the route from the input fields
    var startString = Map24.trim( $v('start') );
    var destinationString = Map24.trim( $v('destination') );
    
    //Check if the start and the destination form fields are empty.
    if( startString == "" ) { alert("Please enter start address!"); return; }
    if( destinationString == "" ) { alert("Please enter destination address!"); return; }
	
	//document.getElementById("button_calculate_route").disabled = true;
    
    //Create a geocoder stub
    var geocoder = new Map24.GeocoderServiceStub();
    //Geocode the start address of the route
    //Define the name of the callback function that is called when the result is available on the client.
    geocoder.geocode({ 
      SearchText: startString, 
      MaxNoOfAlternatives: 1, 
      CallbackFunction: setRouteEndPoint, 
      CallbackParameters: {position: "start"}
    });
    
    //Geocode the destination address of the route.
    //Define the name of the callback function that is called when the result is available on the client.
    geocoder.geocode({
      SearchText: destinationString, 
      MaxNoOfAlternatives: 1, 
      CallbackFunction: setRouteEndPoint,
      CallbackParameters: {position: "destination"}
    });
  }
  
  //Callback function that is called when the geocoding result is available.
  //After both the start and the destination address is geocoded, this function calls the calculateRoute() function.
  //The geocoded address is stored at the first position in the locations array.
  function setRouteEndPoint(locations, params){
    //Access the geocoded address and add it to the routePoints array.
    routePoints[ params.position ] = locations[0];
    if( typeof routePoints["start"] != "undefined" && typeof routePoints["destination"] != "undefined")
      calculateRoute(); 
  }
  
  //Calculate the route.
  function calculateRoute() {
    router = new Map24.RoutingServiceStub();
    router.setDefaultDescriptionLanguage('ES');
	
	// Definim Cambrils
	myLoc = new Map24.Location({
      Longitude: 63.450666666665256,
      Latitude: 2463.8544138305087,
      Description: "This is a plain tooltip for the location which is visible on the interactive map.",
	  TooltipContent: "Patronat de Turisme",
      TooltipLayout: Map24.MapObject.LAYOUT_PLAIN,
      TooltipSize: Map24.MapObject.SIZE_M,
      LogoURL: "http://www.nadiua.com/app/webroot/phpthumb/images/icono_ajuntament.gif"
	  
    });
	myLoc.commit();

	//Igualem el desti a Cambrils
	routePoints["destination"]=myLoc;
	
	router.calculateRoute({
      Start: routePoints["start"],
      Destination: routePoints["destination"],
      CallbackFunction: displayRoute
    });
    routePoints = [];
  }
  
  //Callback function used to access the result of the route calculation.
  //This function is called after the client has received the result from the Routing Service.
  //The route parameter is of type Map24.WebServices.Route.
  function displayRoute( route ){
    
	//Remember the routeId. It is used to hide a route.
	routeID = route.RouteID;
	
    //Access the assumed time needed for traversing the route in hours
    var totalTime = ((route.TotalTime)/(60*60) ).toPrecision(3) 
    //Access the total lenght of the route in kilometers
    var totalLength = (route.TotalLength/1000) 
    //Create table with description of the route
    /*var div_content = "Total Time: " + totalTime + " h<br>" ;     
    div_content += "Total Length: "+ totalLength +" km<br>";
    div_content += "<br>";*/
	var div_content="";
    
    //Iterate through the route segments and output the step-by-step textual description of the route
    for(var i = 0; i < route.Segments.length; i++){
      for(var j = 0; j < route.Segments[i].Descriptions.length; j++){
      	//The route description contains tags for further evaluation. For example, the [M24_STREET] tag is used 
      	//to denote a street in the description. Add the following line of code to replace these tags by a blank:
        div_content += (i+1) + ". " + route.Segments[i].Descriptions[j].Text.replace(/(\[|\[\/)[0-9A-Z_]+\]/g, '' ) + "<br><br>";
      }
    }
    document.getElementById('route_description').innerHTML = div_content;
	
	document.getElementById("button_hide_route").disabled = false;
	document.getElementById("button_remove_route").disabled = false;
  }
  
  //Show route
  function showRoute(routeID) {
  	router.showRoute({RouteId: routeID});
	document.getElementById("button_show_route").disabled = true;
	document.getElementById("button_hide_route").disabled = false;
  }
  
  //Hide route
  function hideRoute(routeID) {
  	router.hideRoute({RouteId: routeID});
	document.getElementById("button_show_route").disabled = false;
	document.getElementById("button_hide_route").disabled = true;
  }
  
  //Remove route
  function removeRoute(routeID) {
  	router.removeRoute({RouteId: routeID});
	
	document.getElementById("button_calculate_route").disabled = false;
	document.getElementById("button_show_route").disabled = true;
	document.getElementById("button_hide_route").disabled = true;
	document.getElementById("button_remove_route").disabled = true;
  }
  
  //Helper function.
  function $v( id ) { 
    return   (document.getElementById( id ).value != "undefined") ?  
        document.getElementById( id ).value : ""; 
  }


