Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [Google Maps APIv3] Usuwanie markera
SpiJay
post
Post #1





Grupa: Zarejestrowani
Postów: 63
Pomógł: 2
Dołączył: 18.11.2009
Skąd: Grudziądz

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


Witam,

korzystam z geolokalizacji Google, aby określić współrzędne za pośrednictwem wyłącznie nazwy ulicy,

Kod:
[JAVASCRIPT] pobierz, plaintext
  1. function codeAddress() {
  2. var rozmiar = new google.maps.Size(20,34);
  3. var rozmiar_cien = new google.maps.Size(37,34);
  4. var punkt_startowy = new google.maps.Point(0,0);
  5. var punkt_zaczepienia = new google.maps.Point(10,34);
  6. var ikona_url = new google.maps.Point(15,12);
  7.  
  8. var ikona_green = new google.maps.MarkerImage("xxxxx/marker.png", rozmiar, punkt_startowy, punkt_zaczepienia);
  9. var ikona_green_shadow = new google.maps.MarkerImage("http://www.google.com/intl/en_ALL/mapfiles/shadow50.png", rozmiar_cien, punkt_startowy, punkt_zaczepienia);
  10.  
  11. var address = document.getElementById("address").value;
  12.  
  13. geokoder.geocode( {'address': 'Warszawa, ' + address}, function(results, status) {
  14. var marker;
  15. if (status == google.maps.GeocoderStatus.OK) {
  16. document.mapa.kod.value = results[0].geometry.location;
  17. mapa.setCenter(results[0].geometry.location);
  18. function updateMarkerPosition(latLng) {
  19. $("#kod").val( [
  20. latLng.lat(),
  21. latLng.lng()
  22. ].join(', '));
  23. }
  24. if( marker != null ) {
  25. marker.setPosition( results[0].geometry.location );
  26. updateMarkerPosition( results[0].geometry.location );
  27. } else {
  28. marker = new google.maps.Marker({
  29. map: mapa,
  30. icon: ikona_green,
  31. shadow: ikona_green_shadow,
  32. position: results[0].geometry.location,
  33. draggable: true,
  34. zoom: 5
  35. });
  36.  
  37. updateMarkerPosition(results[0].geometry.location);
  38. google.maps.event.addListener(marker, 'drag', function(local) {
  39. updateMarkerPosition(marker.getPosition());
  40. });
  41. }
  42.  
  43. } else {
  44. alert("Geocode was not successful for the following reason: " + status);
  45. }
  46. });
  47. }
[JAVASCRIPT] pobierz, plaintext


Do dodania markera na mapie używam przycisku,
  1. <input type="button" value="Sprawdź lokalizację" onclick="codeAddress()"/>

który kieruje się wartością (nazwą ulicy) wpisanej w polu input:
  1. <input type="text" value="Nazwa ulicy" id="address" />


Co mam zrobić, aby po ponownym kliknięciu na przycisk "Sprawdź lokalizację" usuwało poprzedni marker ?
Po wpisaniu innej nazwy ulicy i kliknięciu pokazuje się nowy marker z nową lokalizacją...

Pozdrawiam !

Ten post edytował SpiJay 2.01.2013, 13:58:59


--------------------
zmoderowano - waga i rozmiar
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi (1 - 3)
Pawel_W
post
Post #2





Grupa: Zarejestrowani
Postów: 1 675
Pomógł: 286
Dołączył: 15.06.2009
Skąd: Wieliczka

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


  1. if( marker != null ) {
  2. marker.setPosition( results[0].geometry.location );
  3. updateMarkerPosition( results[0].geometry.location );
  4. } else {
  5. marker = new google.maps.Marker({

przed tworzeniem nowego markera ustaw staremu marker.setMap(null) smile.gif

edit: chodzi mi oczywiście o linijkę po else

Ten post edytował Pawel_W 2.01.2013, 14:07:53
Go to the top of the page
+Quote Post
SpiJay
post
Post #3





Grupa: Zarejestrowani
Postów: 63
Pomógł: 2
Dołączył: 18.11.2009
Skąd: Grudziądz

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


Współrzędne są pokazywane ale nie dodaje mi w tym przypadku markera, który może być przesuwany sad.gif

[JAVASCRIPT] pobierz, plaintext
  1. if( marker != null ) {
  2.  
  3. marker.setPosition( results[0].geometry.location );
  4. updateMarkerPosition( results[0].geometry.location );
  5. } else {
  6. marker.setMap(null);
  7.  
  8. marker = new google.maps.Marker({
  9. map: mapa,
  10. icon: ikona_green,
  11. shadow: ikona_green_shadow,
  12. position: results[0].geometry.location,
  13. draggable: true,
  14. zoom: 5
  15. });
  16.  
  17. updateMarkerPosition(results[0].geometry.location);
  18. google.maps.event.addListener(marker, 'drag', function(local) {
  19. updateMarkerPosition(marker.getPosition());
  20. });
  21. }
[JAVASCRIPT] pobierz, plaintext


Ten post edytował SpiJay 3.01.2013, 13:28:16


--------------------
zmoderowano - waga i rozmiar
Go to the top of the page
+Quote Post
Pawel_W
post
Post #4





Grupa: Zarejestrowani
Postów: 1 675
Pomógł: 286
Dołączył: 15.06.2009
Skąd: Wieliczka

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


Cytat(SpiJay @ 2.01.2013, 13:55:15 ) *
geokoder.geocode( {'address': 'Warszawa, ' + address}, function(results, status) {
var marker;

nie wiem czy widzisz, ale robiąc w ten sposób marker będzie widoczny tylko w tej funkcji - efektem jest to, że tracisz do niego "zaczepienie" i nie możesz go potem usunać
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 - 20:21