Skip to content

Commit

Permalink
New examples added
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaly-eremenko committed Feb 24, 2017
1 parent 7b2dd8f commit 8dc53d0
Show file tree
Hide file tree
Showing 6 changed files with 346 additions and 0 deletions.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/com/teamdev/jxmaps/demo/res/jxmaps256x256.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
77 changes: 77 additions & 0 deletions src/com/teamdev/jxmaps/examples/SaveImageExample.java
@@ -0,0 +1,77 @@
package com.teamdev.jxmaps.examples;

import com.teamdev.jxmaps.*;
import com.teamdev.jxmaps.swing.MapView;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.RenderedImage;
import java.io.File;
import java.io.IOException;

/**
* This example demonstrates how to save displayed map to PNG file.
*
* @author Vitaly Eremenko
*/
public class SaveImageExample extends MapView {
public SaveImageExample() {

// Creation of MapViewer with specifying LIGHTWEIGHT mode. getImage method of MapView class working only in the this mode.
super(new MapViewOptions(MapComponentType.LIGHTWEIGHT));

// Setting of a ready handler to 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.
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 = 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 of idle event listener
map.addEventListener("idle", new MapEvent() {
@Override
public void onEvent() {
// Getting of current map image
Image image = getImage();
// Saving of 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();
}
}
});
}
}
});
}

public static void main(String[] args) {
SaveImageExample sample = new SaveImageExample();

JFrame frame = new JFrame("Save Image Example");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.add(sample, BorderLayout.CENTER);
frame.setSize(700, 500);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
84 changes: 84 additions & 0 deletions src/com/teamdev/jxmaps/examples/StreetViewLayoutExample.java
@@ -0,0 +1,84 @@
/*
* Copyright (c) 2000-2017 TeamDev Ltd. All rights reserved.
* Use is subject to Apache 2.0 license terms.
*/
package com.teamdev.jxmaps.examples;

import com.teamdev.jxmaps.*;
import com.teamdev.jxmaps.swing.MapView;

import javax.swing.*;
import java.awt.*;

/**
* This example demonstrates how to customize street view panorama layout.
*
* @author Vitaly Eremenko
*/
public class StreetViewLayoutExample extends MapView {

public static void main(String[] args) {

final MapViewOptions option = new MapViewOptions();
option.streetViewLayout().setSize(70);
option.streetViewLayout().setPosition(StreetViewLayout.Position.BOTTOM);
final MapView view = new MapView(option);

// Setting of a ready handler to 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.
view.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
Map map = view.getMap();
// Creating a map options object
MapOptions mapOptions = 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
mapOptions.setMapTypeControlOptions(controlOptions);
// Setting map options
map.setOptions(mapOptions);
// Setting the map center
map.setCenter(new LatLng(51.500871, -0.1222632));
// Setting initial zoom value
map.setZoom(13.0);
// Creating a street view panorama options object
StreetViewPanoramaOptions options = new StreetViewPanoramaOptions();
// Creating a street view address control options object
StreetViewAddressControlOptions svControlOptions = new StreetViewAddressControlOptions();
// Changing position of the address control on the panorama
svControlOptions.setPosition(ControlPosition.TOP_RIGHT);
// Setting address control options
options.setAddressControlOptions(svControlOptions);
// Setting street view panorama options
view.getPanorama().setOptions(options);
// Setting initial position of the street view
view.getPanorama().setPosition(map.getCenter());
// Creating point of view object
StreetViewPov pov = new StreetViewPov();
// Setting heading for the point of view
pov.setHeading(270);
// Setting pitch for the point of view
pov.setPitch(20);
// Applying the point of view to the panorama object
view.getPanorama().setPov(pov);
}
}
});

JFrame frame = new JFrame("Street View Layout");

frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.add(view, BorderLayout.CENTER);
frame.setSize(700, 500);
frame.setLocationRelativeTo(null);
frame.setVisible(true);


}
}
185 changes: 185 additions & 0 deletions src/com/teamdev/jxmaps/examples/TrafficLayerExample.java
@@ -0,0 +1,185 @@
/*
* Copyright (c) 2000-2017 TeamDev Ltd. All rights reserved.
* Use is subject to Apache 2.0 license terms.
*/
package com.teamdev.jxmaps.examples;

import com.teamdev.jxmaps.*;
import com.teamdev.jxmaps.swing.MapView;

import javax.swing.*;
import javax.swing.plaf.basic.BasicButtonUI;
import javax.swing.plaf.basic.BasicTextFieldUI;
import java.awt.*;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
* This example demonstrates how to display traffic layer on the map.
*
* @author Vitaly Eremenko
* @author Sergei Piletsky
*/
public class TrafficLayerExample extends MapView {

private static final String INITIAL_LOCATION = "New York";

private OptionsWindow optionsWindow;

public TrafficLayerExample() {
// Setting of a ready handler to 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.
setOnMapReadyHandler(new MapReadyHandler() {
@Override
public void onMapReady(MapStatus status) {
// Getting the associated map object
final Map map = getMap();
// Setting initial zoom value
map.setZoom(13.0);
// 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);

// Enabling of traffic layer on the map
new TrafficLayer(map);

performGeocode(INITIAL_LOCATION);
}
});
}

@Override
public void addNotify() {
super.addNotify();

optionsWindow = new OptionsWindow(this, new Dimension(350, 40)) {
@Override
public void initContent(JWindow contentWindow) {
JPanel content = new JPanel(new GridBagLayout());
content.setBackground(Color.white);

Font robotoPlain13 = new Font("Roboto", 0, 13);
final JTextField searchField = new JTextField();
searchField.setText(INITIAL_LOCATION);
searchField.setToolTipText("Enter address or coordinates...");
searchField.setBorder(BorderFactory.createEmptyBorder());
searchField.setFont(robotoPlain13);
searchField.setForeground(new Color(0x21, 0x21, 0x21));
searchField.setUI(new SearchFieldUI(searchField));

final JButton searchButton = new JButton();
searchButton.setIcon(new ImageIcon(MapOptionsExample.class.getResource("res/search.png")));
searchButton.setRolloverIcon(new ImageIcon(MapOptionsExample.class.getResource("res/search_hover.png")));
searchButton.setBorder(BorderFactory.createEmptyBorder());
searchButton.setUI(new BasicButtonUI());
searchButton.setOpaque(false);
ActionListener searchActionListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
performGeocode(searchField.getText());
}
};
searchButton.addActionListener(searchActionListener);
searchField.addActionListener(searchActionListener);

content.add(searchField, new GridBagConstraints(0, 0, 1, 1, 1.0, 0.0,
GridBagConstraints.NORTHWEST, GridBagConstraints.HORIZONTAL, new Insets(11, 11, 11, 0), 0, 0));
content.add(searchButton, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0,
GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets(11, 0, 11, 11), 0, 0));

contentWindow.getContentPane().add(content);
}

@Override
protected void updatePosition() {
if (parentFrame.isVisible()) {
Point newLocation = parentFrame.getContentPane().getLocationOnScreen();
newLocation.translate(56, 11);
contentWindow.setLocation(newLocation);
contentWindow.setSize(340, 40);
}
}
};
}

class SearchFieldUI extends BasicTextFieldUI {
private final JTextField textField;

public SearchFieldUI(JTextField textField) {
this.textField = textField;
}

@Override
protected void paintBackground(Graphics g) {
super.paintBackground(g);
String toolTipText = textField.getToolTipText();
String text = textField.getText();
if (toolTipText != null && text.isEmpty()) {
paintPlaceholderText(g, textField);
}
}

protected void paintPlaceholderText(Graphics g, JComponent c) {
g.setColor(new Color(0x75, 0x75, 0x75));
g.setFont(c.getFont());
String text = textField.getToolTipText();
if (g instanceof Graphics2D) {
Graphics2D graphics2D = (Graphics2D) g;
graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
}
g.drawString(text, 0, 14);
}
}

@Override
public void removeNotify() {
super.removeNotify();
optionsWindow.dispose();
}

private void performGeocode(String text) {
// Getting the associated map object
final Map map = getMap();
// Creating a geocode request
GeocoderRequest request = new GeocoderRequest();
// Setting address to the geocode request
request.setAddress(text);

// Geocoding position by the entered address
getServices().getGeocoder().geocode(request, new GeocoderCallback(map) {
@Override
public void onComplete(GeocoderResult[] results, GeocoderStatus status) {
// Checking operation status
if ((status == GeocoderStatus.OK) && (results.length > 0)) {
// Getting the first result
GeocoderResult result = results[0];
// Getting a location of the result
LatLng location = result.getGeometry().getLocation();
// Setting the map center to result location
map.setCenter(location);
}
}
});
}


public static void main(String[] args) {
final TrafficLayerExample mapView = new TrafficLayerExample();

JFrame frame = new JFrame("Traffic layer");

frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.add(mapView, BorderLayout.CENTER);
frame.setSize(700, 500);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}

0 comments on commit 8dc53d0

Please sign in to comment.