2014-06-19 49 views
0

我面临一个问题,我不知道如何面对它。我有一个数组(数据库中的一组记录)和循环遍历数组。在循环中,我使用Google Maps api创建标记对象。标记采用与各种信息(位置,相关地图等)设置的关键值对。它还有一个标题,我想指定一个特定的文本来包含特定于该标记的信息。然而,当我遍历代码时,似乎我总是以所有标记引用的相同字符串结束(即,只有一个标题消息在点击时显示,并且仅与单个标记相关,尽管它出现在所有标记如何在循环中生成唯一的字符串对象

我的代码是:

for (record = 0; record < numRecords; record++){ 

     zomLat =parseFloat(data[record][1]); 
     zomLng = parseFloat(data[record][2]); 
     posterName = data[record][3]; 
     headline = data[record][7]; 
     details = data[record][8]; 
     type = data[record][4]; 
     num = data[record][5]; 
     timePosted = data[record][6]; 
     mapLabel = data[record][9];  //this is the key info I'd like added to each marker 
     //creates a position object for the marker 
     zomLATLNG = getCustomLatLng(Lat,Lng); 


     marker = new google.maps.Marker(

            { 
            position: zomLATLNG, 
            map: window.map, 
            icon: window.Icon, 
            draggable:true, 
            title://How can I add a unique string here? 
            }); 


    //and add a click listener 
    google.maps.event.addListener(marker, 'click', function() 
           { 
           alert(marker.title); 

           }); 

    window.zomMarkers.push(marker); 

    } 

我想我需要做的是每个回路,克里特岛一个新的独立字符串对象(主要由数据[记录中定义的mapLabel对象的副本时但是经过很多小时的努力,我似乎​​无法正确理解。

非常感谢您提供任何想法或帮助。

+0

您可以参考这个 http://stackoverflow.com/questions/3059044/google-maps-js- api-v3-simple-multiple-marker-example – ryacii

+2

使用'this.title'而不是'marker.title' –

+0

你需要'var marker = new google ...'? – bloodyKnuckles

回答

0

正如Dr.Molle建议的非常有帮助,使用this.title代替marker.title解决了这个问题。在将标题键分配给标记选项时,我直接使用了mapLabel变量。

这里

最终代码(非常感谢Dr.Molle!):

for (record = 0; record < numRecords; record++){ 

     zomLat =parseFloat(data[record][1]); 
     zomLng = parseFloat(data[record][2]); 
     posterName = data[record][3]; 
     headline = data[record][7]; 
     details = data[record][8]; 
     type = data[record][4]; 
     num = data[record][5]; 
     timePosted = data[record][6]; 
     mapLabel = data[record][9];  //this is the key info I'd like added to each marker 
     //creates a position object for the marker 
     zomLATLNG = getCustomLatLng(Lat,Lng); 


     marker = new google.maps.Marker(

            { 
            position: zomLATLNG, 
            map: window.map, 
            icon: window.Icon, 
            draggable:true, 
            title:mapLabel 
            }); 


    //and add a click listener 
    google.maps.event.addListener(marker, 'click', function() 
           { 
           alert(this.title); 

           }); 

    window.zomMarkers.push(marker); 

    } 
相关问题