Advertisement
Guest User

Untitled

a guest
Nov 15th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.14 KB | None | 0 0
  1. function initMap() {
  2. var map = new google.maps.Map(document.getElementById('harta'), {
  3. zoom: 7,
  4. center: {lat: 46.095968, lng: 24.915397}
  5. });
  6. var geocoder = new google.maps.Geocoder;
  7. var infowindow = new google.maps.InfoWindow;
  8.  
  9. document.getElementById('gasireLocatie1').addEventListener('click', function() {
  10. geocodareAdresa(geocoder, map, infowindow, 'adresa1');
  11. });
  12.  
  13. document.getElementById('gasireLocatie2').addEventListener('click', function() {
  14. geocodareAdresa(geocoder, map, infowindow, 'adresa2');
  15. });
  16.  
  17. document.getElementById('calculeazaDistanta').addEventListener('click', function() {
  18. calculeazaDistanta(geocoder, map, infowindow);
  19. });
  20. }
  21.  
  22. function geocodareAdresa(geocoder, map, infowindow, adresa) {
  23. var textAdresa = document.getElementById(adresa).value;
  24. geocoder.geocode({'address': textAdresa}, function(results, status) {
  25. if (status === 'OK') {
  26. if (results[0]) {
  27. map.setZoom(11);
  28. map.setCenter(results[0].geometry.location);
  29. var marker = new google.maps.Marker({
  30. map: map,
  31. position: results[0].geometry.location
  32. });
  33. infowindow.setContent(results[0].formatted_address);
  34. infowindow.open(map, marker);
  35. } else {
  36. window.alert('Niciun rezultat pentru locaţia introdusă!');
  37. }
  38. } else {
  39. window.alert('Geocodarea a eşuat: ' + status);
  40. }
  41. });
  42. }
  43.  
  44. function calculeazaDistanta(geocoder, map, infowindow)
  45. {
  46. var adresa1 = document.getElementById('adresa1').value;
  47. var adresa2 = document.getElementById('adresa2').value;
  48. var locatie1, locatie2;
  49. var marker1, marker2;
  50. geocoder.geocode({'address': adresa1}, function(results, status) {
  51. if (status === 'OK') {
  52. if (results[0]) {
  53. marker1 = new google.maps.Marker({
  54. map: map,
  55. position: results[0].geometry.location
  56. });
  57. locatie1 = results[0].geometry.location;
  58. infowindow.setContent(results[0].formatted_address);
  59. infowindow.open(map, marker1);
  60.  
  61. geocoder.geocode({'address': adresa2}, function(results, status) {
  62. if (status === 'OK') {
  63. if (results[0]) {
  64. marker2 = new google.maps.Marker({
  65. map: map,
  66. position: results[0].geometry.location
  67. });
  68. locatie2 = results[0].geometry.location;
  69. infowindow.setContent(results[0].formatted_address);
  70. infowindow.open(map, marker2);
  71.  
  72. var service = new google.maps.DistanceMatrixService();
  73. service.getDistanceMatrix({
  74. origins: [locatie1],
  75. destinations: [locatie2],
  76. travelMode: google.maps.TravelMode.DRIVING,
  77. unitSystem: google.maps.UnitSystem.METRIC,
  78. avoidHighways: false,
  79. avoidTolls: false
  80. }, function (response, status) {
  81. if (status == google.maps.DistanceMatrixStatus.OK && response.rows[0].elements[0].status != "ZERO_RESULTS") {
  82. var distance = response.rows[0].elements[0].distance.text;
  83. var duration = response.rows[0].elements[0].duration.text;
  84. var dvDistance = document.getElementById("distanta");
  85. dvDistance.innerHTML = "";
  86. dvDistance.innerHTML += "Distanţă în linie dreaptă: " +
  87. Math.round(google.maps.geometry.spherical.computeDistanceBetween(locatie1, locatie2) / 1000)
  88. + " km<br />";
  89. dvDistance.innerHTML += "Distanţă pe şosea: " + distance + "<br />";
  90. // dvDistance.innerHTML += "Durată drum: " + duration;
  91. dvDistance.style.display = "block";
  92.  
  93. } else {
  94. alert("Imposibil de găsit distanţa de parcurs.");
  95. }
  96. });
  97. } else {
  98. window.alert('Niciun rezultat pentru locaţia introdusă!');
  99. }
  100. } else {
  101. window.alert('Geocodarea a eşuat: ' + status);
  102. }
  103. });
  104. } else {
  105. window.alert('Niciun rezultat pentru locaţia introdusă!');
  106. }
  107. } else {
  108. window.alert('Geocodarea a eşuat: ' + status);
  109. }
  110. });
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement