I am trying to save the result of a function in a variable
let myDistance = (()=>{
let service = new google.maps.DistanceMatrixService();
service.getDistanceMatrix(
{
origins: [{lat: 55.93, lng: -3.118}],
destinations: [{lat: 50.087, lng: 14.421}],
travelMode: google.maps.TravelMode.DRIVING,
avoidHighways: false,
avoidTolls: false
}, callback );
function callback(response, status) {
let distance = 0;
if(status=="OK") {
distance = response.rows[0].elements[0].distance.value;
} else {
alert("Error: " + status);
}
return distance;
}
})();
console.log(myDistance)
But not working I use Google Maps Api v3 - Distance Matrix section
<script async defer
src="https://maps.googleapis.com/maps/api/jskey=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk">
</script>