Good morning. I have this code that receives lat start, long start, lat end, long end and it generates a map with two marker and then shows how to get to that point by DRIVING.
<?php
$latitudInicio = $_GET['latitudInicio'];
$longitudInicio = $_GET['longitudInicio'];
$latitudFin = $_GET['latitudFin'];
$longitudFin = $_GET['longitudFin'];
?>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<div id="map" style="width: 100%; height: 700px;"></div>
<script>
function initMap() {
var inicio = {lat: <?php echo $latitudInicio ?>, lng: <?php echo $longitudInicio ?>};
var fin = {lat: <?php echo $latitudFin ?>, lng: <?php echo $longitudFin ?>};
var map = new google.maps.Map(document.getElementById('map'), {
center: inicio,
zoom: 7
});
var inicioMarker = new google.maps.Marker({
position: inicio,
map: map,
title: '<?php echo $latitudInicio ?> <?php echo $longitudInicio ?>'
});
var finMarker = new google.maps.Marker({
position: fin,
map: map,
title: '<?php echo $latitudFin ?> <?php echo $longitudFin ?>'
});
var directionsDisplay = new google.maps.DirectionsRenderer({
map: map,
suppressMarkers: true
});
// Set destination, origin and travel mode.
var request = {
destination: fin,
origin: inicio,
travelMode: 'DRIVING'
};
// Pass the directions request to the directions service.
var directionsService = new google.maps.DirectionsService();
directionsService.route(request, function(response, status) {
if (status == 'OK') {
// Display the route on the map.
directionsDisplay.setDirections(response);
}
});
}
</script>
<script src="https://maps.googleapis.com/maps/api/js?
key=API_KEY&callback=initMap"
async defer></script>
</body>
</html>
Now what I need is to show in a label, input text, etc, the distance between those two marker. Searching in the google documentation I come to this:
Here what happens to me in a direction that generates a json with the two directions that I can pass by parameter and there if I see the time it takes between the points. Now, I do not understand how to use it or what I have to do with that code to be able to show it, do they give me a hand? Thank you very much!
{
"destination_addresses" : [ "Nueva York, EE. UU." ],
"origin_addresses" : [ "Washington D. C., Distrito de Columbia, EE. UU." ],
"rows" : [
{
"elements" : [
{
"distance" : {
"text" : "225 mi",
"value" : 361720
},
"duration" : {
"text" : "3h 50 min",
"value" : 13794
},
"status" : "OK"
}
]
}
],
"status" : "OK"
}