2013-07-09 1 views
0

지도에서 처음 클릭하면 마커가 표시됩니다. 마커 주소를 클릭하면 경고가 먼저 울리고 다음 다시 마커를 클릭하면 마커를 클릭합니다. 다음 번에 두 번 경고가 표시됩니다. 세 번째로지도를 클릭 한 다음 마커에 표시된 3 번 경고를 클릭하면 계속됩니다. 마커 클릭에 대한 경고가 여러 번 발생하는 이유는 무엇입니까? 클릭 이벤트가 여러 번 발생하는 이유는 무엇입니까? 도와주세요.클릭 이벤트 화재 여러 번 마커를 초기화하는 경우 Google지도에서 여러 번

 <!DOCTYPE html> 
    <html> 
     <head> 
     <title>Accessing arguments in UI events</title> 
     <meta name="viewport" content="initial-scale=1.0, user-scalable=no"> 
     <meta charset="utf-8"> 
     <link  href="http://code.google.com//apis/maps/documentation/javascript/examples/default.css" rel="stylesheet" type="text/css"> 
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script> 
<script> 
    var infowindow; 
    function initialize() { 
     infowindow = new google.maps.InfoWindow(); 
      var mapOptions = { 
zoom: 2 , 
center: new google.maps.LatLng(-25.363882,131.044922), 
mapTypeId: google.maps.MapTypeId.ROADMAP 
}; 
     map = new google.maps.Map(document.getElementById('map-canvas'), 
     mapOptions); 
     markerForWhatsHere = new google.maps.Marker({ 
      // position: position, 
      map: map 
     }); 
     google.maps.event.addListener(map, 'click', function(e) { 
      // placeMarker(e.latLng, map); 
     whatsHereEventLatLng=e.latLng; 
      whatsHere(); 
      }); 

       } 
     var markerForWhatsHere; 
      var map ; 
      function whatsHere(){ 
//alert("whats here latlng is "+ whatsHereEventLatLng); 
if (markerForWhatsHere != undefined) 
{ 
var addressForWhatsHere; 
var tmpName; 
markerForWhatsHere.setMap(null); 
// google.maps.event.clearListeners(map, 'click'); 
markerForWhatsHere.setPosition(null); 
markerForWhatsHere.setMap(map); 
markerForWhatsHere.setPosition(whatsHereEventLatLng);// updating marker positin for whats here 
var image = "http://www.google.com/intl/en_ALL/mapfiles/marker_greenA.png"; 
var geocoder = new google.maps.Geocoder(); 
geocoder.geocode({ 
latLng:whatsHereEventLatLng 
       }, function(responses,status) 
       { 
        if (responses && responses.length > 0) 
        { 
        addressForWhatsHere=responses[0].formatted_address; //for address 
        // alert("address is "+address); 
        for (i = 0; i < responses[0].address_components.length; ++i)//gettin city name where right click happen 
         { 
          for (j = 0; j < responses[0].address_components[i].types.length; ++j) 
          { 
          if (responses[0].address_components[i].types[j] == "locality") //for city 
           { 
            city = responses[0].address_components[i].long_name; 
           } 
          } 
          } //end code for getting city 
        } 
        if(typeof(city)=='undefined') 
         city='Unknown'; 
       if(typeof(address)=='undefined') 
        address='Unknown'; 
        infowindow.setContent("<b>Address : </b>"+addressForWhatsHere+" <br><b>City : </b>"+city+"<br><b> Lat.-lng. : </b>"+whatsHereEventLatLng); 
        infowindow.open(map, markerForWhatsHere); 
        markerForWhatsHere.setIcon(image); 
        var splitAddress=[]; 
        splitAddress=address.split(","); 
        tmpName=splitAddress[0]; 
        var markerTitle=image; 


       });//end of geocoder 


       google.maps.event.addListener(markerForWhatsHere, 'click', function (event) 
         { 
        alert("addressForWhatsHere "+addressForWhatsHere); 
        if (event.alreadyCalled_) { 
         // alert('circle clicked again'); 
        } 
        else { 
        var markerTitle=markerForWhatsHere.getIcon(); 
         // alert("marker title is "+markerTitle); 

          if(markerTitle.indexOf("green")!=-1) 
          {   //for marker selection 
           setMarkerTitle=markerTitle.replace("green","yellow");//changing color of marker 
           markerForWhatsHere.setIcon(setMarkerTitle); 

          }//end if marker is green     //end of marker selection 

        if(markerTitle.indexOf("yellow")!=-1) 
          {//for marker unselection 
               setMarkerTitle=markerTitle.replace("yellow","green"); //changing color of marker 
     markerForWhatsHere.setIcon(setMarkerTitle); 
      }//end if marker is yellow 
       event.alreadyCalled_ = true;  } 
        });//end of marker click   
} 
infowindow.close(); 

      } 

       google.maps.event.addDomListener(window, 'load', initialize); 

       </script> 
       </head> 
       <body> 
        <div id="map-canvas"></div> 
       </body> 
       </html> 

답변

0

당신이 (첫 번째 후)지도를 클릭 할 때마다 당신은 마커 이동 :

if (markerForWhatsHere != undefined) 
{ 
    // ... 
markerForWhatsHere.setPosition(whatsHereEventLatLng); 

를하지만 해 새로운 '클릭'이벤트 리스너를 추가 :

google.maps.event.addListener(markerForWhatsHere, 'click', function (event) 
{ 
    alert("addressForWhatsHere "+addressForWhatsHere); 
    if (event.alreadyCalled_) { 
     // alert('circle clicked again'); 
    } 
    else { 
    var markerTitle=markerForWhatsHere.getIcon(); 
    if(markerTitle.indexOf("green")!=-1) 
    { //for marker selection 
     setMarkerTitle=markerTitle.replace("green","yellow");//changing color of marker 
     markerForWhatsHere.setIcon(setMarkerTitle); 
    }//end if marker is green     //end of marker selection 
    if(markerTitle.indexOf("yellow")!=-1) 
    { //for marker unselection 
     setMarkerTitle=markerTitle.replace("yellow","green"); //changing color of marker 
     markerForWhatsHere.setIcon(setMarkerTitle); 
    } //end if marker is yellow 
    event.alreadyCalled_ = true; 
    } 
});//end of marker click   

마커가 처음에 정의되지 않은 경우에만 클릭 수신기를 추가하거나 마커를 삭제하고 다시 생성하십시오.

working example

+0

나중에, 난 내 마커를 이동하고, 난 경우 코드가 맞다하지만 난 마커에 주소가지도를 클릭에없는 클릭합니다, 당신에 따라 나는 새로운 '클릭'이벤트 리스너를 추가하고 있지만, 새를 생성하기 전에 클릭 목록을 지우십시오. 다시 작동하지 않습니다. –

+0

예를 들어 마커 클릭이 아니라 맵 클릭에 경고가 표시됩니다.이 예제는 내 req를 충족시키지 못합니다. –

+0

언제나 우리가 whatshere 메서드를 호출 할 때 마커에 새로운 클릭 이벤트가 추가되면 click listner를 initlize 함수에 추가하면 올바르게 작동합니다. 감사합니다. @geocodeZip –