谷歌地图 - 将InfoWindows附加到数组中的多边形

Cri*_*thy 3 javascript google-maps polygon infowindow

我整个早上一直用这个撞到墙上.我创建了一个多边形数组,并希望将每个将在infoWindow中显示的数据关联起来.我可以在地图上看到所有多边形.我添加了监听器,它会触发(颜色发生变化),但我没有得到infoWindow.任何帮助都会有很大的帮助!

干杯!

C...

tmppoly = new google.maps.Polygon({
   map: map,
   paths: polypath,
   strokeColor: scolor,
   strokeOpacity: 0.5,
   strokeWeight: 2,
   fillColor: fcolor,
   fillOpacity: 0.5
});

addPolygonClick(tmppoly,mdata);
plot_polygons.push(tmppoly);

...

function addPolygonClick(poly,html) {

    infowindow = new google.maps.InfoWindow(
    { 
        content: html
    });

    google.maps.event.addListener(poly,'click', function(event) {
        this.setOptions({fillColor: "#000000"});
        infowindow.open(map);
    }); 

}
Run Code Online (Sandbox Code Playgroud)

Gal*_*ery 8

我可以就您的问题提供一些有用的建议.

首先,你应该知道关于方法'infowindow.open(map,anchor?:MVCObject)'的表达式.正如google api v3所说:在核心API中,唯一的锚是Marker类.Polygon类与条件不匹配,但是,我们可以用另一种方式实现.

var polygon = new google.maps.Polygon({
    paths: PGpoints,   //The PGpoints is the collection of points around this polygon.
    map: map,
    strokeColor: colory,
    strokeOpacity: 0.6,
    strokeWeight: 1,
    fillColor: colory,
    fillOpacity: 0.35       
});

polygon.set("Info", idy);  // Set some attributes for adding extra information into this polygon.   

google.maps.event.addListener(polygon, 'click', function() {
    var infoWindow = new google.maps.InfoWindow();
    infoWindow.setContent("Information : " + polygon.get("Info"));

    // 'laty,lngy' is the location of one point in PGpoints, which can be chosen as you wish
    infoWindow.setPosition(new google.maps.LatLng(laty,lngy));     
    infoWindow.open(map);
});
Run Code Online (Sandbox Code Playgroud)

上面的代码已通过测试,您可以直接使用它.然后,如果单击一个多边形,infoWindow将出现在地图上方.