我有谷歌地图..标记从xml获取数据..一切正常..但我想写信息从xml到标签..但我不知道怎么做..请看我的代码..帮助我:((
var gmarkers = [];
var gicons = [];
map = null;
var iconImage = new google.maps.MarkerImage('images/intake.png',
new google.maps.Size(20, 34),
new google.maps.Point(0,0),
new google.maps.Point(9, 34));
var iconShadow = new google.maps.MarkerImage('http://www.google.com/mapfiles/shadow50.png',
new google.maps.Size(37, 34),
new google.maps.Point(0,0),
new google.maps.Point(9, 34));
var iconShape = {
coord: [9,0,6,1,4,2,2,4,0,8,0,12,1,14,2,16,5,19,7,23,8,26,9,30,9,34,11,34,11,30,12,26,13,24,14,21,16,18,18,16,20,12,20,8,18,4,16,2,15,1,13,0],
type: 'poly'
};
var contentString = [
'<div id="tabs">',
'<ul>',
'<li><a href="#tab-1"><span>Info</span></a></li>',
'<li><a href="#tab-2"><span>Info</span></a></li>',
'<li><a href="#tab-3"><span>Photo</span></a></li>',
'</ul>',
'<div id="tab-1">',
' i must add info here',
'</div>',
'<div id="tab-2">',
'<p>and here</p>',
'</div>',
'<div id="tab-3">',
'<p>Any info and photo will be here.. <br/> <img src="images/logo.gif" height="60" ></p>',
'</div>',
'</div>'
].join('');
var infowindow = new google.maps.InfoWindow(
{
content: contentString
});
// A function to create the marker and set up the event window
function createMarker(latlng,name,html,category) {
var contentString = html;
var marker = new google.maps.Marker({
position: latlng,
icon: gicons[category],
shadow: iconShadow,
map: map,
title: name,
zIndex: Math.round(latlng.lat()*-100000)<<5
});
// === Store the category and name info as a marker properties ===
marker.mycategory = category;
marker.myname = name;
gmarkers.push(marker);
google.maps.event.addListener(marker, 'click', function() {
infowindow.setContent(contentString);
infowindow.open(map,marker);
});
}
// == shows all markers of a particular category, and ensures the checkbox is checked ==
function show(category) {
for (var i=0; i<gmarkers.length; i++) {
if (gmarkers[i].mycategory == category) {
gmarkers[i].setVisible(true);
}
}
// == check the checkbox ==
document.getElementById(category+"box").checked = true;
}
// == hides all markers of a particular category, and ensures the checkbox is cleared ==
function hide(category) {
for (var i=0; i<gmarkers.length; i++) {
if (gmarkers[i].mycategory == category) {
gmarkers[i].setVisible(false);
}
}
// == clear the checkbox ==
document.getElementById(category+"box").checked = false;
// == close the info window, in case its open on a marker that we just hid
infowindow.close();
}
// == a checkbox has been clicked ==
function boxclick(box,category) {
if (box.checked) {
show(category);
} else {
hide(category);
}
}
function initialize() {
var myOptions = {
zoom: 8,
center: new google.maps.LatLng(40.0000, 48.0000),
mapTypeId: google.maps.MapTypeId.ROADMAP
}
map = new google.maps.Map(document.getElementById("map"), myOptions);
google.maps.event.addListener(infowindow, 'domready', function() {
$("#tabs").tabs();
});
google.maps.event.addListener(map, 'click', function() {
infowindow.close();
});
这是xml
// Read the data
downloadUrl("xml/cat.xml", function(doc) {
var xml = xmlParse(doc);
var markers = xml.documentElement.getElementsByTagName("marker");
for (var i = 0; i < markers.length; i++) {
// obtain the attribues of each marker
var lat = parseFloat(markers[i].getAttribute("lat"));
var lng = parseFloat(markers[i].getAttribute("lng"));
var point = new google.maps.LatLng(lat,lng);
var address = markers[i].getAttribute("address");
var id = markers[i].getAttribute("id");
var name = markers[i].getAttribute("name");
var html = contentString;
var category = markers[i].getAttribute("category");
// create the marker
var marker = createMarker(point,name,html,category);
}
// == show or hide the categories initially ==
show("intake");
hide("reservoir");
hide("wps");
show("wtp");
hide("wwps");
hide("wwtp");
// == create the initial sidebar ==
});
toggleKML = function(x) {
i = x;
//alert(i);
//alert(toggleArray[0]);
//alert(toggleArray[1]);
//alert(toggleArray[2]);
if (toggleArray[i] == 1) {
kmlArray[i].setMap(null);
toggleArray.splice(i,1,'0');
}
else {
kmlArray[i].setMap(map);
toggleArray.splice(i,1,'1');
}
}}
</script>
伙计们请帮助我((..
超过12个小时我为此工作但它不起作用((..
如何将xml数据放入contentstring?
我的xml示例也是..
<marker code="20070" id="WWS7" name="8 Wells Nisaqala Gallery" title="NWSSP-2" capacity="9.84" completed="0" lat="xxx" lng="xx" category="intake"/>
答案 0 :(得分:0)
您需要根据标记上的click事件设置infowindow的内容。您最需要使用闭包来完成此操作。现在你每次都设置相同的内容,因为contentString
永远不会改变。您只需要使用XML文件中的值在Click事件处理程序中设置该内容。
答案 1 :(得分:0)
我已经意识到你要求的东西,你可以在这里查看我的代码:http://turom-mice.it/condorincentive/incentive.html(使用Firebug来获取脚本和相关的XML文件) 代码尽可能地被评论,但如果您需要解释,请询问。 代码是此处各种搜索和阅读以及Google Maps v3 API /示例的结果。
希望它有所帮助。 皮疹*