问题描述
好的,所以我正在使用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});
});
解决方法
我认为您应该尝试像创建JSON对象一样,一旦完成,就同时创建每个标记。 (我不确定是否应该使用String,但是您知道了)。
// 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] });