首页 > 解决方案 > 在地图上放置标记 - GeoTools 25-SNAPSHOT - Java 11

问题描述

我在我的应用程序中使用 geotools 25-SNAPSHOT。我在我的应用程序(光栅)中显示了一张地图。现在我想在地图上的特定纬度、经度位置放置一个标记(图像或简单的彩色点)而不刷新。

在下面的代码中,标记是在鼠标单击时放置的,但在addLayer()地图上会重新加载,这是不合适的。

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("mapfile.tif");

            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("Marker test");

            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("icon.png");
            JButton btn = new JButton(icon);
            btn.setToolTipText("Add mining manually");
            toolBar.addSeparator();
            toolBar.add(btn);
            
            //Action on mouse click for adding marker
            btn.addActionListener(e -> mapFrame.getMapPane().setCursorTool(new CursorTool() {
                @Override
                public void onMouseClicked(MapMouseEvent ev) {
                    try {
                        DirectPosition2D p = ev.getWorldPos();
                        System.out.println(p.getX() + " -- " + p.getY());                       
                        Layer layer = getLayerPointByCoord(new Coordinate(p.getX(), p.getY()));
                        map.addLayer(layer);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }));
            
            mapFrame.setTitle("Aeron GIS Utility");
            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("test", "point", "the_geom:Point");
        SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder((SimpleFeatureType) TYPE);
        featureBuilder.add(point);
        SimpleFeature feature = featureBuilder.buildFeature("LineString_Sample");

        DefaultFeatureCollection lineCollection = new DefaultFeatureCollection();
        lineCollection.add(feature);

        Style style = SLD.createPointStyle("Circle", Color.BLACK, Color.RED, 1f, 15f);
        return new FeatureLayer(lineCollection, style);
    }

}

标签: mapsjava-11geotools

解决方案


推荐阅读