【问题标题】:How to display all the fields in a array with JavaScript?如何使用 JavaScript 显示数组中的所有字段?
【发布时间】:2011-12-09 22:52:27
【问题描述】:

我想在 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.'
},
];

我也试过在大括号}后面不加逗号,但结果完全一样。

【问题讨论】:

    标签: javascript arrays google-maps-markers


    【解决方案1】:

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

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

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

    这样效果更好吗?

    【讨论】:

    • 非常感谢!确实更好
    • 酷。请务必接受答案(就像上面描述的 Yoshi)。
    猜你喜欢
    • 2017-12-25
    • 2015-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-27
    • 2016-09-08
    相关资源
    最近更新 更多