Google 地图:如何通过单击打开多边形的信息窗口?

发布于 2024-09-01 13:47:12 字数 1170 浏览 3 评论 0原文

我有一个简单的问题,但我无法在 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 技术交流群。

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

发布评论

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

评论(5

じее 2024-09-08 13:47:12

我找到了一个非常好的解决方案,可以使用多个带有单独信息窗口的多边形。

这是我的代码:

<script type="text/javascript"
        src="https://maps.google.com/maps/api/js?sensor=false">
</script>
<script type="text/javascript" charset="utf-8">

    var map;
    var cities = {
        "Hamburg":[
            [53.60, 9.75],
            [53.73, 10.19],
            [53.48, 10.22]
        ],
        "Hannover":[
            [52.34, 9.61],
            [52.28, 9.81],
            [52.43, 9.85]
        ],
        "Bremen":[
            [53.22, 8.49],
            [53.12, 8.92],
            [53.02, 8.72]
        ]
    };
    var opened_info = new google.maps.InfoWindow();

    function init() {
        var mapOptions = {
            zoom:8,
            center:new google.maps.LatLng(53, 9.2),
            mapTypeId:google.maps.MapTypeId.TERRAIN
        };
        map = new google.maps.Map(document.getElementById('map_canvas'),
                mapOptions);
        for (var c in cities) {
            var coods = cities[c]
            var polyPath = [];
            for (j = 0; j < coods.length; j++) {
                polyPath.push(new google.maps.LatLng(coods[j][0], coods[j][1]));
            }
            var shape = new google.maps.Polygon({
                paths:polyPath,
                fillColor:"#00FF00",
                fillOpacity:0.6,
            });
            shape.infowindow = new google.maps.InfoWindow(
                    {
                        content:"<b>" + c + "</b>" + "</br>",
                    });
            shape.infowindow.name = c;
            shape.setMap(map);
            google.maps.event.addListener(shape, 'click', showInfo);
        }

    }
    ;

    function showInfo(event) {
        opened_info.close();
        if (opened_info.name != this.infowindow.name) {
            this.infowindow.setPosition(event.latLng);
            this.infowindow.open(map);
            opened_info = this.infowindow;
        }
    }

</script>

它还提供了通过再次单击同一多边形来关闭信息窗口的功能。

I found a very nice solution to have multiple polygons with individual infowindows.

here is my code:

<script type="text/javascript"
        src="https://maps.google.com/maps/api/js?sensor=false">
</script>
<script type="text/javascript" charset="utf-8">

    var map;
    var cities = {
        "Hamburg":[
            [53.60, 9.75],
            [53.73, 10.19],
            [53.48, 10.22]
        ],
        "Hannover":[
            [52.34, 9.61],
            [52.28, 9.81],
            [52.43, 9.85]
        ],
        "Bremen":[
            [53.22, 8.49],
            [53.12, 8.92],
            [53.02, 8.72]
        ]
    };
    var opened_info = new google.maps.InfoWindow();

    function init() {
        var mapOptions = {
            zoom:8,
            center:new google.maps.LatLng(53, 9.2),
            mapTypeId:google.maps.MapTypeId.TERRAIN
        };
        map = new google.maps.Map(document.getElementById('map_canvas'),
                mapOptions);
        for (var c in cities) {
            var coods = cities[c]
            var polyPath = [];
            for (j = 0; j < coods.length; j++) {
                polyPath.push(new google.maps.LatLng(coods[j][0], coods[j][1]));
            }
            var shape = new google.maps.Polygon({
                paths:polyPath,
                fillColor:"#00FF00",
                fillOpacity:0.6,
            });
            shape.infowindow = new google.maps.InfoWindow(
                    {
                        content:"<b>" + c + "</b>" + "</br>",
                    });
            shape.infowindow.name = c;
            shape.setMap(map);
            google.maps.event.addListener(shape, 'click', showInfo);
        }

    }
    ;

    function showInfo(event) {
        opened_info.close();
        if (opened_info.name != this.infowindow.name) {
            this.infowindow.setPosition(event.latLng);
            this.infowindow.open(map);
            opened_info = this.infowindow;
        }
    }

</script>

It Provide also the function to close the Infowindows by clicking on the same polygon again.

初与友歌 2024-09-08 13:47:12

你好,非常感谢 filip-fku!

感谢您的评论,我终于找到了如何做到这一点! :-)
因此,如果有人搜索“如何执行此操作”,这里是代码片段:

GEvent.addListener(zone_up_champagne, "click", function(overlay,latlng) {
    map.openInfoWindowHtml(overlay, '<strong>Your html things :</strong><br />etc...');
});

希望这可以有所帮助!

再次感谢菲利普! :)

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 :

GEvent.addListener(zone_up_champagne, "click", function(overlay,latlng) {
    map.openInfoWindowHtml(overlay, '<strong>Your html things :</strong><br />etc...');
});

hope this can helps !

thanks again filip! :)

自此以后,行同陌路 2024-09-08 13:47:12

不需要全局信息窗口。在事件处理程序工作时初始化信息窗口。
使用此原型检查该点是否包含在多边形数组中的多边形实例中。

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

饭团 2024-09-08 13:47:12

我将描述该解决方案,因为我有一段时间没有使用 API,并且很难上传大量代码 - 不习惯这里的代码编辑功能。有关详细信息,请参阅 API 参考。

那么,让我们开始吧:

  1. 您使用map.AddOverlay() 添加多边形,然后地图存储您的多边形。
  2. 声明一个事件处理程序来捕获对地图的点击。该事件处理程序将传递单击位置的 GLatLng 以及单击的覆盖层(在您的情况下是多边形)。您可以进行简单的类型测试来确定叠加层是否是多边形。
  3. 在事件处理程序中使用 map.openInfoWindowHtml(),传递作为位置提供的 GLatLng 以及要显示的 HTML 内容。

就这么简单!您将必须查找如何附加事件处理程序,因为我不记得具体细节。因此,您需要在 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:

  1. You are adding polygons using map.AddOverlay(), the map then stores your polygons.
  2. Declare an event handler that catches clicks to the map. That event handler will be passed a GLatLng of the location clicked, and the overlay that was clicked (in your case the polygon). You can do a simple type test to decide if the overlay is a polygon.
  3. In the event handler use map.openInfoWindowHtml(), passing the GLatLng supplied as the location, and the HTML content you want displayed.

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:

  • Adding event handlers to the map
  • Checking the type of an overlay

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!

葬﹪忆之殇 2024-09-08 13:47:12

在V3中,您仍然会使用信息窗口,但使用不同的语法:

function initialize() {

// SOME CODE
var mont_blanc_path_array = [ 
new google.maps.LatLng(46.21270329318585, 6.134903900311617),
new google.maps.LatLng(46.20538443787925, 6.136844716370282),
new google.maps.LatLng(46.20525043957647, 6.141375978638086)
];
zone_up_montblanc = new google.maps.Polygon({
    editable: false,    
    paths: mont_blanc_path_array,       
    fillColor: "#0000FF",
    fillOpacity: 0.5,
    strokeColor: "#FF0000",
    strokeOpacity: 1.0,     
    strokeWeight: 2
});
// Creating InfoWindow object
var infowindow = new google.maps.InfoWindow({
    content: ' ',
    suppressMapPan:true
});
eventPolygonClick = google.maps.event.addListener(zone_up_montblanc, 'click', function() {
    // MORE CODE OR FUNCTION CALLS
});
// MORE CODE

}

任何您想要更改文本并显示信息窗口的地方:

infowindow.setContent("CLICKED me");
infowindow.open(map, zone_up_montblanc);

我已经制作了多边形和事件监听器全局变量(通过在变量声明中抑制“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:

function initialize() {

// SOME CODE
var mont_blanc_path_array = [ 
new google.maps.LatLng(46.21270329318585, 6.134903900311617),
new google.maps.LatLng(46.20538443787925, 6.136844716370282),
new google.maps.LatLng(46.20525043957647, 6.141375978638086)
];
zone_up_montblanc = new google.maps.Polygon({
    editable: false,    
    paths: mont_blanc_path_array,       
    fillColor: "#0000FF",
    fillOpacity: 0.5,
    strokeColor: "#FF0000",
    strokeOpacity: 1.0,     
    strokeWeight: 2
});
// Creating InfoWindow object
var infowindow = new google.maps.InfoWindow({
    content: ' ',
    suppressMapPan:true
});
eventPolygonClick = google.maps.event.addListener(zone_up_montblanc, 'click', function() {
    // MORE CODE OR FUNCTION CALLS
});
// MORE CODE

}

anywhere you feel like changing the text and displaying the infowindow:

infowindow.setContent("CLICKED me");
infowindow.open(map, zone_up_montblanc);

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文