2013-10-28 33 views
1

我在Google地图上显示地址时遇到问题。我尝试了一切,但它不工作。它只显示第一个标记,即使我点击其他地址。我怎样才能让它显示其他标记?只有在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); 

回答

1

所有的链接(锚点标记)都具有相同的ID属性,所以当函数运行时,它会选择最后一个从中获取标记数据。

尝试为每个链接使用不同的ID属性并发送ID作为参数的函数。

+0

谢谢你的回复。 我注意到,但是因为数据是使用xsl从XML文件中读取的,所以很棘手。它循环读取数据,因此我正在寻找一种替代方法! – Zen

+0

它不应该是棘手的,你只需要改变每个链接的ID。我假设每个地址在数据库中都有不同的ID,为什么不使用它呢? – amosmos

+0

不,数据标签在XML文档内部是相似的,这就是为什么我使用xsl循环并获取数据的原因。我试图通过tagName,名称和其他不同的方式获取元素,但它不起作用。尝试标记阵列,它并没有工作,我是新手谷歌地图 – Zen