gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:09:40 24 4
gpt4 key购买 nike

我整个早上都在用这个把头撞在墙上。我创建了一个多边形数组,并希望将每个多边形中的一些数据关联起来,这些数据将显示在信息窗口中。我可以在 map 上看到所有的多边形。我添加了监听器,它触发了(颜色发生变化),但我没有得到 infoWindow。任何帮助将不胜感激!

干杯!

我...

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);
});

}

最佳答案

我可以针对您的问题提供一些有用的建议。

首先,你应该知道'infowindow.open(map, anchor?:MVCObject)'方法的表达式。正如 google api v3 所说:在核心 API 中,唯一的 anchor 是 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);
});

以上代码已经通过测试,可以直接使用。然后,如果您单击一个多边形, map 上方就会出现信息窗口。

关于javascript - 谷歌地图 - 将 InfoWindows 附加到数组中的多边形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7111046/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com