我在Google地图上显示地址时遇到问题。我尝试了一切,但它不起作用。即使我点击其他地址,它也只显示第一个标记。如何让它显示其他标记?
<xsl:for-each select="Attractions/Museums">
<a href="#maps" data-role="button" id="addMarker" onclick="addMarker();" data-theme="c"> <xsl:value-of select="address"/></a>
</xsl:for-each>
<script type="text/javascript">
var map;
var geocoder = new google.maps.Geocoder();
$( "#maps" ).on( 'pageshow', function() {
google.maps.event.trigger(map,'resize');
})
// initialize the map
function initialize() {
var mapOptions = {
zoom: 12,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("map-canvas"),
mapOptions);
function addMarker() {
var address = document.getElementById("addMarker").text;
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
var image = 'flag1.png';
var markers = new google.maps.Marker({
map: map,
icon: image,
position: results[0].geometry.location
});
} else {
alert("Geocode was not successful for the following reason: " + status);
}
});
}
google.maps.event.addDomListener(window, 'load', initialize);
答案 0 :(得分:1)
所有链接(锚标记)都具有相同的ID属性,因此当该函数运行时,它会选择最后一个来获取标记数据。
尝试为每个链接使用不同的ID属性,并发送ID作为参数的函数。