Reputation: 15
I want to add several markers to a map and then have the user click on the map and display a different marker for the clicked location.
I don't seem to be able to remove the event listener
before displaying the new marker so that further map clicks are disabled.
I get Uncaught TypeError:
Cannot read property '__closure_events_fn_249633963' of undefined sample code I've tried.
function setUpClickListener(map) {
// Attach an event listener to map
map.addEventListener('tap', function(evt) {addMarker(evt);});
// map.addEventListener('tap', function addMarker(evt));
// map.addEventListener('tap', addMarker(function (evt)));
// map.addEventListener('tap', addMarker(evt));
}
function addMarker(myevt) {
// remove event listener so no further map clicks can be made
map.removeEventListener();
// add clicked marker
var coord = map.screenToGeo(myevt.currentPointer.viewportX, myevt.currentPointer.viewportY);
var sLat = coord.lat;
var sLng = coord.lng;
var svgMarkup = '<svg width="24" height="24" xmlns="http://www.w3.org/2000/svg">' +
'<rect stroke="white" fill="#ff0000" x="1" y="1" width="22" height="22" />' +
'<text x="12" y="18" font-size="12pt" font-family="Arial" font-weight="bold" ' +
'text-anchor="middle" fill="white">S</text></svg>';
//add marker
var myIcon = new H.map.Icon(svgMarkup),
coords = {lat:sLat, lng:sLng},
marker = new H.map.Marker(coords, {icon: myIcon});
// Add the marker to the map
map.addObject(marker);
}
Can anyone give me the correct syntax to remove the listener?
Upvotes: 1
Views: 766
Reputation: 17626
To remove an event listener please see the documentation
Given an event listener previously added by calling addEventListener(), you may eventually come to a point at which you need to remove it. Obviously, you need to specify the same type and listener parameters to removeEventListener() (...)
function myTapEvent(evt) {
addMarker(evt);
}
function setUpClickListener(map) {
// Attach an event listener to map
map.addEventListener('tap', myTapEvent);
}
function addMarker(myevt) {
map.removeEventListener('tap', myTapEvent);
///more code...
}
Upvotes: 1