  var geocoder;
  var map;
  var linea;
  var dista = document.getElementById('dst');
  var marker;
  var markersArray = [];
  var lineaArray= [];

  function initialize() {
    var latlng = new google.maps.LatLng(-39.8274322,-73.2512409);
	geocoder = new google.maps.Geocoder();
    var myOptions = {
      zoom: 2,
      center: latlng,
	  mapTypeControl: false,
      mapTypeId: google.maps.MapTypeId.HYBRID,
	  scrollwheel: false
    };
    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
	var marker = new google.maps.Marker({
            map: map, 
            position: new google.maps.LatLng(-39.8274322,-73.2512409)
        });
  }

 function destino() {
    var address = document.getElementById("address").value;
    geocoder.geocode( { 'address': address}, function(results, status) {
      if (status == google.maps.GeocoderStatus.OK) {
		deleteOverlays(); deleteLineas();
		var dista = document.getElementById('dst');
		var lat=results[0].geometry.location.lat();
		var lng=results[0].geometry.location.lng();
		map.panToBounds(results[0].geometry.bounds);
        //map.setCenter(results[0].geometry.location);
        marker = new google.maps.Marker({
            map: map, 
            position: results[0].geometry.location
        });
		markersArray.push(marker);
		var ruta = [
			new google.maps.LatLng(-39.8274322,-73.2512409),
			results[0].geometry.location			
		];
		linea = new google.maps.Polyline({
			path: ruta,
			geodesic: true,
			strokeColor: "#FF0000",
			strokeOpacity: 1.0,
			strokeWeight: 2
		});
		lineaArray.push(linea);
		var dist=distancia(lat,lng);
		var timeflight= Math.round(dist/800);
		dista.innerHTML = '<b>Distancia:</b> '+ dist +'KM  <b>Tiempo Vuelo estimado:</b> '+timeflight+'Hr';
		linea.setMap(map);
      } else {
        alert("No fue posible la geocodificación: " + status);
      }
    })};

	function deleteOverlays() {
	  if (markersArray.length) {
		for (i=0; i<markersArray.length;i++) {
		  markersArray[i].setMap(null);
		}
		markersArray.length = 0;
	  }
	}

	function deleteLineas() {
	  if (lineaArray.length) {
		for (i=0; i<lineaArray.length;i++) {
		  lineaArray[i].setMap(null);
		}
		lineaArray.length = 0;
	  }
	}

	function distancia(lat1,lon1) {
	var PIx = 3.141592653589793;
	lat2=-39.8274322; lon2=-73.2512409;
	var R = 6371; // Radius of the earth in km
	var dLat = (lat2-lat1)*PIx/180;  // Javascript functions in radians
	var dLon = (lon2-lon1)*PIx/180; 
	lat1=lat1*PIx/180; lat2=lat2*PIx/180;
	var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
        Math.cos(lat1) * Math.cos(lat2) * 
        Math.sin(dLon/2) * Math.sin(dLon/2); 
	var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
	var d = R * c;
	return Math.round(d);
	}
