javascript variables and google maps distance service : <p>I use Google Maps Distance Service API to get driving distance and time between two points.
Here's my code:</p>

<pre><code>&lt;div id="right-panel"&gt;
  &lt;div id="output"&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div id="map"&gt;&lt;/div&gt;
&lt;script&gt;

  function initMap() {
    var bounds = new google.maps.LatLngBounds;
    var markersArray = [];

    var origin1 = "&lt;?php echo $partenza; ?&gt;";
    var destinationA = "&lt;?php echo $destinazione; ?&gt;";

    var destinationIcon = 'https://chart.googleapis.com/chart?' +
        'chst=d_map_pin_letter&amp;chld=D|FF0000|000000';
    var originIcon = 'https://chart.googleapis.com/chart?' +
        'chst=d_map_pin_letter&amp;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 + '&lt;br&gt;';

              distanza = results[0].distance.value;
              durata = results[0].duration.value;

            document.innerHTML = distanza + "&lt;br&gt;" + durata;

      }
    });
  }

  function deleteMarkers(markersArray) {
    for (var i = 0; i &lt; markersArray.length; i++) {
      markersArray[i].setMap(null);
    }
    markersArray = [];
  }

  console.log("Distanza: "+distanza);
  console.log("Durata: "+durata);

&lt;/script&gt;
&lt;script async defer
src="https://maps.googleapis.com/maps/api/js?key=MY_API_HERE&amp;callback=initMap"&gt;
&lt;/script&gt;
</code></pre>

<p>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?</p>
