0

你可以只有一个 window.location.href 吗?

问题只是标记的最后一次单击功能在单击时是正确的。其余的即使输出不同(这是正确的),它们总是打开最后一个 url。

注意cfloop。

  map = new google.maps.Map(document.getElementById("map"), options);
   // Create an Array for the Markers
   var markers = [];
     <cfloop array="#rc.details.poi.getPageRecords()#" index="local.poi">
      var latLng = new google.maps.LatLng(#local.poi.getpoiLat()#,#local.poi.getpoiLong()#);
      //var iconImg = '/assets/images/pin-50.png';

     <cfif !ArrayIsEmpty(local.poi.getimages())>
         var iconImg = new google.maps.MarkerImage("http://#CGI.HTTP_HOST#/plugins/api/index.cfm/image/#local.poi.getimages()[1].getimageID()#/50/50/get?apiKey=#application.factory.getBean('authenticationService').getAPIKey('image', 'GET')#&defaulttype=poi", null, new google.maps.Point(0,0), new google.maps.Point(0,0));
        <cfelse>
         var iconImg = new google.maps.MarkerImage("http://#CGI.HTTP_HOST#/plugins/api/index.cfm/image/1be71dec-a525-404f-a148-48ad74e46397/50/50/get?apiKey=#application.factory.getBean('authenticationService').getAPIKey('image', 'GET')#&defaulttype=poi", null, new google.maps.Point(0,0), new google.maps.Point(0,0));
      </cfif>

   var shadow = new google.maps.MarkerImage('/plugins/assets/images/AirTag50.png',
        // The shadow image is larger in the horizontal dimension
        // while the position and offset are the same as for the main image.
        new google.maps.Size(50, 80),
        new google.maps.Point(0,0),
        new google.maps.Point(0, 7));
        // Shapes define the clickable region of the icon.
        // The type defines an HTML <area> element 'poly' which
        // traces out a polygon as a series of X,Y points. The final
        // coordinate closes the poly by connecting to the first
        // coordinate.
    var shape = {
        coord: [1, 10, 10, 50, 55, 50,55 , 1],
        type: 'poly'
    };

    var URLonClick = "#buildURL(action='public:scape.view',queryString='scapeID=#local.poi.getScape().getscapeID()#&airtag=#local.poi.getpoiID()#')#";
     var marker = new google.maps.Marker({ position: latLng, map: map, draggable: false, title: 'Click to View AirTag', icon: iconImg, shadow: shadow, shape: shape, url: URLonClick });
     // Action Listener for the Marker
     google.maps.event.addListener(marker, 'click', function(event) {
            window.location.href = marker.url;
        });


      markers.push(marker);
    </cfloop>

    var mcOptions = {gridSize: 50, maxZoom: 15};
    var markerCluster = new MarkerClusterer(map, markers, mcOptions);        
4

2 回答 2

1

这是关于函数闭包的。当您在循环内创建标记时,var URLonClick 的值会不断更新,因此最终它具有您放置的最后一个值。解决方案是在一个单独的函数中创建标记,向该函数传递必要的参数。

关于这个主题的好读物,在这里:

http://econym.org.uk/gmap/closure.htm

于 2012-06-29T18:04:17.510 回答
0

问题是marker.url

您正在覆盖每个循环上的标记变量,因此marker.url将始终指向已创建的最后一个标记的 url 成员。

使用this.url而不是marker.url访问单击标记的 url 成员。

于 2012-07-02T10:51:55.673 回答