Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [JavaScript][HTML]Odświeżanie markerów google maps
Forum PHP.pl > Forum > Przedszkole
olszam
Mam zwykły skrypcik na losowe markery w mapce, ale niestety nie mogę sobie poradzić z odświeżaniem markerów co daną sekundę. Próbowałem z setInterval odświeżyć punkty, ale zamiast odświeżyć to dodawały kolejne markery, z setMap też próbowałem ale nie wychodzi mi.

Kod jaki mam na ten czas:
  1. html, body, #map {
  2. height: 100%;
  3. margin: 0px;
  4. padding: 0px
  5. }
  6. </style>
  7. <script src="http://maps.google.com/maps/api/js?v=3.exp" type="text/javascript"></script>
  8.  
  9. </head>
  10. <div id="map"></div>
  11.  
  12. <script type="text/javascript">
  13.  
  14. var map = new google.maps.Map(document.getElementById('map'), {
  15. zoom: 3,
  16. center: new google.maps.LatLng(0, 0),
  17. mapTypeId: google.maps.MapTypeId.ROADMAP
  18. });
  19.  
  20. var infowindow = new google.maps.InfoWindow();
  21. var marker , i;
  22.  
  23. var locations = [
  24. ['random5', Math.floor((Math.random() * 100) + 1), Math.floor((Math.random() * 100) + 1), 5],
  25. ['random4', Math.floor((Math.random() * 100) + 1), Math.floor((Math.random() * 100) + 1), 4],
  26. ['random3', Math.floor((Math.random() * 100) + 1), Math.floor((Math.random() * 100) + 1), 3],
  27. ['random2', Math.floor((Math.random() * 100) + 1), Math.floor((Math.random() * 100) + 1), 2],
  28. ['random1', Math.floor((Math.random() * 100) + 1), Math.floor((Math.random() * 100) + 1), 1]
  29. ];
  30.  
  31. for (i = 0; i < locations.length; i++) {
  32. marker = new google.maps.Marker({
  33. position: new google.maps.LatLng(locations[i][1], locations[i][2]),
  34. map: map
  35. });
  36.  
  37. google.maps.event.addListener(marker, 'click', (function(marker, i) {
  38. return function() {
  39. infowindow.setContent(locations[i][0]);
  40. infowindow.open(map, marker);
  41. }
  42. })(marker, i));
  43. }
  44. </script>
  45. </body>
  46. </html>
Turson
setInterval zadziała, ale przed dodaniem musisz wyczyścić tablicę z markerami, żeby dodać nowy
https://developers.google.com/maps/document...er-remove?hl=pl
olszam
byłem już na tej stronie i z początku nie pomogło bo trochę się pogubiłem porównując jeden kod z drugim, posiedziałem trochę i działa.
  1. html, body, #map {
  2. height: 100%;
  3. margin: 0px;
  4. padding: 0px
  5. }
  6. </style>
  7. <script src="http://maps.google.com/maps/api/js?v=3.exp" type="text/javascript"></script>
  8.  
  9. </head>
  10. <div id="map"></div>
  11.  
  12. <script type="text/javascript">
  13.  
  14. var map = new google.maps.Map(document.getElementById('map'), {
  15. zoom: 3,
  16. center: new google.maps.LatLng(0, 0),
  17. mapTypeId: google.maps.MapTypeId.ROADMAP
  18. });
  19.  
  20. var infowindow = new google.maps.InfoWindow();
  21. var marker, i, locations;
  22. var markers = [];
  23.  
  24. setInterval(function(){
  25.  
  26. locations = [
  27. ['random5', Math.floor((Math.random() * 100) + 1), Math.floor((Math.random() * 100) + 1), 5],
  28. ['random4', Math.floor((Math.random() * 100) + 1), Math.floor((Math.random() * 100) + 1), 4],
  29. ['random3', Math.floor((Math.random() * 100) + 1), Math.floor((Math.random() * 100) + 1), 3],
  30. ['random2', Math.floor((Math.random() * 100) + 1), Math.floor((Math.random() * 100) + 1), 2],
  31. ['random1', Math.floor((Math.random() * 100) + 1), Math.floor((Math.random() * 100) + 1), 1]
  32. ];
  33. setAllMap(null);
  34. for (i = 0; i < locations.length; i++) {
  35.  
  36. marker = new google.maps.Marker({
  37. position: new google.maps.LatLng(locations[i][1], locations[i][2]),
  38. map: map
  39. });
  40. markers.push(marker);
  41. google.maps.event.addListener(marker, 'click', (function(marker, i) {
  42. return function() {
  43. infowindow.setContent(locations[i][0]);
  44. infowindow.open(map, marker);
  45. }
  46. })(marker, i));
  47.  
  48. }
  49. }, 3000);
  50.  
  51. function setAllMap(map) {
  52. for (var i = 0; i < markers.length; i++) {
  53. markers[i].setMap(map);
  54. }
  55. }
  56. </script>
  57. </body>
  58. </html>

i jeszcze pytanie, które ze zdarzeń najlepiej by pasowało tak aby przy załadowaniu markerów pojawiła się chmurka na wszystkich markerach?
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.