title: Place marker on map - GeoTools 25-SNAPSHOT - Java 11
body: <p>I am using geotools 25-SNAPSHOT in my application. I have displayed a map in my application (raster).
  Now I want to place a marker (image or simple colored dot) on a map at a specific latitude, longitude location without refresh.</p>
  <p>In the following code the marker is placed at mouse click but on <code>addLayer()</code> the map is reloaded which is inappropriate.</p>
  <pre><code>import java.awt.Color;
  import java.io.File;
  
  import javax.swing.Icon;
  import javax.swing.ImageIcon;
  import javax.swing.JButton;
  import javax.swing.JToolBar;
  
  import org.geotools.coverage.grid.GridCoverage2D;
  import org.geotools.coverage.grid.io.AbstractGridFormat;
  import org.geotools.coverage.grid.io.GridCoverage2DReader;
  import org.geotools.coverage.grid.io.GridFormatFinder;
  import org.geotools.data.DataUtilities;
  import org.geotools.feature.DefaultFeatureCollection;
  import org.geotools.feature.SchemaException;
  import org.geotools.feature.simple.SimpleFeatureBuilder;
  import org.geotools.geometry.DirectPosition2D;
  import org.geotools.geometry.jts.JTSFactoryFinder;
  import org.geotools.map.FeatureLayer;
  import org.geotools.map.GridCoverageLayer;
  import org.geotools.map.Layer;
  import org.geotools.map.MapContent;
  import org.geotools.styling.SLD;
  import org.geotools.styling.Style;
  import org.geotools.styling.StyleBuilder;
  import org.geotools.swing.JMapFrame;
  import org.geotools.swing.event.MapMouseEvent;
  import org.geotools.swing.tool.CursorTool;
  import org.locationtech.jts.geom.Coordinate;
  import org.locationtech.jts.geom.GeometryFactory;
  import org.locationtech.jts.geom.Point;
  import org.opengis.feature.simple.SimpleFeature;
  import org.opengis.feature.simple.SimpleFeatureType;
  
  public class Test {
      /**
       * GeoTools Quickstart demo application. Prompts the user for a shapefile and
       * displays its contents on the screen in a map frame
       */
      public static void main(String[] args) throws Exception {
          try {
              JMapFrame mapFrame;
  
              File file = new File(&quot;mapfile.tif&quot;);
  
              GridFormatFinder.scanForPlugins();
              AbstractGridFormat format = GridFormatFinder.findFormat(file);
              GridCoverage2DReader reader = format.getReader(file);
  
              GridCoverage2D coverage = (GridCoverage2D) reader.read(null);
  
              // Create a map content and add our shapefile to it
              MapContent map = new MapContent();
              map.setTitle(&quot;Marker test&quot;);
  
              StyleBuilder sb = new StyleBuilder();
              Style baseStyle = sb.createStyle(sb.createRasterSymbolizer());
              map.addLayer(new GridCoverageLayer(coverage, baseStyle));
  
              mapFrame = new JMapFrame(map);
              mapFrame.enableToolBar(true);
              mapFrame.enableStatusBar(true);
              
              //ToolBar
              JToolBar toolBar = mapFrame.getToolBar();
              Icon icon = new ImageIcon(&quot;icon.png&quot;);
              JButton btn = new JButton(icon);
              btn.setToolTipText(&quot;Add mining manually&quot;);
              toolBar.addSeparator();
              toolBar.add(btn);
              
              //Action on mouse click for adding marker
              btn.addActionListener(e -&gt; mapFrame.getMapPane().setCursorTool(new CursorTool() {
                  @Override
                  public void onMouseClicked(MapMouseEvent ev) {
                      try {
                          DirectPosition2D p = ev.getWorldPos();
                          System.out.println(p.getX() + &quot; -- &quot; + p.getY());                       
                          Layer layer = getLayerPointByCoord(new Coordinate(p.getX(), p.getY()));
                          map.addLayer(layer);
                      } catch (Exception e) {
                          e.printStackTrace();
                      }
                  }
              }));
              
              mapFrame.setTitle(&quot;Aeron GIS Utility&quot;);
              mapFrame.setSize(2030,830);
              mapFrame.setVisible(true);
          } catch (Exception e) {
              e.printStackTrace();
          }
      }
      
      static Layer getLayerPointByCoord(Coordinate coords) throws SchemaException {
          GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory();
          Point point = geometryFactory.createPoint(coords);
          SimpleFeatureType TYPE = DataUtilities.createType(&quot;test&quot;, &quot;point&quot;, &quot;the_geom:Point&quot;);
          SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder((SimpleFeatureType) TYPE);
          featureBuilder.add(point);
          SimpleFeature feature = featureBuilder.buildFeature(&quot;LineString_Sample&quot;);
  
          DefaultFeatureCollection lineCollection = new DefaultFeatureCollection();
          lineCollection.add(feature);
  
          Style style = SLD.createPointStyle(&quot;Circle&quot;, Color.BLACK, Color.RED, 1f, 15f);
          return new FeatureLayer(lineCollection, style);
      }
  
  }
  </code></pre>