Documentation‎ > ‎

Taking Screenshots

JxMaps library allows you to take a screenshot of the currently displayed map and save it as an image file.

To take a screenshot of the specified web page you need to follow the steps below:
  1. Create an instance of the Map object.
  2. Set up the map properties and wait until the the map is loaded.
  3. Get an image of the loaded map.
  4. Save the image to the file.
Important: Taking screenshots is possible only in LIGHTWEIGHT mode due to limitations of the Chromium engine.

The following examples demonstrate how to perform this action:

Swing:

// Creation of the MapViewer specifying LIGHTWEIGHT mode. getImage method of the MapView class works only in this mode.
final MapViewOptions options = new MapViewOptions();
options.setComponentType(MapComponentType.LIGHTWEIGHT);
options.setApiKey("<your_google_maps_api_key>");
MapView mapView = new MapView(options
);
// Setting of a ready handler to the MapView object. onMapReady will be called when map initialization is done and
// the map object is ready to use. Current implementation of onMapReady customizes the map object.
mapView.setOnMapReadyHandler(new MapReadyHandler() {
@Override
public void onMapReady(MapStatus status) {
// Check if the map is loaded correctly
if (status == MapStatus.MAP_STATUS_OK) {
// Getting the associated map object
final Map map = mapView.getMap();
// Creating a map options object
MapOptions options = new MapOptions();
// Creating a map type control options object
MapTypeControlOptions controlOptions = new MapTypeControlOptions();
// Changing position of the map type control
controlOptions.setPosition(ControlPosition.TOP_RIGHT);
// Setting map type control options
options.setMapTypeControlOptions(controlOptions);
// Setting map options
map.setOptions(options);
// Setting initial zoom value
map.setZoom(2.0);
// Setting the map center
map.setCenter(new LatLng(35.91466, 10.312499));

// Adding the idle event listener
map.addEventListener("idle", new MapEvent() {
@Override
public void onEvent() {
// Getting the current map image
Image image = mapView.getImage();
// Saving the image of the displayed map into a PNG file.
try {
ImageIO.
write((RenderedImage) image, "PNG", new File("map-image.png"));
}
catch (IOException e) {
e.printStackTrace();
}
}
});
}
}
});

JavaFX:

public class JavaFXExample extends Application {
@Override
public void init() throws Exception {
// Initializing of the JavaFX engine
MapView.InitJavaFX();
}

@Override
public void start(final Stage primaryStage) {
// Creation of a JavaFX map view
final MapView mapView = new MapView(new MapViewOptions(MapComponentType.LIGHTWEIGHT));

// Setting of a ready handler to a MapView object. onMapReady will be called when map initialization is done and
// the map object is ready to use. Current implementation of onMapReady customizes the map object.
mapView.setOnMapReadyHandler(new MapReadyHandler() {
@Override
public void onMapReady(MapStatus status) {
// Check if the map is loaded correctly
if (status == MapStatus.MAP_STATUS_OK) {
// Getting the associated map object
final Map map = mapView.getMap();
// Creating a map options object
MapOptions options = new MapOptions();
// Creating a map type control options object
MapTypeControlOptions controlOptions = new MapTypeControlOptions();
// Changing the position of the map type control
controlOptions.setPosition(ControlPosition.TOP_RIGHT);
// Setting the map type control options
options.setMapTypeControlOptions(controlOptions);
// Setting the map options
map.setOptions(options);
// Setting the map center
map.setCenter(new LatLng(35.91466, 10.312499));
// Setting initial zoom value
map.setZoom(2.0);

// Adding the idle event listener
map.addEventListener("idle", new MapEvent() {
@Override
public void onEvent() {
// Getting the current map image
Image image = mapView.getImage();
// Saving image of the displayed map into a PNG file.
try {
ImageIO.write(SwingFXUtils.fromFXImage(image, null), "PNG", new File("map-fx-image.png"));
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
}
});

Scene scene = new Scene(new BorderPane(mapView), 700, 500);
primaryStage.setScene(scene);
primaryStage.show();
}

public static void main(String[] args) {
launch(args);
}
}