<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyDa_d1tGwiwmST9lBBWRwWSdGiuECKiE4s&libraries=places"></script>
<div id="map"></div>
<script>
function calculateDistance(address1, address2) {
	const directionsService = new google.maps.DirectionsService();
	const geocoder = new google.maps.Geocoder();

	const request = {
		origin: address1,
		destination: address2,
		travelMode: google.maps.TravelMode.DRIVING
	};

	geocoder.geocode({ address: address1 }, function(results, status) {
		if (status === google.maps.GeocoderStatus.OK) {
			const originLocation = results[0].geometry.location;

			geocoder.geocode({ address: address2 }, function(results, status) {
				if (status === google.maps.GeocoderStatus.OK) {
					const destinationLocation = results[0].geometry.location;
					const mapOptions = {
						center: { lat: 0, lng: 0 },
						zoom: 1
					};
					const map = new google.maps.Map(document.getElementById("map"), mapOptions);

					directionsService.route(request, function(result, status) {
						if (status === google.maps.DirectionsStatus.OK) {
							const distance = result.routes[0].legs[0].distance.value/1000;
							console.log("Distance between " + address1 + " and " + address2 + ": " + distance);
						} else {
							console.log("Error calculating distance: " + status);
						}
					});
				} else {
					console.log("Error geocoding destination address: " + status);
				}
			});
		} else {
			console.log("Error geocoding origin address: " + status);
		}
	});
}
calculateDistance("Stockholm, Sweden", "Köping, Sweden");

</script>