Google 地图:如何通过单击打开多边形的信息窗口?
我有一个简单的问题,但我无法在 Google Maps API 文档中找到答案...
我有一张由 API 绘制的包含 13 个多边形的地图。这是这些多边形之一的示例:
var zone_up_montblanc = new GPolygon([
new GLatLng(46.21270329318585, 6.134903900311617),
new GLatLng(46.20538443787925, 6.136844716370282),
new GLatLng(46.20525043957647, 6.141375978638086),
new GLatLng(46.20698751554006, 6.148050266912262),
new GLatLng(46.21110286985207, 6.153203229026629),
new GLatLng(46.21730757985668, 6.151718301267355),
new GLatLng(46.22092122197341, 6.153676364285801),
new GLatLng(46.22615123408969, 6.149844858907489),
new GLatLng(46.22851200024137, 6.149876939987202),
new GLatLng(46.22945159836955, 6.142758190170017),
new GLatLng(46.21735908463437, 6.141457132705133),
new GLatLng(46.21753573755057, 6.138058122426195),
new GLatLng(46.21270329318585, 6.134903900311617)
], "#6b1f43", 2, 0.9, "#92c87f", 0.5);
然后:
map.addOverlay(zone_up_montblanc);
多边形出现在我的地图上,没问题。 但我现在要做的事情是通过单击每个多边形(每个多边形具有不同的内容)来打开“信息窗口”。
有人有想法或例子吗?
非常感谢您的帮助!
I have a simple question, but i can't find the answer in the Google Maps API documentation...
I have a map with 13 polygons drawed by the API. Here is an exemple of one of these polygons :
var zone_up_montblanc = new GPolygon([
new GLatLng(46.21270329318585, 6.134903900311617),
new GLatLng(46.20538443787925, 6.136844716370282),
new GLatLng(46.20525043957647, 6.141375978638086),
new GLatLng(46.20698751554006, 6.148050266912262),
new GLatLng(46.21110286985207, 6.153203229026629),
new GLatLng(46.21730757985668, 6.151718301267355),
new GLatLng(46.22092122197341, 6.153676364285801),
new GLatLng(46.22615123408969, 6.149844858907489),
new GLatLng(46.22851200024137, 6.149876939987202),
new GLatLng(46.22945159836955, 6.142758190170017),
new GLatLng(46.21735908463437, 6.141457132705133),
new GLatLng(46.21753573755057, 6.138058122426195),
new GLatLng(46.21270329318585, 6.134903900311617)
], "#6b1f43", 2, 0.9, "#92c87f", 0.5);
then :
map.addOverlay(zone_up_montblanc);
Polygons appears on my map, no problem.
But the thing I have to do now is to open an "InfoWindow" by clicking on each polygons (with a different content for each polygons).
Did someone have an idea or an example?
Thanks a lot for your help !
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我找到了一个非常好的解决方案,可以使用多个带有单独信息窗口的多边形。
这是我的代码:
它还提供了通过再次单击同一多边形来关闭信息窗口的功能。
I found a very nice solution to have multiple polygons with individual infowindows.
here is my code:
It Provide also the function to close the Infowindows by clicking on the same polygon again.
你好,非常感谢 filip-fku!
感谢您的评论,我终于找到了如何做到这一点! :-)
因此,如果有人搜索“如何执行此操作”,这里是代码片段:
希望这可以有所帮助!
再次感谢菲利普! :)
Hi and thanks a lot filip-fku !
thanks to your comment, i finnaly find how to do this! :-)
so, if anyone search for "how to do this", here is the code snippet :
hope this can helps !
thanks again filip! :)
不需要全局信息窗口。在事件处理程序工作时初始化信息窗口。
使用此原型检查该点是否包含在多边形数组中的多边形实例中。
http://hammerspace.co.uk/projects/maps/
有一个功能示例http://www.ikeepuposted.com/area_served.aspx
No global infowindow needed. initialize the infowindow in side the event handler works.
use this protoype to check if the point is contained in an instance of the polygon in a array of polygons.
http://hammerspace.co.uk/projects/maps/
There is a functioning example at http://www.ikeepuposted.com/area_served.aspx
我将描述该解决方案,因为我有一段时间没有使用 API,并且很难上传大量代码 - 不习惯这里的代码编辑功能。有关详细信息,请参阅 API 参考。
那么,让我们开始吧:
就这么简单!您将必须查找如何附加事件处理程序,因为我不记得具体细节。因此,您需要在 API 中查找的内容是:
您应该能够在 api 页面上找到要调用的方法和所有信息:
http://code.google.com/apis/maps/documentation/reference.html
祝你好运!
I will describe the solution because I haven't used the API in a while, and struggle to upload any larger amounts of code - not used to the code edit feature here. Refer to the API reference for the details.
So, lets start:
It's a simple as that! You will have to look up how you attach event handlers as I don't remember the specifics off the top of my head. So things you need to look up in the API are:
You should be able to find the methods to call and all info on the api page:
http://code.google.com/apis/maps/documentation/reference.html
Good luck!
在V3中,您仍然会使用信息窗口,但使用不同的语法:
任何您想要更改文本并显示信息窗口的地方:
我已经制作了多边形和事件监听器全局变量(通过在变量声明中抑制“var”),以便您可以在其他功能中更改它们。这对于信息窗口的行为来说确实很棘手。有些人更喜欢 infowindow 的实例,我宁愿有一个全局实例并动态更改其内容。当心变量声明的效果!
其他用于多边形的 addListener 有很多错误,应该在发布之前在所有主要浏览器上进行测试,尤其是 DRAGGING 和 MOUSEOVER 的变体。
这有一些参考:http://code.google.com/apis/地图/文档/javascript/overlays.html
in V3, you would still use the infowindow, but with different syntax:
anywhere you feel like changing the text and displaying the infowindow:
i have made the polygon and eventlistener global variables (by suppressing "var" in the variable declaration), so that you may alter them in other functions. that does prove tricky with the infowindow behavior. some people prefer instances of infowindow, i would rather have one global instance and alter its content on the fly. beware of the effect of variable declarations!
other addListener for polygons are quite buggy and should be tested on all major browsers before publication, especially the variations of DRAGGING and MOUSEOVER.
this has some reference: http://code.google.com/apis/maps/documentation/javascript/overlays.html