我想在Googlemap气泡中显示数组的内容。
我不知道下面的代码为什么不能正常工作,因为我只看到标题,而没有看到气泡中的新闻。

function addMarker(latitude, longitude, title, news)
{
    var marker = new GMarker(new GLatLng(latitude, longitude));

    GEvent.addListener(marker, 'click',function() {
        marker.openInfoWindowHtml(title, news);
    });

    map.addOverlay(marker);
}

function init()
{
    if (GBrowserIsCompatible())
    {
        map = new GMap2(document.getElementById("map"));
        map.addControl(new GSmallMapControl());
        map.setCenter(new GLatLng(centerLatitude, centerLongitude), startZoom);

        for (id = 0; id < markers.length; ++id)
        {
            addMarker(markers[id].latitude, markers[id].longitude, markers[id].title, markers  [id].news); //
        }
    }
}

window.onload = init;
window.onunload = GUnload;


这是数组结构:

var markers = [
{
    'latitude': 56.7382105,
    'longitude': 12.8584020,
    'title': 'Hallo Planet',
    'news': 'blaaaa blaaaa blaaaa.'
},
{
    'latitude': 62.6549167,
    'longitude': 16.6354329,
    'title': 'Hallo World',
    'news': 'bla bla bla.'
},
];


在括号}之后,我也尝试了不使用逗号,但结果完全相同。

最佳答案

Google的“ openInfoWindowHtml”接受两个参数,但不能接受。请参见their documentation on this

要在该信息窗口中设置标题和正文的格式,您需要将“ title”和“ news”元素组合为一个字符串(使用HTML格式)。或者,您可以将“ openInfoWindow”与现成的预制DOM节点结合使用。尝试:

marker.openInfoWindowHtml("<h1>" + title + "</h1> " + news);


这样效果更好吗?

09-30 13:44
查看更多