Google Maps API Multiple Markers with Infowindows

前端 未结 5 1660
挽巷
挽巷 2020-11-30 17:45

I am trying to add multiple markers each with its own infowindow that comes up when clicked on. I am having trouble with getting the infowindows coming up, when I try it eit

5条回答
  •  一整个雨季
    2020-11-30 18:13

    function setMarkers(map,locations){
    
    for (var i = 0; i < locations.length; i++)
     {  
    
     var loan = locations[i][0];
     var lat = locations[i][1];
     var long = locations[i][2];
     var add =  locations[i][3];
    
     latlngset = new google.maps.LatLng(lat, long);
    
     var marker = new google.maps.Marker({  
              map: map, title: loan , position: latlngset  
     });
     map.setCenter(marker.getPosition());
    
    
     marker.content = "

    Loan Number: " + loan + '

    ' + "Address: " + add; google.maps.events.addListener(marker,'click', function(map,marker){ map.infowindow.setContent(marker.content); map.infowindow.open(map,marker); }); } }

    Then move var infowindow = new google.maps.InfoWindow() to the initialize() function:

    function initialize() {
    
        var myOptions = {
          center: new google.maps.LatLng(33.890542, 151.274856),
          zoom: 8,
          mapTypeId: google.maps.MapTypeId.ROADMAP
    
        };
        var map = new google.maps.Map(document.getElementById("default"),
            myOptions);
        map.infowindow = new google.maps.InfoWindow();
    
        setMarkers(map,locations)
    
      }
    

提交回复
热议问题