I use Google Maps Distance Service API to get driving distance and time between two points. Here's my code:
<div id="right-panel">
  <div id="output"></div>
</div>
<div id="map"></div>
<script>
  function initMap() {
    var bounds = new google.maps.LatLngBounds;
    var markersArray = [];
    var origin1 = "<?php echo $partenza; ?>";
    var destinationA = "<?php echo $destinazione; ?>";
    var destinationIcon = 'https://chart.googleapis.com/chart?' +
        'chst=d_map_pin_letter&chld=D|FF0000|000000';
    var originIcon = 'https://chart.googleapis.com/chart?' +
        'chst=d_map_pin_letter&chld=O|FFFF00|000000';
    var map = new google.maps.Map(document.getElementById('map'), {
      center: {lat: 55.53, lng: 9.4},
      zoom: 10
    });
    var geocoder = new google.maps.Geocoder;
    var service = new google.maps.DistanceMatrixService;
    distanza = 0;
    service.getDistanceMatrix({
      origins: [origin1],
      destinations: [destinationA],
      travelMode: 'DRIVING',
      unitSystem: google.maps.UnitSystem.METRIC,
      avoidHighways: false,
      avoidTolls: false
    }, function(response, status) {
      if (status !== 'OK') {
        alert('Error was: ' + status);
      } else {
        var originList = response.originAddresses;
        var destinationList = response.destinationAddresses;
        var outputDiv = document.getElementById('output');
        outputDiv.innerHTML = '';
        deleteMarkers(markersArray);
        var showGeocodedAddressOnMap = function(asDestination) {
          var icon = asDestination ? destinationIcon : originIcon;
          return function(results, status) {
            if (status === 'OK') {
              map.fitBounds(bounds.extend(results[0].geometry.location));
              markersArray.push(new google.maps.Marker({
                map: map,
                position: results[0].geometry.location,
                icon: icon
              }));
            } else {
              alert('Geocode was not successful due to: ' + status);
            }
          };
        };
        results = response.rows[0].elements;
        geocoder.geocode({'address': originList[0]},
            showGeocodedAddressOnMap(false));
          geocoder.geocode({'address': destinationList[0]},
              showGeocodedAddressOnMap(true));
          outputDiv.innerHTML += results[0].distance.text + ' in ' +
              results[0].duration.text + '<br>';
              distanza = results[0].distance.value;
              durata = results[0].duration.value;
            document.innerHTML = distanza + "<br>" + durata;
      }
    });
  }
  function deleteMarkers(markersArray) {
    for (var i = 0; i < markersArray.length; i++) {
      markersArray[i].setMap(null);
    }
    markersArray = [];
  }
  console.log("Distanza: "+distanza);
  console.log("Durata: "+durata);
</script>
<script async defer
src="https://maps.googleapis.com/maps/api/js?key=MY_API_HERE&callback=initMap">
</script>
I would like to get the distance and time between the points with a "console.log", but when I type console.log("Distanza: "+distanza); the javascript console says: "distanza is not defined"... How could I access these two variables also outside the function?
 
    