[英]How to identify a google map marker on click?
I'm creating a Google Map from the developer API v3. 我正在从开发人员API v3创建Google Map。 It's populated with markers created dynamically from ColdFusion querying an MsSQL database.
它填充了从ColdFusion查询MsSQL数据库动态创建的标记。
<cfloop query="One">
<script>locations[<cfoutput>#One.userID#</cfoutput>
] = new google.maps.LatLng(<cfoutput>#One.latLng#</cfoutput>);
</script>
</cfloop>
I need a way to recognise the marker when its clicked so I can display address details in a box below the map and also higlight markers when a button is clicked lower on the page. 我需要一种方法来识别单击时标记,这样我就可以在地图下方的框中显示地址详细信息,并在页面下方单击按钮时显示高光标记。
In general, you would typically assign your own custom properties to the Marker. 通常,您通常会将自己的自定义属性分配给标记。 Something like:
就像是:
function markerClicked(e) {
console.log('Marker ' + marker.myData + ' has been clicked');
}
var marker = new google.maps.Marker(...);
marker.myData = 1; //this could be arbitrary data or even another object
google.maps.event.addListener(marker, 'click', markerClicked);
Adding custom data to any Google Maps API object has risks though. 但是,将自定义数据添加到任何Google Maps API对象都存在风险。 Google's code is obfuscated and the internal (non-documented) properties can and do change.
谷歌的代码被混淆,内部(未记录)属性可以并且确实发生变化。 Make sure your property is named in such a way that it won't conflict with any existing property or future property.
确保您的财产的命名方式不会与任何现有财产或未来财产发生冲突。 Tip: Choose a property name longer than 3 letters.
提示:选择长度超过3个字母的属性名称。
If you are going to minify/compile/compress your maps code, then there are additional considerations . 如果您要缩小/编译/压缩地图代码,则还有其他注意事项 。
What about : 关于什么 :
google.maps.event.addListener(marker, "click", function (e) {
var clicked = this;
//...
});
This is pretty thoroughly documented/explained in the documentation. 这在文档中已经完全记录/解释。
https://developers.google.com/maps/documentation/javascript/overlays#InfoWindows https://developers.google.com/maps/documentation/javascript/overlays#InfoWindows
When you create markers, add dom listeners to the markers like this 创建标记时,将dom侦听器添加到标记中,如下所示
google.maps.event.addListener(marker, 'click', function() {
infowindow.open(map,marker);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.