-1

我正在尝试将背景图像添加到谷歌地图信息框内容内的 div 中。我遇到了问题,因为我用来更改 .css('background-image') 的函数无法识别 div 的 ID。这可能是因为打开信息框时内容字符串尚未被 DOM 加载。我知道我必须添加一个监听“domready”或类似的事件,但我不确定如何实现它。这是有问题的代码:

var boxContent = var boxContent = "<div id=box><div id=leftside><img id=logo src=" + 
                    logopath + " /><h1>" + number + "<br />" + street + 
                    "<br />" + city + "</h1><h2>" + phone + 
                    "<br /><a target=_blank href=" + website + 
                    ">visit website</a></h2></div><div id=menu>" +
                    "<button id=photo onClick=buttonState(this.id)><img src=infomenu/photoicon.png></button></button>" +
                    "<button id=comments onClick=buttonState(this.id)><img src=infomenu/commentsicon.png></button>" +
                    "<button id=games onClick=buttonState(this.id)><img src=infomenu/gamesicon.png></button>" +
                    "<button id=ed onClick=buttonState(this.id)><img src=infomenu/edicon.png></button>" +
                    "<button id=stay onClick=buttonState(this.id)><img src=infomenu/stayicon.png></button>" +
                    "<button id=fly onClick=buttonState(this.id)><img src=infomenu/flyicon.png></button>"  +
                    "<div id=display></div>"

var myOptions = {
        content: boxContent,
        disableAutoPan: false,
        pixelOffset: new google.maps.Size(-400, -173),
        closeBoxMargin: "-12px -12px 0px 0px",
        closeBoxURL: "close.png",
        infoBoxClearance: new google.maps.Size(1, 1),
        pane: "floatPane",
        enableEventPropagation: false
   };

var ib = new InfoBox(myOptions);

  google.maps.event.addListener(marker, 'click', function(e) {
        map.setZoom(13);
        map.setCenter(this.getPosition());  
        ib.open(map, this, displayphotos(imagepath);
  });

function displayphotos(imagepath){

       $('#display').css('background-image', 'url(' + imagepath + '1.png)');

}
4

1 回答 1

0

InfoBox.open只需要 2 个参数 - 地图和锚点。您需要单独设置该属性。我发现持有对boxContentDOM 元素的引用可以让你的生活更轻松。

var boxContent = $("<div id='display'></div>");
var myOptions = {
    content: boxContent[0],
    disableAutoPan: false,
    pixelOffset: new google.maps.Size(-400, -173),
    closeBoxMargin: "-12px -12px 0px 0px",
    closeBoxURL: "http://www.carltonhotelblanchardstown.com/files/images/a/calendar-popup-close.png",
    infoBoxClearance: new google.maps.Size(1, 1),
    pane: "floatPane",
    enableEventPropagation: false
};

http://jsfiddle.net/Jeff_Meadows/ujxSq/

于 2013-03-26T19:00:57.213 回答