我正在使用下面的 Google 地图代码从使用 PHP 动态创建的 XML 文件中检索和绘制多个地址的标记。除了在谷歌地图信息窗口中显示相应标记的正确信息外,该代码正在做我需要的一切。我得到了所有标记的最后一个 XML 项目/列表的信息。
我一直在寻找并尝试不同的变化来让它工作,但没有运气。
样本 XML 数据
<?xml version="1.0" encoding="UTF-8"?>
<listings>
<listing>
<address>123 Street</address>
<city>MANOTICK</city>
</listing>
<listing>
<address>456 Street</address>
<city>MANOTICK</city>
</listing>
<listing>
<address>111 Avenue</address>
<city>MANOTICK</city>
</listing>
<listing>
<address>777 Avenue</address>
<city>Ottawa</city>
</listing>
<listing>
<address>333 Street</address>
<city>Manotick</city>
</listing>
</listings>
谷歌地图代码
function initialize ()
{
var myLatLng = new google.maps.LatLng(45.2340684, -75.6287287);
var myOptions =
{
zoom: 10,
mapTypeControl: true,
center: myLatLng,
zoomControl: true,
zoomControlOptions:
{
style: google.maps.ZoomControlStyle.SMALL
},
StreetViewControl: false,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
var map = new google.maps.Map(document.getElementById('google_map'), myOptions);
var info_window = new google.maps.InfoWindow;
google.maps.event.addListener
(map, 'click',
function ()
{
info_window.close();
});
downloadUrl
('listings.xml',
function (listings_data)
{
var markers = listings_data.documentElement.getElementsByTagName('listing');
var geocoder = new google.maps.Geocoder();
for (var i = 0; i < markers.length; i++)
{
var address = markers[i].getElementsByTagName('address')[0].firstChild.data;
var city = markers[i].getElementsByTagName('city')[0].firstChild.data;
var address_google_map = address + ', ' + city + ', ON';
var info_text = address + '<br />' + city + ' ON';
geocoder.geocode
({'address': address_google_map},
function (results)
{
var marker = new google.maps.Marker
({
map: map,
position: results[0].geometry.location
});
google.maps.event.addListener
(marker, 'click',
function()
{
info_window.setContent(info_text);
info_window.open(map, marker);
});
});
}
});
}