Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: google map api / alternatywne trasy
Forum PHP.pl > Forum > Po stronie przeglądarki
qbas-s
Wygenerowałem sobie mapę z alternatywnymi trasami pomiędzy dwoma punktami.
Jak napisać listener aby po kliknięciu po kliknięciu na konkretną trasą pobierał jej id?(w sumie to jakikolwiek parametr, abym na jego podstawie podstawie mógł rozpoznać która trasa została wybrana)
werdan
Nie wiem jak rysujesz trasy.

Czy to polyline czy directionsDisplay

Jeśli polyline, mozesz podczas tworzenia dodac jakis id, a potem go pobrac np. poprzez:


  1.  
  2. google.maps.event.addListener(routePath, 'click', function(h) { ..... });
  3.  
qbas-s
Tak też mam aktualnie. Napisałem sobie taką funkcję(sorry za czytelność ale funkcja jest po kompresji a wersję developerską gdzieś mi wcieło)

  1. function calcRoute() {
  2. if (trasa != null)
  3. trasa.setMap(null);
  4. var e = document.getElementById("autocomplete").value;
  5. var t = document.getElementById("autocomplete_to").value;
  6. var n = [];
  7. var r = document.getElementById("additional_places").value;
  8. if (r.trim() != '') {
  9. var i = r.toString().split(";");
  10. for (var s = 0; s < i.length; s++) {
  11. if (i[s] !== "") {
  12. n.push({location: i[s], stopover: true})
  13. }
  14. }
  15. }
  16. var o = {origin: e, destination: t, waypoints: n, optimizeWaypoints: false, provideRouteAlternatives: true, travelMode: google.maps.TravelMode.DRIVING};
  17. var u = new google.maps.DirectionsService;
  18. u.route(o, function (e, t) {
  19. if (t == google.maps.DirectionsStatus.OK) {
  20.  
  21. var r = document.getElementById("distance");
  22. var i = document.getElementById("dist_inp");
  23.  
  24. for (var ind = 0, len = e.routes.length; ind < len; ind++) {
  25.  
  26. var n = e.routes[ind];
  27.  
  28. r.innerHTML = "";
  29. var s = 0;
  30. for (var o = 0; o < n.legs.length; o++) {
  31. s += parseInt(n.legs[o].distance.value)
  32. }
  33. r.innerHTML = s / 1e3;
  34. i.value = s / 1e3;
  35.  
  36.  
  37. if (ind > 0) {
  38. polylineOptions = {map: map, strokeColor: "#c7c3c7", strokeOpacity: .7, strokeWeight: 5, path: e.routes[ind].overview_path};
  39. } else {
  40. polylineOptions = {map: map, strokeColor: "#FF0000", strokeOpacity: .7, strokeWeight: 5, path: e.routes[ind].overview_path};
  41. }
  42. trasa = new google.maps.Polyline(polylineOptions);
  43. map.fitBounds(e.routes[ind].bounds);
  44. }
  45.  
  46.  
  47. }
  48. });
  49. }


jeśli na koniec pętli for dodam sobie

  1. google.maps.event.addDomListener(trasa, 'click', function () {
  2. alert('lorem');
  3. });


to alert działa ale wiadomo - tylko w przypadku ostatniej trasy

=======
ok mam

  1. trasa.addListener("click", function () {
  2. alert(this)
  3. }.bind(ind));
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.