Calculate a route by Google Map and multiply the distance by a value in real

Asked

Viewed 57 times

0

A colleague posted this code and I took advantage of it on my site more I would like to do a multiplication with the distance between the two points, for example the distance gave 1000 km I want to multiply this distance by 2 = R$ 2000,00... some can help me.

<script>
function CalculaDistancia() {
        $('#litResultado').html('Aguarde...');
        //Instanciar o DistanceMatrixService
        var service = new google.maps.DistanceMatrixService();
        //executar o DistanceMatrixService
        service.getDistanceMatrix(
          {
              //Origem
              origins: [$("#txtOrigem").val()],
              //Destino
              destinations: [$("#txtDestino").val()],
              //Modo (DRIVING | WALKING | BICYCLING)
              travelMode: google.maps.TravelMode.DRIVING,
              //Sistema de medida (METRIC | IMPERIAL)
              unitSystem: google.maps.UnitSystem.METRIC
              //Vai chamar o callback
          }, callback);
    }
    //Tratar o retorno do DistanceMatrixService
    function callback(response, status) {
        //Verificar o Status
        if (status != google.maps.DistanceMatrixStatus.OK)
            //Se o status não for "OK"
            $('#litResultado').html(status);
        else {
            //Se o status for OK
            //Endereço de origem = response.originAddresses
            //Endereço de destino = response.destinationAddresses
            //Distância = response.rows[0].elements[0].distance.text
            //Duração = response.rows[0].elements[0].duration.text
            var x = response.rows[0].elements[0].distance.text;
            $('#litResultado').html("<strong>Origem</strong>: "+ response.originAddresses +
            "<br /><strong>Destino:</strong> "+ response.destinationAddresses +
            "<br /><strong>Distância</strong>: "+ response.rows[0].elements[0].distance.text +
            " <br /><strong>Duração</strong>: "+ response.rows[0].elements[0].duration.text
            );
            //Atualizar o mapa
            $("#map").attr("src", "https://maps.google.com/maps?saddr=" + 
            response.originAddresses + "&daddr=" + response.destinationAddresses +  
            "&output=embed");
        }
    }
</script>
  • opa very good could not make it leave identado.

  • how do you do it

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.