Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> google map api / alternatywne trasy
qbas-s
post
Post #1





Grupa: Zarejestrowani
Postów: 304
Pomógł: 1
Dołączył: 28.06.2009

Ostrzeżenie: (0%)
-----


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)
Go to the top of the page
+Quote Post
werdan
post
Post #2





Grupa: Zarejestrowani
Postów: 354
Pomógł: 100
Dołączył: 14.11.2013
Skąd: Płock

Ostrzeżenie: (0%)
-----


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.  
Go to the top of the page
+Quote Post
qbas-s
post
Post #3





Grupa: Zarejestrowani
Postów: 304
Pomógł: 1
Dołączył: 28.06.2009

Ostrzeżenie: (0%)
-----


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));


Ten post edytował qbas-s 27.08.2015, 16:05:29
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 21.08.2025 - 13:20