views:

138

answers:

1

Hi all

I'm working on a GMaps application to retrieve images, via getJSON(), and to populate a popup marker.

The following is the markup which I add to the marker dynamically:

<div id="images"></div>                 
<div id="CampWindow" style="display:none;width:550px;height:500px;">
<h4 id="camp-title"></h4>
<p>View... (all links open in new windows)</p>
<ul>
    <li><a id="camp-hp-link" target="_blank" href="">camp home page</a></li>
    <li>information: <a id="camp-av-link" target="_blank" href="">availability</a> | <a id="camp-vi-link" target="_blank" href="">vital information</li>
</ul>
<p id="message"></p>

I've been clawing out my eyes and woohoo for the past couple of days, trying to get the images to show inside the CampWindow . Then, I decided to think laterally and to see if the images were being retrieved at all. I then moved the images outside and sure as Bob (Hope), the images were being retrieved and refreshed with every click.

So, I decided to the keep the images outside and then once loaded, append it to the CampWindow . It's not working still; when I append the div to the main CampWindow div, the images won't show. I check in Firebug with the pointer thingy and it shows me the images as empty. I try it again with the images outside and it shows the images. I've tried before append and appendTo with no success. Am I missing something here?

I have no more woohoo to claw out. Please, please help.

  marker.clicked = function(marker){
    $("#images").html('');
    $('#camp-title').text(this.name);
    $('#camp-hp-link').attr('href', this.url);
    $('#camp-av-link').attr('href', this.url + '/tourism/availability.php');
    $('#camp-vi-link').attr('href', this.url + '/tourism/general.php');          

    // get resort images via jQuery AJAX call - includes/GetResortImages.inc.php
    $.getJSON('./includes/GetResortImages.inc.php', { park: this.park_name, camp: this.camp_name }, RetrieveImages);

    function RetrieveImages (data)
    {
      if ('failed' == data.status)
      {
        $('#messages').append("<em>We don't have any images for this rest camp right now!</em>");
      }
      else
      {
        if ('' != data.camp)
        {   
          $.each(data, function(key,value){
              $("<img/>").attr("src", value).appendTo('#images');
          }); 
        }
      }
    }
          //.append($("#images"));
    $("#CampWindow").show(); 
    var windowContent = $("<html />");              
    $("#CampWindow").appendTo(windowContent);        

    var infoWindowAnchor = marker.getIcon().infoWindowAnchor;
    var iconAnchor = marker.getIcon().iconAnchor;
    var offset = new google.maps.Size(infoWindowAnchor.x-iconAnchor.x,infoWindowAnchor.y-iconAnchor.y);
    map.openInfoWindowHtml(marker.getLatLng(), windowContent.html(), {pixelOffset:offset});             
  }
  markers.push(marker);
});
+2  A: 

When you add the <html> tag to your page it confuses the browser and is most likely the problem. I would suggest to either do as Pointy said and use window.open() to make a popup window (check out this tutorial), or better yet try out one of the many jQuery light box plugins.


I'm not sure what you are doing with the google maps, so I decided to just go with a basic example for you. With this script, if you click on an image inside the #image div, it'll open a popup window the same size as the image.

$(document).ready(function(){
 $('#images img').click(function(){
  var padding = 20;
  var w = $(this).width() + padding;
  var h = $(this).height() + padding;
  var popup = '\
   <html>\
    <head>\
     <link type="text/css" href="popup-style.css" rel="stylesheet" />\
     <scr'+'ipt type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"&gt;&lt;/scr'+'ipt&gt;\
    </head>\
    <body>\
    <img src="' + $(this).attr('src') + '">\
    </body>\
   </html>';
  var pop = window.open('','Image View','toolbar=0,location=0,status=0,width=' + w + ',height=' + h + ',scrollbars=1,resizable=1');
  pop.document.write(popup);
  pop.document.close();
 })
});

NOTE: When adding a script tag inside a string, make sure you break up the word "script" otherwise you will get an error.


Update #2: Ok, since you want to work with what you have, try doing this:

Remove the <html> tag from your campwindow, then position your campwindow using CSS and/or javascript. Add something like:

 var w = $(window).width();
 var h = $(window).height();
 // Add overlay and make clickable to hide popup
 // you can remove the background color and opacity if you want to make it invisible
 var $overlay = $('<div/>', {
  'id': 'overlay',
  css: {
   position   : 'absolute',
   height     : h + 'px',
   width      : w + 'px',
   left       : 0,
   top        : 0,
   background : '#000',
   opacity    : 0.5,
   zIndex     : 99
  }
 }).appendTo('body');
 // Position your popup window in the viewport
 $('#CampWindow').css({
  position: 'absolute',
  top     : $(window).scrollTop() + 50 + 'px',
  left    : w/2 - $('#CampWindow').width()/2 + 'px', // centers the popup
  zIndex  : 100
 })
 .fadeIn('slow');
 // Click overlay to hide popup
 $('#overlay').click(function(){
  $('#CampWindow').hide();
  $(this).remove(); // remove the overlay
 })
fudgey
Hi guys Thanks a lot for your responses. The thing is, that although I agree that it may be an unelegant solution, it was working before I wanted to append divs to the marker popup. I guess that in itself says it all. Could you advise on how to go about reworking the marker popup generation with window.open()?
Midiane
Hi, I've updated my answer. It makes the `#CampWindow` into a popup. Note, the code I used does require jQuery v1.4+. I hope it helps!
fudgey
@fudgey thank you so much for the detailed answer. will try update #2 solution first; if not, shall try the 1st one. thanks muchly.
Midiane
@fudgey: 2nd solution worked perfectly! thanks a lot. could you just guide me now on how to incorporate the actual GMaps marker code/popup?
Midiane
I've never used GMaps before, so I would suggest asking a separate question. But just guessing, if GMap works inside of an iframe, you could put the iframe inside of your `#CampWindow` and see if it works.
fudgey
I've found myself a dozen of on-line tutorials. Will figure it out. Thanks again muchly
Midiane