2017-07-18 1 views
0

J'ai essayé d'ouvrir toutes les infoWindows du marqueur dès que la carte est chargée en premier. Cependant "infoWindow.open (carte, marqueur [i]);" ou d'autres variations ne fonctionne pas. Même j'ai essayé une autre boucle à l'intérieur mais je n'ai pas pu le faire. Donc vous avez des suggestions à ce sujet?Comment ouvrir tous les infowindows par défaut lorsque la carte est chargée?

var spLocations_length = spLocations.length; 

// Display multiple markers on a map 
var infoWindow = new google.maps.InfoWindow(), 
    marker, i; 

var iconCounter = 0; 
// Loop through our array of markers & place each one on the map 
for (i = 0; i < spLocations_length; i++) { 
    var position = new google.maps.LatLng(spLocations[i][1], spLocations[i][2]); 
    bounds.extend(position); 
    marker = new google.maps.Marker({ 
     position: position, 
     map: map, 
     title: spLocations[i][0], 
     icon: icons[iconCounter] 
    }); 
    // Allow each marker to have an info window 
    google.maps.event.addListener(marker, 'click', (function (marker, i) { 
     return function() { 
      infoWindow.setContent(infoWindowContent[i][0]); 
      infoWindow.open(map, marker); 
     } 
    })(marker, i)); 

    // IT DOESN'T WORK FOR OPEN ALL MARKERS INFOWINDOWS WHEN MAP LOADED 
    infoWindow.open(map, marker[i]); 



    // Automatically center the map fitting all markers on the screen 
    map.fitBounds(bounds); 
} 

Répondre

0

Enfin, j'ai trouvé une solution, mais c'est une façon indirecte. A la place, en ouvrant tous les infowindows, j'ai utilisé la solution "Marker Label". L'astuce consiste à utiliser l'image "Marker Icon" comme arrière-plan au lieu du conteneur infowindow. Par conséquent, le texte de l'étiquette change dynamiquement à travers la boucle for et le tableau.

J'espère que cet extrait aidera quelqu'un.

// locations Array contains location coords and titles // 
var locations[.....]; 

for (i = 0; i < locations.lenght; i++) { 

    var position = new google.maps.LatLng(locations[i][1], locations[i][2]); 

    marker = new google.maps.Marker({ 
    position: position, 
    map: map, 
    title: locations[i][0], 
    icon: "https.......", 
    index: i, 
    label: { 
     text: locations.length + locations[i][0] "This is text", 
     color: 'black', 
     fontSize: "14px", 
     fontFamily: "Roboto, Arial, sans-serif", 
     fontWeight: "bold" 
    } 

    }); 
} 

Google Source ici: https://developers.google.com/maps/documentation/javascript/examples/marker-labels