2

I need update/refresh/repaint the layer on the map when i click on the button refresh. I remove the layer and add it again but it not working and points on the map are not displaying. Is there an other way to refresh data.

private void showMap(File rasterFile, final File shpFile) throws Exception{

    AbstractGridFormat format = GridFormatFinder.findFormat( rasterFile );        
    reader = format.getReader(rasterFile);

    final Style rasterStyle = createRGBStyle();

    // Connect to the shapefile
    FileDataStore dataStore = FileDataStoreFinder.getDataStore(shpFile);
    SimpleFeatureSource shapefileSource = dataStore.getFeatureSource();

    Style shpStyle = SLD.createSimpleStyle(dataStore.getFeatureSource().getSchema(), Color.RED);


    // Set up a MapContent with the two layers
    final MapContent map = new MapContent();
    map.setTitle("The Map");

    Layer rasterLayer = new GridReaderLayer(reader, rasterStyle);
    map.addLayer(rasterLayer);

    final Layer shpLayer = new FeatureLayer(shapefileSource, shpStyle);
    map.addLayer(shpLayer);


    // Create the mappan.
    final JMapPane mapPane = new JMapPane(map);


    jInternalFrame1.setLayout(new BorderLayout());
    jInternalFrame1.add(mapPane, BorderLayout.CENTER);

    JMenuBar menuBar = new JMenuBar();
    jInternalFrame1.setJMenuBar(menuBar);
    JMenu menu = new JMenu("Map");
    menuBar.add(menu);

    menu.add( new SafeAction("Refresh") {
        public void action(ActionEvent e) throws Throwable {
            map.removeLayer(shpLayer);                
            // Connect to the shapefile
            FileDataStore dataStore = FileDataStoreFinder.getDataStore(shpFile);
            SimpleFeatureSource shapefileSource = dataStore
            .getFeatureSource();

            Layer shpLayerNew = new FeatureLayer(shapefileSource, shpStyle);

            map.addLayer(shpLayerNew);                
        }
    });
}
  • why are you trying to force a refresh? Could you maybe use the MapLayerListEvent methods (layerChanged) to signal to the map to redraw. – Ian Turton Mar 17 '15 at 10:49
  • because i have a map displayed and a form to add a new layer, i want to refresh the map when i add a new layer from the from. can you give me an example. – Nadir Mar 19 '15 at 21:03
  • thanks @iant, can you give me an example with MapLayerListEvent method. – Nadir Mar 31 '15 at 14:59
  • Will you please help me too I have the same problem. Which is that when I add multiple layers on map at the time of addition for second layer on map and set bounds of MapViewport to that layer's bounds, bounds has been set but map does not refreshed. if i interacted with map by (Zoom change or by clicking it or Drag) it will change the bounds. Is there any solution to do this .? – Shubham Goyal Oct 20 '16 at 12:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.