Guest User

Untitled

a guest
Jul 15th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.01 KB | None | 0 0
  1. <!DOCTYPE html>
  2.  
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head>
  5. <title>Calcular distancia entre cidades (mapas e rotas)</title>
  6. <script src="http://code.jquery.com/jquery-1.8.1.js" type="text/javascript"></script>
  7. </head>
  8. <body>
  9. <!-- Parâmetro sensor é utilizado somente em dispositivos com GPS -->
  10. <script src="http://maps.google.com/maps/api/js?sensor=false"></script>
  11. <script type="text/javascript">
  12. function CalculaDistancia() {
  13. $('#litResultado').html('Aguarde...');
  14. //Instanciar o DistanceMatrixService
  15. var service = new google.maps.DistanceMatrixService();
  16. //executar o DistanceMatrixService
  17. service.getDistanceMatrix(
  18. {
  19. //Origem
  20. origins: [$("#txtOrigem").val()],
  21. //Destino
  22. destinations: [$("#txtDestino").val()],
  23. //Modo (DRIVING | WALKING | BICYCLING)
  24. travelMode: google.maps.TravelMode.DRIVING,
  25. //Sistema de medida (METRIC | IMPERIAL)
  26. unitSystem: google.maps.UnitSystem.METRIC
  27. //Vai chamar o callback
  28. }, callback);
  29. }
  30. //Tratar o retorno do DistanceMatrixService
  31. function callback(response, status) {
  32. //Verificar o Status
  33. if (status != google.maps.DistanceMatrixStatus.OK)
  34. //Se o status não for "OK"
  35. $('#litResultado').html(status);
  36. else {
  37. //Se o status for OK
  38. //Endereço de origem = response.originAddresses
  39. //Endereço de destino = response.destinationAddresses
  40. //Distância = response.rows[0].elements[0].distance.text
  41. //Duração = response.rows[0].elements[0].duration.text
  42. $('#litResultado').html("<strong>Origem</strong>: " + response.originAddresses +
  43. "<br /><strong>Destino:</strong> " + response.destinationAddresses +
  44. "<br /><strong>Distância</strong>: " + response.rows[0].elements[0].distance.text +
  45. " <br /><strong>Duração</strong>: " + response.rows[0].elements[0].duration.text
  46. );
  47. //Atualizar o mapa
  48. $("#map").attr("src", "https://maps.google.com/maps?saddr=" + response.originAddresses + "&daddr=" + response.destinationAddresses + "&output=embed");
  49. }
  50. }
  51. </script>
  52. <table width="100%" cellspacing="0" cellpadding="0" border="0">
  53. <tbody>
  54. <tr>
  55. <td>
  56. <label for="txtOrigem"><strong>Endere&ccedil;o de origem</strong></label>
  57. <input type="text" id="txtOrigem" class="field" style="width: 400px" />
  58.  
  59. </td>
  60. </tr>
  61. <tr>
  62. <td>
  63. <label for="txtDestino"><strong>Endere&ccedil;o de destino</strong></label>
  64. <input type="text" style="width: 400px" class="field" id="txtDestino" />
  65.  
  66. </td>
  67. </tr>
  68. <tr>
  69. <td>
  70. <input type="button" value="Calcular dist&acirc;ncia" onclick="CalculaDistancia()" class="btnNew" />
  71. </td>
  72. </tr>
  73. </tbody>
  74. </table>
  75. <div><span id="litResultado">&nbsp;</span></div>
  76. <div style="padding: 10px 0 0; clear: both">
  77. <iframe width="750" scrolling="no" height="350" frameborder="0" id="map" marginheight="0" marginwidth="0" src="https://maps.google.com/maps?saddr=são paulo&daddr=rio de janeiro&output=embed"></iframe>
  78. </div>
  79.  
  80. { "destinationAddresses" : [ "Rio de Janeiro - RJ, República Federativa do Brasil" ],
  81. "originAddresses" : [ "São Paulo - SP, República Federativa do Brasil" ],
  82. "rows" : [ { "elements" : [ { "distance" : { "text" : "430 km",
  83. "value" : 430395
  84. },
  85. "duration" : { "text" : "4 horas 42 minutos",
  86. "value" : 16942
  87. },
  88. "status" : "OK"
  89. } ] } ]
  90. }
Add Comment
Please, Sign In to add comment