谷歌地图v3向圆圈添加信息窗口

发布于 2024-11-18 07:59:00 字数 710 浏览 2 评论 0原文

有没有办法将 infoWindow 添加到由 google.maps.Circle 创建的圆圈中

var circ = new google.maps.Circle({
            center:latlng,
            clickable:false,
            fillColor:colors[count],
            fillOpacity:0.3, 
            map:map,
            radius:radius,
            strokeColor:colors[count],
            strokeOpacity:0.3});

或者

//create info window
var infoWindow= new google.maps.InfoWindow({
    content: "Your info here"
    });

//add a click event to the circle
google.maps.event.addListener(circ, 'click', function(){
//call  the infoWindow
infoWindow.open(map, circ);
}); 

有没有办法在圆圈中心创建一个不可见的标记,可以单击该标记来访问 infoWindow

Is there a way to add a infoWindow to a circle created by google.maps.Circle

something like this

var circ = new google.maps.Circle({
            center:latlng,
            clickable:false,
            fillColor:colors[count],
            fillOpacity:0.3, 
            map:map,
            radius:radius,
            strokeColor:colors[count],
            strokeOpacity:0.3});

and

//create info window
var infoWindow= new google.maps.InfoWindow({
    content: "Your info here"
    });

//add a click event to the circle
google.maps.event.addListener(circ, 'click', function(){
//call  the infoWindow
infoWindow.open(map, circ);
}); 

or alternatively is there a way to create an invisible marker at the center of the circle that can be clicked on to access an infoWindow

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

说不完的你爱 2024-11-25 07:59:00

您可以为您的圆圈叠加层提供信息窗口。但你必须稍微调整你的代码。

首先,需要为您的 Circle 叠加层设置 clickable=true (否则不会处理圆圈上的点击事件)。

然后你必须更改点击监听器的代码。将 Circle 作为函数 open() 的参数没有任何效果(Circle 不是正确的 MVCObject 类型,有关解释,请阅读 InfoWindow.open() 函数)。要显示信息窗口,您必须提供其位置 - 例如点击事件的位置、圆的中心......。

然后代码是

google.maps.event.addListener(circ, 'click', function(ev){
    infoWindow.setPosition(ev.latLng);
    infoWindow.open(map);
});

google.maps.event.addListener(circ, 'click', function(ev){
    infoWindow.setPosition(circ.getCenter());
    infoWindow.open(map);
});

回答您的评论
您可以使用不可见的标记创建一个技巧(只需将完全透明的图像作为标记图标),但我更喜欢使用圆形覆盖的解决方案。

you can have info window for your circle overlay. But you have to slightly adjust your code.

First, it is necessary to set clickable=true for your Circle overlay (otherwise click events on the circle are not processed).

Then you have to change the code of the click listener. Putting circle as a parameter to function open() has no effect (Circle is not the proper kind of MVCObject, for explanation read documentation of InfoWindow.open() function). To display the info window you have to provide its position - e.g. position of the click event, center of the circle, ....

The code is then

google.maps.event.addListener(circ, 'click', function(ev){
    infoWindow.setPosition(ev.latLng);
    infoWindow.open(map);
});

or

google.maps.event.addListener(circ, 'click', function(ev){
    infoWindow.setPosition(circ.getCenter());
    infoWindow.open(map);
});

Answer to your comment:
You can create a trick with an invisible marker (just put fully transparent image as the marker icon), but I would prefer solution with Circle overlay.

仅此而已 2024-11-25 07:59:00

在鼠标单击的位置设置信息窗口

google.maps.event.addListener(circ, 'click', function(ev){
    infoWindow.setPosition(ev.latLng); //<-- ev matches what you put ^ (mouse event)
    infoWindow.open(map);
});

to set info window at where mouse clicked

google.maps.event.addListener(circ, 'click', function(ev){
    infoWindow.setPosition(ev.latLng); //<-- ev matches what you put ^ (mouse event)
    infoWindow.open(map);
});
三五鸿雁 2024-11-25 07:59:00

我也有同样的困惑。我简单地使用解决了它

const circle = new google.maps.Circle({
            strokeColor: "#FFFF00",
            strokeOpacity: 0.8,
            strokeWeight: 1,
            fillColor: "#FFFF00",
            fillOpacity: 0.4,
            center: position,    // { lat: 46.021667, lng: 11.124717 }
            radius: distance,
            map: map,

        });

        const info = "Radius: " + circle.getRadius() + "m";
        const iw = new google.maps.InfoWindow({
            content: info,
            position: position,
        });
        circle.addListener("mouseover", () => {
            iw.open({
                map,
            });
        });
        circle.addListener("mouseout", () => {
            iw.close();
        });
        circle.addListener("click", () => {
            circle.setMap(null);
        });

I got same confusion. I simply solved it using

const circle = new google.maps.Circle({
            strokeColor: "#FFFF00",
            strokeOpacity: 0.8,
            strokeWeight: 1,
            fillColor: "#FFFF00",
            fillOpacity: 0.4,
            center: position,    // { lat: 46.021667, lng: 11.124717 }
            radius: distance,
            map: map,

        });

        const info = "Radius: " + circle.getRadius() + "m";
        const iw = new google.maps.InfoWindow({
            content: info,
            position: position,
        });
        circle.addListener("mouseover", () => {
            iw.open({
                map,
            });
        });
        circle.addListener("mouseout", () => {
            iw.close();
        });
        circle.addListener("click", () => {
            circle.setMap(null);
        });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文