如何用jQuery隐藏Google Maps Api标记 [英] How to hide Google Maps Api Markers with jQuery
问题描述
你好,这可能是一个非常愚蠢的问题,但我试图让
点击时标记消失。标记正确地位于地图上,但是当我点击它时,
不会做任何事情。我想知道为什么它不工作。谢谢!
Hello this might be really silly question but I am trying to make markers disappear when they are clicked. The marker is located properly on the map but when I click it, it doesn't do anything. I was wondering why its not working. Thank you!
<script type="text/javascript" src="jQuery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
var myOptions = {
center: new google.maps.LatLng(40.1, -88.2),
zoom: 13,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
var myLatlng = new google.maps.LatLng(40.1, -88.2);
var temp_marker = new google.maps.Marker({
position: myLatlng,
map: map,
title:"Hello World!"
});
console.log($(temp_marker));
console.log(temp_marker);
//temp_marker.click(function(){$(this).hide();});
$(temp_marker).click(function(){console.log("click is working"); $(this).hide();});
});
</script>
</head>
<body>
<div id="map_canvas" style="width:100%; height:100%"></div>
</body>
推荐答案
temp_marker
是一个Javascript对象,不是DOM元素。要将侦听器附加到标记上(API将处理要附加到的DOM元素的具体情况以及如何),您应该使用Google Maps API自己的事件系统,如:
temp_marker
is a Javascript object, not a DOM element. To attach a listener to the marker (the API will handle the specifics of which DOM element to attach to and how), you should use the Google Maps API's own events system like:
google.maps.event.addListener(marker, 'click', function() {
marker.setVisible(false); // maps API hide call
});
他们的文档: Google Maps JavaScript API v3 - 活动
这篇关于如何用jQuery隐藏Google Maps Api标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!