2014-05-04 124 views
1

当我将一个标记插入数组时,它会正确地绘制标记并向数组添加标题。Google Maps Api从标记获得标题

我知道这个工程,因为当我console.log console.log(markersArray);它返回以下内容。标记的标题是next door。下面

enter image description here

的是,打开信息窗口我标记的点击,但是当我CONSOLE.LOG出被称为mll数据它没有它里面的称号。

google.maps.event.addListener(marker, 'click', function(mll) { 
    console.log(mll); 
    var html= "<div style='color:#000;background-color:#fff;padding:5px;width:150px;'><p>"+mll+"</p></div>"; 
    iw = new google.maps.InfoWindow({content:html}); 
    iw.open(map,marker); 
}); 

enter image description here

我如何将能够得到click功能拉在title当阵列有它,并成功地推动了吗?

+1

如果你看看[文件](https://developers.google.com/maps/documentation/javascript/reference#Marker)的标记, click事件返回一个MouseEvent作为处理函数的参数(你的'mll');你可能想使用'this'这个应该是对标记的引用(所以'this.getTitle()'将返回标题)。 – geocodezip

回答

1

我记得之前做过类似的事情。在我的情况下,我用了圆圈标记的infoWindows。实质上,我有两个单独的数组。当我制作圆形标记时,我给它一个独特的值,称为地点,它基本上是计数(创建的第n的值为n)。在事件监听器上,我根据当前圆圈的位置从另一个数组中调用了infoWindow。

您可以使数组var titles = [];拥有标题。 每次您制作新标记时,都会增加一个var count = 0;,记录您拥有多少标记。 在您的标记选项中,添加place: count。当你需要一个特定的标题,你可以调用titles[marker.place];

var infos = []; 
    var count = 0; 

    var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions); 

    var lastWindow; 

    count++; 
    var populationOptions = { 
    //leaving out defaults 
    place: count 
    }; 

    var circle = new google.maps.Circle(populationOptions); 
    lastCircle = circle; 

    var contentString = 'just a string...'; 

    var infowindow = new google.maps.InfoWindow({ 
    content: contentString, 
    position: new google.maps.LatLng(data.lon, data.lad) 
    }); 

    infos.push(infowindow); 

    google.maps.event.addListener(circle, 'mouseover', function() { 
    if(lastWindow){ 
     lastWindow.close(); 
    } 

    infos[circle.place].open(map); 
    lastWindow = infos[circle.place]; 
    });