我正在使用 Here maps api 设置地图,但在删除地图侦听器时遇到问题
I'm setting up a map with Here maps api and I am having trouble removing a map listener
我想向地图添加多个标记,然后让用户单击地图并为单击的位置显示不同的标记。
我似乎无法在显示新标记之前删除 event listener
以禁用进一步的地图点击。
我收到未捕获的类型错误:
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);
}
任何人都可以给我正确的语法来删除侦听器吗?
解释:
要删除事件侦听器,请参阅 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...
}
我想向地图添加多个标记,然后让用户单击地图并为单击的位置显示不同的标记。
我似乎无法在显示新标记之前删除 event listener
以禁用进一步的地图点击。
我收到未捕获的类型错误:
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);
}
任何人都可以给我正确的语法来删除侦听器吗?
解释:
要删除事件侦听器,请参阅 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...
}