2012-06-29 70 views
0

你只能有一個window.location.href?谷歌地圖點擊標記功能問題

問題只是點擊時標記的最後點擊功能是正確的。其餘的甚至是輸出是不同的(這是正確的),他們總是打開最後一個網址。

注意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);   

回答

1

這是關於函數關閉。當你在循環內部創建標記時,var URLonClick的值不斷更新,所以最後它有最後一個值。 解決方案是在您傳遞必要參數的單獨函數中創建標記。

良好的閱讀關於這個問題,在這裏:

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

0

問題是marker.url

您將要覆蓋在每個循環的標記變量,所以marker.url總是指向的URL - 已創建的最後一個標記的成員。

使用this.url,而不是marker.url訪問點擊標記的網址成員。