javascript - Remove Fixed Marker From Google Maps -


how can remove fixed marker, added div element containing google maps. below line of code adds using class 'centermarker', $('<div/>').addclass('centermarker').appendto(map.getdiv()); how remove (ideally using javascript) ?

here full js fiddle example adds fixed marker (from this stack overflow question). code js fiddle example shown below.

html

<div id="map_canvas"></div> 

css

body,html,#map_canvas{height:100%;margin:0;} #map_canvas .centermarker{   position:absolute;   /*url of marker*/   background:url(http://maps.gstatic.com/mapfiles/markers2/marker.png) no-repeat;   /*center marker*/   top:50%;left:50%;   z-index:1;   /*fix offset when needed*/   margin-left:-10px;   margin-top:-34px;   /*size of 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 onclick             .click(function(){                var that=$(this);                if(!that.data('win')){                 that.data('win',new google.maps.infowindow({content:'this center'}));                 that.data('win').bindto('position',map,'center');                }                that.data('win').open(map);             });       }        google.maps.event.adddomlistener(window, 'load', initialize); 

you can remove using googlemaps domevents, example lets want remove on click (seems more appropriate, since atleast want show marker on mark, if not, remove js code).

using google maps api

bassed on example, add code, initialize function

google.maps.event.adddomlistener(map, 'click', removemarker()); 

here jsfiddle

if want alert user why marker gone, use function instead, this.

function removemarker(ele){            ele.remove();             alert('ok marker gone!')         }  google.maps.event.adddomlistener(map, 'click', removemarker(this)); 

here other jsfiddle function.

pure javascript

if want remove outside initialize function pure javascript code, use remove() method @adam point on comment, this.

function removemarker(elem){   console.log('removed')       elem = document.getelementbyid("map_canvas");       elem.childnodes[0].remove()  } settimeout(function(){ removemarker(this) }, 3000); 

here pure js jsfiddle


Comments

Popular posts from this blog

tcpdump - How to check if server received packet (acknowledged) -