在谷歌地图KML位置标记上触发点击事件

Trigger click event on a Google Maps KML placemark

本文关键字:事件 谷歌地图 KML 位置      更新时间:2023-09-26

我有一个映射,它正在动态外部KML中加载,其中的占位符定义如下:

<Placemark id="MapZoneID_23443">
    <name>Name Here</name>
    <description>Text Here</description>
    <styleUrl>#ff8080ff</styleUrl>
    <Polygon>
        <outerBoundaryIs>
            <LinearRing>
                <coordinates>
                    ....
                </coordinates>
            </LinearRing>
        </outerBoundaryIs>
    </Polygon>
</Placemark>

我想做的是有一个链接/下拉列表/任何可以点击或选择的东西,基本上触发点击$('#MapZoneID_23443')。。。但我不知道如何触发点击,也不知道这是否可能。映射可能相当复杂,所以我不想使用JS gmaps标记来预加载所有内容。谢谢

目前还不可能。

在错误跟踪器上标记问题,以便投票支持并关注其进展:https://code.google.com/p/gmaps-api-issues/issues/detail?id=3006

我找到了一个解决方法。

将其添加到<style>部分中的位置标记中

<BalloonStyle><text>TEXT</text></BalloonStyle>

您将能够在点击.js回调后访问此值作为

event.featureData.info_window_html

因此,在您的KML文件中

<Placemark id="MapZoneID_23443">
   <BalloonStyle><text>TEXT</text></BalloonStyle>
   ...
</Placemark>

在javascript 中

google.maps.event.addListener(kmlLayer, 'click', function(event) {
  var content = event.featureData.info_window_html;
  console.log(content);
});