简体   繁体   中英

how to allow only a single marker

my map event listener adds a marker on click

 let coordsData = new H.geo.Point(coord.lat, coord.lng);
        let standardMarker = new H.map.Marker(coordsData);

        map.addObject(standardMarker);

how to make it if the user clicks again to replace this marker with new one? map.removeObjects() and map.removeObject() both gives and error

like map.removeObject(standardMarker) map.addObject(standardMarker);

To remove an object from the map, a call to the map object's method removeObjects is required.

Please find the sample code snippet that creates a series of H.map.Markers for each location found, and adds it to the map. However, adding the removeObjects() method removes the marker object on every search.

 function addLocationsToMap(locations) { map.removeObjects(map.getObjects()) // // debugger var group = new H.map.Group(), position, i; // Add a marker for each location found for (i = 0; i < locations.length; i += 1) { let location = locations[i]; marker = new H.map.Marker(location.position); marker.label = location.address.label; group.addObject(marker); } group.addEventListener('tap', function(evt) { map.setCenter(evt.target.getGeometry()); openBubble( evt.target.getGeometry(), evt.target.label); }, false); // Add the locations group to the map map.addObject(group); map.setCenter(group.getBoundingBox().getCenter()); }

You can refer to this complete example: https://jsfiddle.net/raj0665/1ck3v9oh/18/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM