0

好的,所以我使用gmap显示来自 xml 文件的许多位置,该文​​件存储城市、州、html 和有关某些位置的其他信息。这里的问题是很可能有超过 100 个位置。

在 NEW 我的 ajax 调用中:

    var myMarkers = new Array;
$(xml).find("location").each(function(){
    var locAddress = $(this).find("city").text() + "," + $(this).find("state").text() ;
    var cc = $(this).find("cc").text();
    var bcc; 
    if ($(this).find("bcc")){ 
            bcc = $(this).find("bcc").text();
        }else{
            bcc = " - ";
        }
    var vendor =$(this).find("vendor").text();
    var hours = $(this).find("hours").text();
    var HTMLString =  "<div class=\"map-balloon\"><h3>" + locAddress + "</h3>Vendor: " + vendor + "<br />CC#: " + cc + "<br />Bulk CC#: " + bcc + "<br />Hours: " + hours + "</div>" ;

    myMarkers.push("{ address: \"" + locAddress + "\", html: \"" + HTMLString + "\"}");

});

$("#map").gMap({ markers: [myMarkers.toString()], address: "United States", zoom: 4 }); // shows the correct zoom and region, but markers do not display now.
console.log(myMarkers.toString());//Shows the correct string we want

问题是它每次都加载,firefox 讨厌它并去计算,它在 IE7 中工作。

我的问题是,动态设置多个标记的最佳方法是什么?这是新的工作代码:

var myMarkers = new Array;  

    $.ajax({
    url: "tire-banks.xml",
    dataType: ($.browser.msie) ? "text" : "xml",
    async: false,
    success: function(data){
                    var xml;    
                    if (typeof data == "string") {
                       xml = new ActiveXObject("Microsoft.XMLDOM");
                      // xml.async = false;
                       xml.loadXML(data);
                    } else {
                       xml = data;
                    }

                    $(xml).find("location").each(function(){
                        var locAddress = $(this).find("city").text() + "," + $(this).find("state").text() ;
                        var cc = $(this).find("cc").text();
                        var bcc; 
                        if ($(this).find("bcc")){ 
                            bcc = $(this).find("bcc").text();
                            }else{
                            bcc = " - ";
                            }
                        var vendor =$(this).find("vendor").text();
                        var hours = $(this).find("hours").text();
                        var HTMLString =  "<div><h3>" + locAddress + "</h3>Vendor: " + vendor + "<br />CC#: " + cc + "<br />Bulk CC#: " + bcc + "<br />Hours: " + hours + "</div>" ;

                        myMarkers.push({ address: locAddress, html: HTMLString});   
                    });
4

1 回答 1

0

我认为你应该尝试像你一样创建 JSON 对象,一旦完成,同时创建每个标记。(我不确定它是否是你应该使用的字符串,但你明白了)。

// With something like :
var myMarkers = "";
// 'Each' loop {
 myMarkers += "{ address: "+locAddress+", html: "+HTMLString+"},";
// End 'Each' loop }
// Don't forget to remove the trailing ','
 $("#map").gMap({ markers: [myMarkers] });
于 2011-06-14T14:28:22.340 回答