Getting infowindows to work within the loop

I am generating a map from meta data from a series of posts in WordPress. The map and points generate fine but when I click on the points the infowindow always generates in the same place and not on the point I click. Can someone please help with this, I had it working ok in v2 but I cant figure it out for 3.

The map is generated in the loop with this :

function initialize(){
    var myLatlng = new google.maps.LatLng(45, -123);
     var myOptions = {
            zoom: 3,
            center: myLatlng,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };
    var map = new google.maps.Map(document.getElementById('travel_map'), myOptions);
    <?php if ( have_posts() ) :  while ( have_posts() ) : the_post();
        $my_post = get_post($post_id);
        $mapTitle  = $my_post->post_title;
        $lat = get_post_meta($post->ID, 'latitude', true); 
        $long = get_post_meta($post->ID, 'longitude', true);
        ?>

    var contentString = 'test';

    var infowindow = new google.maps.InfoWindow({
        content: contentString
    });

     var marker = new google.maps.Marker({
        position: new google.maps.LatLng(<?php echo $lat . ", " . $long ?>),
        map: map
     });

    google.maps.event.addListener(marker, 'click', function() {
      infowindow.open(map,marker);
    }); 

<?php endwhile;  ?>
<?php endif; ?>
}

Related posts

Leave a Reply

1 comment

  1. http://you.arenot.me/2010/06/29/google-maps-api-v3-0-multiple-markers-multiple-infowindows/

    Here describes how to use multiple markers and multiple infowindows on your page.

    And here’s the implementation:

    <script type="text/javascript">
    
        var infowindow = null;
        var markers = [];
        function initialize(){
        var myLatlng = new google.maps.LatLng(45, -123);
         var myOptions = {
                zoom: 3,
                center: myLatlng,
                mapTypeId: google.maps.MapTypeId.ROADMAP
            };
    
        var map = new google.maps.Map(document.getElementById('travel_map'), myOptions);
    
        infowindow = new google.maps.InfoWindow({
            content: "holding..."
        });
    
        <?php if ( have_posts() ) :  while ( have_posts() ) : the_post();
            $my_post = get_post($post_id);
            $mapTitle  = $my_post->post_title;
            $lat = get_post_meta($post->ID, 'latitude', true); 
            $long = get_post_meta($post->ID, 'longitude', true);
            ?>
    
        var marker = new google.maps.Marker({
            position: new google.maps.LatLng(<?php echo $lat . ", " . $long ?>),
            map: map,
            html: "<b><?php echo $mapTitle; ?></b><br><br><?php echo $my_post; ?>"
        });
    
        markers.push(marker);
    
        google.maps.event.addListener(marker, 'click', function () {
            infowindow.setContent(this.html);
            infowindow.open(map, this);
        });
    
    
    <?php endwhile;  ?>
    <?php endif; ?>
    }
    </script>