Capture Coordinates In this Api Maps script

Asked

Viewed 131 times

2

In this scrupt below I enter the origin and the destination it returns me the km the time and data of the address among them. more my question is how to make it return me to latitude and longitude

<script type="text/javascript">
  function CalculaDistancia() {
    $('#litResultado').html('Aguarde...');
    // Instancia o DistanceMatrixService.
    var service = new google.maps.DistanceMatrixService();

    // Executa o DistanceMatrixService.
    service.getDistanceMatrix({
        origins: [$("#txtOrigem").val()], // Origem
        destinations: [$("#txtDestino").val()], // Destino
        travelMode: google.maps.TravelMode.DRIVING, // Modo (DRIVING | WALKING | BICYCLING)
        unitSystem: google.maps.UnitSystem.METRIC // Sistema de medida (METRIC | IMPERIAL)
    }, callback); // Vai chamar o 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".
        $("#litResultado").html("&nbsp;"); // Remove o "aguarde".

        // Popula os campos.
        $("#txtOrigemResultado").val(response.originAddresses);
        $("#txtDestinoResultado").val(response.destinationAddresses);
        $("#txtDistancia").val(response.rows[0].elements[0].distance.text); // se text Km se value em Metros

        /////////////////////////////////////////////////////////////////////////////////////////////////
        var tempo = response.rows[0].elements[0].duration.text;
        tempo = tempo.replace("day", "dia").replace("hour", "hora").replace("min", "min");
        $("#txtTempo").val(tempo);
        /////////////////////////////////////////////////////////////////////////////////////////////////




        var dist = (response.rows[0].elements[0].distance.value); //pego a distancia em metros
        var km = Math.round(dist / 1000); //converto para Km e arredondo



        }}
  • You want latitude and longitude than exactly? Because what this API does is to receive latitude and longitude of origin and destination and return only the data of "travel", such as distance, estimated time, etc. In this link you can see everything that is brought back on request.

  • Hello... Renato. then it returns the distance , time, etc... more need to receive also the coordinates of Origin and Destination

No answers

Browser other questions tagged

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