0

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);

Sam Ban
  • 59
  • 8

1 Answers1

0

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/