2017-02-10 3 views
-1

このように閉じようとしているImはmarker.infowindow.close();ですが、動作しません。なにか提案を?ここでユーザーが他のピンをクリックすると、前の情報ウィンドウを閉じる方法は?

marker.infowindow = new google.maps.InfoWindow({ 
        noSupress: true, 
        content: '' 
       }); 
       marker.addListener('click', (function (marker, i) { 

        return function() { 


         var position = this.position; 
         property_id = locations[i].id; 
         lat = locations[i].lat; 
         lng = locations[i].lng; 
         name = locations[i].name; 
         address = locations[i].address; 
         default_image = locations[i].default_image; 
         $('.hightlight-'+property_id+' a').css('color', '#1996d5'); 

         html = '<div class="markerBox">' + '<div class="default_image"><img src="storage/uploads/property' + property_id + "/" + default_image + '" alt="' + name + '"/></div>' + '<div class="content"><h2><a href="/property/' + property_id + '">' + name + '</a></h2><h4><span>Address: </span>' + address + '</h4>' + '</div></div>'; 
         map.setCenter(marker.getPosition()); 
         // Reset pin 
         for (var ix = 0; ix < markers.length; ix++) { 
          markers[ix].setIcon(searchPinImage); 
         } 
         map.setZoom(14); 
         marker.setIcon(activePinImage); 


         this.infowindow.setContent(html); 
         //this.infowindow.setPosition(position); 
         this.infowindow.open(map); 
         this.infowindow.setPosition(position); 

         google.maps.event.addListener(infowindow, 'closeclick', function() { 
          //marker.setIcon(searchPinImage); 
         }); 

は私のバイオリンです:

http://jsfiddle.net/pGBZD/860/

+0

は、あなたはそれが、作業jsfiddleやスニペットを作成することができます。ちょうどこのコードで問題を見つけることは非常に困難だ、方法はinfowindow.closeある()https://で開発。 google.com/maps/documentation/javascript/3.exp/reference#InfoWindow、あなたのコードに他の問題がある可能性があります。 –

+0

@VladuIonut http://jsfiddle.net/pGBZD/860/ – None

+1

インフォウィンドウを新しいマーカーに移動してその内容を変更するだけではいけませんか? –

答えて

2

にあなたは、変数(currentWindow)に、最後のウィンドウを開いておく必要がありますし、しようとすると、他のウィンドウを開くには、最後に開いたものを閉じることができます(下のスニペットを参照してください)

var currentWindow ; 
    marker.addListener('click', (function(marker, i) { 
     return function() { 
     marker.infowindow.close(); 
      if (currentWindow) currentWindow.close(); 
      this.infowindow.setContent(locations[i][0], locations[i][6]); 
      this.infowindow.open(map, marker); 
      currentWindow = this.infowindow; 
     } 
     })(marker, i)); 
    } 

var locations = [ 
 
    [ 
 
     "New Mermaid", 
 
     36.9079, 
 
     -76.199, 
 
     1, 
 
     "Georgia Mason", 
 
     "", 
 
     "Norfolk Botanical Gardens, 6700 Azalea Garden Rd.", 
 
     "coming soon" 
 
    ], 
 
    [ 
 
     "1950 Fish Dish", 
 
     36.87224, 
 
     -76.29518, 
 
     2, 
 
     "Terry Cox-Joseph", 
 
     "Rowena's", 
 
     "758 W. 22nd Street in front of Rowena's", 
 
     "found" 
 
    ], 
 
    [ 
 
     "A Rising Community", 
 
     36.95298, 
 
     -76.25158, 
 
     3, 
 
     "Steven F. Morris", 
 
     "Judy Boone Realty", 
 
     "Norfolk City Library - Pretlow Branch, 9640 Granby St.", 
 
     "found" 
 
    ], 
 
    [ 
 
     "A School Of Fish", 
 
     36.88909, 
 
     -76.26055, 
 
     4, 
 
     "Steven F. Morris", 
 
     "Sandfiddler Pawn Shop", 
 
     "5429 Tidewater Dr.", 
 
     "found" 
 
    ], 
 
    [ 
 
     "Aubrica the Mermaid (nee: Aubry Alexis)", 
 
     36.8618, 
 
     -76.203, 
 
     5, 
 
     "Myke Irving/ Georgia Mason", 
 
     "USAVE Auto Rental", 
 
     "Virginia Auto Rental on Virginia Beach Blvd", 
 
     "found" 
 
    ] 
 
] 
 

 
    var map = new google.maps.Map(document.getElementById('map'), { 
 
     zoom: 12, 
 
     // center: new google.maps.LatLng(-33.92, 151.25), 
 
     center: new google.maps.LatLng(36.8857, -76.2599), 
 
     mapTypeId: google.maps.MapTypeId.ROADMAP 
 
    }); 
 

 
    var infowindow = new google.maps.InfoWindow(); 
 

 
    var marker, i; 
 

 
    for (i = 0; i < locations.length; i++) { 
 
     marker = new google.maps.Marker({ 
 
     position: new google.maps.LatLng(locations[i][1], locations[i][2]), 
 
     map: map 
 
     }); 
 
\t \t \t marker.infowindow = new google.maps.InfoWindow({ 
 
        noSupress: true, 
 
        content: '' 
 
       }); 
 
    var currentWindow ; 
 
    marker.addListener('click', (function(marker, i) { 
 
     return function() { 
 
     marker.infowindow.close(); 
 
      if (currentWindow) currentWindow.close(); 
 
      this.infowindow.setContent(locations[i][0], locations[i][6]); 
 
      this.infowindow.open(map, marker); 
 
      currentWindow = this.infowindow; 
 
     } 
 
     })(marker, i)); 
 
    }

 
<div> 
 
    <div id="map" style="width: 500px; height: 400px;"></div> 
 

 
<script src="http://maps.google.com/maps/api/js?sensor=false"></script> 
 
</div>

0

のtry変更:google.maps.event.addListener(infowindow, 'closeclick' google.maps.event.addListener(this.infowindow, 'closeclick'

+0

ユーザーが別のピンをクリックしたときに前の情報ウィンドウを閉じたい – None