单击googlemap标记时执行功能

时间:2019-07-22 22:40:12

标签: javascript

我想点击googlemap标记来调用一个函数。我在这里阅读了一些答复,但没有一个对我有用。单击标记时没有任何反应。以下是我的googlemap代码。函数testCase()没有被调用。请注意,此代码包含一些应忽略的JSP代码。

var locations = [];

        <%
                for(Object t: posts)
                {
                    ReportModel md = (ReportModel) t;
                    String toSplit[] = md.getCaseLocation().split(",");

        %>

      locations [locations.length] = ['<%=md.getCategory()%>', <%=toSplit[0]%>, <%=toSplit[1]%>,<%=md.getCode()%>, 4];

        <%
                }
        %>
      console.log(locations);



    var map = new google.maps.Map(document.getElementById('map'), {
      zoom: 14,
      zoomControl: false,
      center: new google.maps.LatLng(6.4549, 3.4246),
      mapTypeId: google.maps.MapTypeId.ROADMAP
    });

    var infowindow = new google.maps.InfoWindow();

    var marker, i;

    for (i = 0; i < locations.length; i++) {  
      marker = new google.maps.Marker({
        position: new google.maps.LatLng(locations[i][1], locations[i][2]),
        map: map
      });

      google.maps.event.addListener(marker, 'click', (function(marker, i) {
        return function() {

          testCase(locations[i][3]);
        }
      })(marker, i));


function testCase(id)
{
    alert(id);
}

1 个答案:

答案 0 :(得分:0)

您可以尝试将侦听器添加到标记:

    marker.addListener('click', function() {
        console.log('TEST CLICKED TO ENSURE THAT IT WORKS')
        testCase(locations[i][3])
    })

参考文献: