  function goMap24() {
    Map24.loadApi( ["core_api", "wrapper_api"] , map24ApiLoaded );
  }
  
   function mapClicked( e ){
   document.getElementById("longitud").innerHTML = document.getElementById("longitud").innerHTML+e.Coordinate.Longitude+"|";      
	document.getElementById("latitud").innerHTML = document.getElementById("latitud").innerHTML+e.Coordinate.Latitude+"|";
	//alert("longitud "+e.Coordinate.Longitude+" latitud "+e.Coordinate.Latitude);
	e.stop();    
	
  }
  
  function map24ApiLoaded(){

    Map24.MapApplication.init( { NodeName: "maparea" } ); 
	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: document.getElementById("plongitud").value,
      Latitude: document.getElementById("platitud").value,
     // Longitude: 63.450666666665256,
     // Latitude: 2463.8544138305087,
      Description: document.getElementById("nom").value,
	  TooltipContent: document.getElementById("nom").value,
      TooltipLayout: Map24.MapObject.LAYOUT_PLAIN,
      TooltipSize: Map24.MapObject.SIZE_M,
      LogoURL: "http://www.nadiua.com/app/webroot/phpthumb/images/"+document.getElementById("icona").value
	  
    });
	myLoc.commit();
	
	
	Map24.MapApplication.center( {Longitude:document.getElementById("plongitud").value, Latitude:document.getElementById("platitud").value, 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>";
    
    //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>";
      }
    }
    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 : ""; 
  }
  
  function cercaCarrer(){
  	geocode(document.getElementById('cerca_carrer').value);
  }

	//Geocode an address.
  //The address is provided in the searchText parameter as free text.
  function geocode( searchText ){
    if(Map24.trim( searchText ) == "") { alert("Siusplau escriu un carrer."); return; }
  
    var geocoder = new Map24.GeocoderServiceStub();
    //Geocodes the address. The address is passed in the Search field. The Alternatives field defines the number
    //of geocoded addresses that are returned in the response. You must pass the name of the callback function
    //that is called as soon as the client has received the response.
    geocoder.geocode( { SearchText: Map24.trim( searchText ), MaxNoOfAlternatives: 10, CallbackFunction: printResult, City: "Cambrils" } );
  }
    
  //Callback function that is called after the client has received the response.
  //This function accesses the array of geocoded addresses and shows them in a list.
  //The elements of this array are objects of the type Map24.Location.
  function printResult( locs ){
    //Center the map view on the first element in the array of geocoded addresses. A Map24.Location object has several
    //methods and properties which you can find in the API documentation for the corresponding class.
   
   
   

	myLoc2 = new Map24.Location({
      Longitude: locs[0].getLongitude(),
      Latitude: locs[0].getLatitude(),
      Description: "This is a plain tooltip for the location which is visible on the interactive map.",
	  TooltipContent: locs[0].getStreet()+"<br/> "+locs[0].getZip(),
      TooltipLayout: Map24.MapObject.LAYOUT_PLAIN,
      TooltipSize: Map24.MapObject.SIZE_M,
      LogoURL: "http://www.nadiua.com/app/webroot/phpthumb/images/icono_platja.gif"
	  
    });
	// NO FUNCIONA 
	//if(!locs[0])	alert("No s'ha trobat");
	myLoc2.commit();
   
  
   
   
   
   
   
   
   
   
   
   Map24.MapApplication.center( { Longitude: locs[0].getLongitude(), Latitude: locs[0].getLatitude(), MinimumWidth: 2500 } );
    
    //Create a list that shows the results.
    /*var result = "<br /><center><b>Search results:</b></center><hr />";
    
    //Iterate through the array of locations.
    for( var i=0; i<locs.length; i++ ){
      
      //Output all relevant properties of all locations.
      result += "<b>Result Nr."+(i+1)+"</b><br />";  
      
      result += "Longitude "+[i+1]+": "+locs[i].getLongitude()+"<br />";
      result += "Latitude "+[i+1]+": "+locs[i].getLatitude()+"<br />";
 
      result += "City: "+locs[i].getCity()+"<br />";
      result += "Zip: "+locs[i].getZip()+"<br />";
      result += "County: "+locs[i].getCounty()+"<br />";
      result += "State: "+locs[i].getState()+"<br />";
      result += "Country: "+locs[i].getCountry()+"<br />";
      result += "<input type=\"button\" value=\"Center on Result\" onclick=\"Map24.MapApplication.center( {Longitude: "+locs[i].getLongitude()+", Latitude: "+locs[i].getLatitude()+"});\" />";            
      result += "<hr/>";
    }
    document.getElementById("geocodingresults").innerHTML = result;      */
  }
 
  
