【发布时间】:2015-06-05 12:42:30
【问题描述】:
如何删除固定标记,该标记已添加到包含谷歌地图的 div 元素中。下面的代码行使用类'centerMarker'添加它,$('<div/>').addClass('centerMarker').appendTo(map.getDiv());
但是如何删除它(最好使用 Javascript)?
这是添加固定标记的full JS Fiddle example(来自this stack overflow question)。 JS Fiddle 示例中的代码也如下所示。
HTML
<div id="map_canvas"></div>
CSS
body,html,#map_canvas{height:100%;margin:0;}
#map_canvas .centerMarker{
position:absolute;
/*url of the marker*/
background:url(http://maps.gstatic.com/mapfiles/markers2/marker.png) no-repeat;
/*center the marker*/
top:50%;left:50%;
z-index:1;
/*fix offset when needed*/
margin-left:-10px;
margin-top:-34px;
/*size of the image*/
height:34px;
width:20px;
cursor:pointer;
}
Javascript
function initialize() {
var mapOptions = {
zoom: 14,
center: new google.maps.LatLng(52.5498783, 13.425209099999961),
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById('map_canvas'),
mapOptions);
$('<div/>').addClass('centerMarker').appendTo(map.getDiv())
//do something onclick
.click(function(){
var that=$(this);
if(!that.data('win')){
that.data('win',new google.maps.InfoWindow({content:'this is the center'}));
that.data('win').bindTo('position',map,'center');
}
that.data('win').open(map);
});
}
google.maps.event.addDomListener(window, 'load', initialize);
【问题讨论】:
-
jQuery.removeapi.jquery.com/remove 只是好奇,但你为什么想要一个固定的标记(以便它独立于地图移动)? -
@user44776 你找到解决方案了吗,如果没有给我更多关于你想要做什么的信息,所以我可以更新我的答案
标签: javascript jquery css google-maps google-maps-api-3