Javascript 从谷歌地图中删除固定标记
如何删除添加到包含GoogleMaps的div元素中的固定标记。下面的代码行使用类“centerMarker”添加它,Javascript 从谷歌地图中删除固定标记,javascript,jquery,css,google-maps,google-maps-api-3,Javascript,Jquery,Css,Google Maps,Google Maps Api 3,如何删除添加到包含GoogleMaps的div元素中的固定标记。下面的代码行使用类“centerMarker”添加它,$(“”).addClass('centerMarker').appendTo(map.getDiv()) 但是如何删除它(理想情况下使用Javascript) 下面是添加固定标记(从)的。下面还显示了JS Fiddle示例中的代码 HTML Javascript function initialize() { var mapOptions = {
$(“”).addClass('centerMarker').appendTo(map.getDiv())代码>
但是如何删除它(理想情况下使用Javascript)
下面是添加固定标记(从)的。下面还显示了JS Fiddle示例中的代码
HTML
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);
函数初始化(){
变量映射选项={
缩放:14,
中心:新google.maps.LatLng(52.5498783,13.4252090999961),
mapTypeId:google.maps.mapTypeId.ROADMAP
};
map=new google.maps.map(document.getElementById('map_canvas'),
地图选项);
$('').addClass('centerMarker').appendTo(map.getDiv())
//再舔一下
。单击(函数(){
var,该值=$(此值);
如果(!that.data('win')){
data('win',新的google.maps.InfoWindow({content:'this is the center'));
数据('win').bindTo('position',map,'center');
}
即.data('win')。open(map);
});
}
google.maps.event.addDomListener(窗口“加载”,初始化);
您可以使用删除它,例如,让我们说我们想在单击时删除它(似乎更合适,因为我们至少想在标记上显示标记,如果不想,就从js代码中删除它)
使用谷歌地图API
根据您的示例,只需将此代码添加到initialize
函数中
google.maps.event.addDomListener(map, 'click', removeMarker());
这是你的电话号码
如果要提醒用户标记消失的原因,请改用函数,如下所示
function removeMarker(ele){
ele.remove();
alert('Ok the marker is GONE!')
}
google.maps.event.addDomListener(map, 'click', removeMarker(this));
function removeMarker(elem){
console.log('removed')
elem = document.getElementById("map_canvas");
elem.childNodes[0].remove()
}
setTimeout(function(){ removeMarker(this) }, 3000);
这是另一个函数
纯Javascript
如果您想使用纯Javascript代码在initialize函数之外删除它,请在注释上使用类似@Adam point的方法,类似于下面的方法
function removeMarker(ele){
ele.remove();
alert('Ok the marker is GONE!')
}
google.maps.event.addDomListener(map, 'click', removeMarker(this));
function removeMarker(elem){
console.log('removed')
elem = document.getElementById("map_canvas");
elem.childNodes[0].remove()
}
setTimeout(function(){ removeMarker(this) }, 3000);
这是纯JSjQuery。删除
只是好奇,但是为什么你想要一个固定的标记(这样它就可以独立于地图移动)?@user44776你找到解决方案了吗?如果不给我你想做的更多信息,那么我可以更新我的答案