2010-04-26 18:24:27 +02:00
|
|
|
package com.osmand;
|
2010-04-26 17:20:50 +02:00
|
|
|
|
|
|
|
import java.awt.BorderLayout;
|
|
|
|
import java.awt.Color;
|
|
|
|
import java.awt.Container;
|
|
|
|
import java.awt.Graphics;
|
2010-04-30 00:47:07 +02:00
|
|
|
import java.awt.Image;
|
2010-04-26 17:20:50 +02:00
|
|
|
import java.awt.Point;
|
|
|
|
import java.awt.event.ComponentAdapter;
|
|
|
|
import java.awt.event.ComponentEvent;
|
|
|
|
import java.awt.event.KeyEvent;
|
|
|
|
import java.awt.event.MouseAdapter;
|
|
|
|
import java.awt.event.MouseEvent;
|
|
|
|
import java.awt.image.BufferedImage;
|
2010-04-30 00:47:07 +02:00
|
|
|
import java.awt.image.RenderedImage;
|
2010-04-26 17:20:50 +02:00
|
|
|
import java.io.File;
|
|
|
|
import java.io.IOException;
|
2010-04-30 00:47:07 +02:00
|
|
|
import java.net.URL;
|
|
|
|
import java.net.URLConnection;
|
|
|
|
import java.net.UnknownHostException;
|
|
|
|
import java.text.MessageFormat;
|
2010-04-26 17:20:50 +02:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
2010-04-30 00:47:07 +02:00
|
|
|
import java.util.Stack;
|
2010-04-26 17:20:50 +02:00
|
|
|
|
2010-04-30 00:47:07 +02:00
|
|
|
import javax.imageio.IIOException;
|
2010-04-26 17:20:50 +02:00
|
|
|
import javax.imageio.ImageIO;
|
|
|
|
import javax.swing.JFrame;
|
|
|
|
import javax.swing.JPanel;
|
|
|
|
import javax.swing.UIManager;
|
|
|
|
|
2010-04-30 00:47:07 +02:00
|
|
|
import org.apache.commons.logging.Log;
|
|
|
|
import org.apache.commons.logging.LogFactory;
|
|
|
|
|
2010-04-26 18:24:27 +02:00
|
|
|
import com.osmand.DataExtraction.ExitListener;
|
2010-04-27 14:37:49 +02:00
|
|
|
import com.osmand.data.DataTileManager;
|
|
|
|
import com.osmand.osm.LatLon;
|
2010-04-27 23:42:19 +02:00
|
|
|
import com.osmand.osm.MapUtils;
|
2010-04-26 17:20:50 +02:00
|
|
|
|
|
|
|
public class MapPanel extends JPanel {
|
|
|
|
|
|
|
|
private static final long serialVersionUID = 1L;
|
2010-04-30 00:47:07 +02:00
|
|
|
|
|
|
|
private static final Log log = LogFactory.getLog(MapPanel.class);
|
2010-04-28 17:06:07 +02:00
|
|
|
|
2010-04-26 17:20:50 +02:00
|
|
|
|
|
|
|
public static void main(String[] args) throws IOException {
|
|
|
|
JFrame frame = new JFrame("Tree of choose");
|
|
|
|
try {
|
|
|
|
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
|
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
frame.addWindowListener(new ExitListener());
|
|
|
|
Container content = frame.getContentPane();
|
|
|
|
|
2010-04-27 23:42:19 +02:00
|
|
|
MapPanel panel = new MapPanel(new File(DefaultLauncherConstants.pathToDirWithTiles));
|
2010-04-26 17:20:50 +02:00
|
|
|
|
|
|
|
content.add(panel, BorderLayout.CENTER);
|
|
|
|
|
|
|
|
frame.setSize(512, 512);
|
|
|
|
frame.setVisible(true);
|
|
|
|
|
|
|
|
}
|
2010-04-27 14:37:49 +02:00
|
|
|
|
|
|
|
// name of source map
|
|
|
|
private String map = "Mapnik";
|
2010-04-26 17:20:50 +02:00
|
|
|
|
2010-04-27 14:37:49 +02:00
|
|
|
// tile size of map
|
2010-04-26 17:20:50 +02:00
|
|
|
private final int tileSize = 256;
|
2010-04-30 00:47:07 +02:00
|
|
|
|
2010-04-27 14:37:49 +02:00
|
|
|
// file name with tiles
|
2010-04-26 17:20:50 +02:00
|
|
|
private final File fileWithTiles;
|
2010-04-27 14:37:49 +02:00
|
|
|
|
|
|
|
// special points to draw
|
|
|
|
private DataTileManager<LatLon> points;
|
|
|
|
|
|
|
|
// zoom levle
|
2010-04-26 17:20:50 +02:00
|
|
|
private int zoom = 15;
|
|
|
|
|
|
|
|
// degree measurements (-180, 180)
|
2010-04-29 01:32:03 +02:00
|
|
|
// долгота
|
2010-04-26 17:20:50 +02:00
|
|
|
private double longitude = 27.56;
|
2010-04-29 01:32:03 +02:00
|
|
|
// широта
|
2010-04-26 17:20:50 +02:00
|
|
|
// degree measurements (90, -90)
|
|
|
|
private double latitude = 53.9;
|
|
|
|
|
|
|
|
private List<IMapLocationListener> listeners = new ArrayList<IMapLocationListener>();
|
|
|
|
|
2010-04-27 14:37:49 +02:00
|
|
|
// cached data to draw image
|
2010-04-30 00:47:07 +02:00
|
|
|
private Image[][] images;
|
2010-04-27 14:37:49 +02:00
|
|
|
private int xStartingImage = 0;
|
|
|
|
private int yStartingImage = 0;
|
2010-04-30 00:47:07 +02:00
|
|
|
private List<Point> pointsToDraw = new ArrayList<Point>();
|
|
|
|
|
|
|
|
private AsyncLoadTileThread autoThread = new AsyncLoadTileThread();
|
|
|
|
Map<String, Image> cache = new HashMap<String, Image>();
|
2010-04-27 14:37:49 +02:00
|
|
|
|
|
|
|
|
2010-04-26 17:20:50 +02:00
|
|
|
|
|
|
|
public MapPanel(File fileWithTiles) {
|
|
|
|
this.fileWithTiles = fileWithTiles;
|
2010-04-30 00:47:07 +02:00
|
|
|
autoThread.start();
|
2010-04-26 17:20:50 +02:00
|
|
|
initUI();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public double getXTile(){
|
2010-04-27 23:42:19 +02:00
|
|
|
return MapUtils.getTileNumberX(zoom, longitude);
|
2010-04-26 17:20:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public double getYTile(){
|
2010-04-27 23:42:19 +02:00
|
|
|
return MapUtils.getTileNumberY(zoom, latitude);
|
2010-04-26 17:20:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void paintComponent(Graphics g) {
|
2010-04-30 00:47:07 +02:00
|
|
|
|
2010-04-26 17:20:50 +02:00
|
|
|
if (images != null) {
|
|
|
|
for (int i = 0; i < images.length; i++) {
|
|
|
|
for (int j = 0; j < images[i].length; j++) {
|
|
|
|
if(images[i][j] == null){
|
2010-04-27 22:37:40 +02:00
|
|
|
int div = 4;
|
|
|
|
int tileDiv = tileSize / div;
|
|
|
|
for(int k1 = 0; k1 < div; k1++){
|
|
|
|
for(int k2 = 0; k2 < div; k2++){
|
|
|
|
if ((k1 + k2) % 2 == 0) {
|
|
|
|
g.setColor(Color.gray);
|
|
|
|
} else {
|
|
|
|
g.setColor(Color.white);
|
|
|
|
}
|
|
|
|
g.fillRect(i * tileSize+xStartingImage + k1*tileDiv,
|
|
|
|
j * tileSize + yStartingImage + k2*tileDiv, tileDiv, tileDiv);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2010-04-26 17:20:50 +02:00
|
|
|
} else {
|
|
|
|
g.drawImage(images[i][j], i * tileSize+xStartingImage, j * tileSize + yStartingImage, this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
g.setColor(Color.black);
|
2010-04-27 14:37:49 +02:00
|
|
|
// draw user points
|
|
|
|
for(Point p : pointsToDraw){
|
|
|
|
g.drawOval(p.x, p.y, 3, 3);
|
|
|
|
g.fillOval(p.x, p.y, 3, 3);
|
|
|
|
}
|
2010-04-30 00:47:07 +02:00
|
|
|
|
|
|
|
String s = MessageFormat.format("Lat : {0}, lon : {1}, zoom : {2}",latitude, longitude, zoom);
|
|
|
|
g.drawString(s, 5, 20);
|
2010-04-27 14:37:49 +02:00
|
|
|
|
2010-04-26 17:20:50 +02:00
|
|
|
|
|
|
|
g.fillOval(getWidth()/2 - 2, getHeight()/2 -2, 4, 4);
|
|
|
|
g.drawOval(getWidth()/2 - 2, getHeight()/2 -2, 4, 4);
|
|
|
|
g.drawOval(getWidth()/2 - 5, getHeight()/2 -5, 10, 10);
|
|
|
|
}
|
|
|
|
|
2010-04-30 00:47:07 +02:00
|
|
|
public String getFile(int zoom, int x, int y){
|
2010-04-26 17:20:50 +02:00
|
|
|
return map +"/"+zoom+"/"+(x) +"/"+y+".png";
|
|
|
|
}
|
|
|
|
|
2010-04-30 00:47:07 +02:00
|
|
|
|
|
|
|
public Image getImageFor(int x, int y) throws IOException{
|
|
|
|
String file = getFile(zoom, x, y);
|
2010-04-26 17:20:50 +02:00
|
|
|
if(!cache.containsKey(file)){
|
|
|
|
// ZipEntry en = fileWithTiles.getEntry(file);
|
|
|
|
File en = new File(fileWithTiles, file);
|
|
|
|
if(cache.size() > 1000){
|
|
|
|
ArrayList<String> list = new ArrayList<String>(cache.keySet());
|
|
|
|
for(int i=0; i<list.size(); i+=2){
|
|
|
|
cache.remove(list.get(i));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// if(en != null){
|
|
|
|
if(en.exists()){
|
|
|
|
// cache.put(file, ImageIO.read(fileWithTiles.getInputStream(en)));
|
|
|
|
long time = System.currentTimeMillis();
|
|
|
|
cache.put(file, ImageIO.read(en));
|
|
|
|
System.out.println("Loaded " + (System.currentTimeMillis() - time));
|
|
|
|
} else {
|
2010-04-30 00:47:07 +02:00
|
|
|
autoThread.requestTileToLoad(zoom, x, y);
|
2010-04-26 17:20:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return cache.get(file);
|
|
|
|
}
|
|
|
|
|
2010-04-30 00:47:07 +02:00
|
|
|
public void loadImage(int zoom, int x, int y){
|
|
|
|
try {
|
|
|
|
Image img = null;
|
|
|
|
File en = new File(fileWithTiles, getFile(zoom, x, y));
|
|
|
|
URL url = new URL("http://tile.openstreetmap.org/" + zoom + "/" + x + "/" + y + ".png");
|
|
|
|
en.getParentFile().mkdirs();
|
|
|
|
try {
|
|
|
|
ImageIO.setUseCache(true);
|
|
|
|
javax.imageio.ImageIO.setCacheDirectory(en.getParentFile());
|
|
|
|
} catch (Exception e) {
|
|
|
|
log.warn("cannot set ImageIO cache-directory to " + en.getParent(), e);
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
URLConnection connection = url.openConnection();
|
|
|
|
connection.setRequestProperty("User-Agent", "osmand TilePainter/0.1");
|
|
|
|
img = javax.imageio.ImageIO.read(connection.getInputStream());
|
|
|
|
} catch (UnknownHostException e) {
|
|
|
|
log.info("UnknownHostException, cannot download tile " + url.toExternalForm());
|
|
|
|
} catch (IIOException e) {
|
|
|
|
if (e.getCause() != null && e.getCause() instanceof UnknownHostException) {
|
|
|
|
log.info("UnknownHostException, cannot download tile " + url.toExternalForm(), e);
|
|
|
|
} else {
|
|
|
|
log.warn("cannot download tile " + url.toExternalForm(), e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cache.put(getFile(zoom, x, y), img);
|
|
|
|
if (img != null) {
|
|
|
|
try {
|
|
|
|
ImageIO.write((RenderedImage) img, "png", en);
|
|
|
|
} catch (IOException e) {
|
|
|
|
log.warn("cannot save img to " + en.getAbsolutePath(), e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
|
|
log.warn("cannot download pre-rendered tile [" + e.getClass().getSimpleName() + "]", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public class AsyncLoadTileThread extends Thread {
|
|
|
|
Stack<TileIndex> tilesToLoad = new Stack<TileIndex>();
|
|
|
|
private class TileIndex {
|
|
|
|
int zoom;
|
|
|
|
int x;
|
|
|
|
int y;
|
|
|
|
}
|
|
|
|
|
|
|
|
public AsyncLoadTileThread() {
|
|
|
|
super("AsyncLoadingTiles");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
try {
|
|
|
|
while (true) {
|
|
|
|
synchronized (this) {
|
|
|
|
wait();
|
|
|
|
}
|
|
|
|
while (!tilesToLoad.isEmpty()) {
|
|
|
|
TileIndex p = tilesToLoad.pop();
|
|
|
|
if (cache.get(getFile(p.zoom, p.x, p.y)) == null) {
|
|
|
|
loadImage(p.zoom, p.x, p.y);
|
|
|
|
// TODO dead cycle (causes cancelAllPreviousTile)
|
|
|
|
prepareImage();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void cancelAllPreviousTile(){
|
|
|
|
tilesToLoad.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void requestTileToLoad(int zoom, int x, int y){
|
|
|
|
synchronized (this) {
|
|
|
|
TileIndex ind = new TileIndex();
|
|
|
|
ind.zoom = zoom;
|
|
|
|
ind.x = x;
|
|
|
|
ind.y = y;
|
|
|
|
tilesToLoad.add(ind);
|
|
|
|
notify();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-04-26 17:20:50 +02:00
|
|
|
|
|
|
|
// TODO async loading images (show busy cursor while it is loaded)
|
|
|
|
public void prepareImage(){
|
|
|
|
try {
|
|
|
|
|
|
|
|
if (images != null) {
|
|
|
|
for (int i = 0; i < images.length; i++) {
|
|
|
|
for (int j = 0; j < images[i].length; j++) {
|
|
|
|
// dispose
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-30 00:47:07 +02:00
|
|
|
double xTileLeft = getXTile() - getSize().width / (2d * tileSize);
|
|
|
|
double xTileRight = getXTile() + getSize().width / (2d * tileSize);
|
|
|
|
double yTileUp = getYTile() - getSize().height / (2d * tileSize);
|
|
|
|
double yTileDown = getYTile() + getSize().height / (2d * tileSize);
|
2010-04-27 22:37:40 +02:00
|
|
|
|
2010-04-30 00:47:07 +02:00
|
|
|
xStartingImage = -(int) ((xTileLeft - Math.floor(xTileLeft)) * tileSize);
|
|
|
|
yStartingImage = -(int) ((yTileUp - Math.floor(yTileUp)) * tileSize);
|
|
|
|
|
|
|
|
autoThread.cancelAllPreviousTile();
|
|
|
|
int tileXCount = ((int) xTileRight - (int) xTileLeft + 1);
|
|
|
|
int tileYCount = ((int) yTileDown - (int) yTileUp + 1);
|
2010-04-26 17:20:50 +02:00
|
|
|
images = new BufferedImage[tileXCount][tileYCount];
|
2010-04-30 00:47:07 +02:00
|
|
|
for (int i = 0; i < images.length; i++) {
|
|
|
|
for (int j = 0; j < images[i].length; j++) {
|
|
|
|
images[i][j] = getImageFor((int) xTileLeft + i, (int) yTileUp + j);
|
2010-04-26 17:20:50 +02:00
|
|
|
}
|
|
|
|
}
|
2010-04-27 14:37:49 +02:00
|
|
|
|
2010-04-30 00:47:07 +02:00
|
|
|
if (points != null) {
|
2010-04-27 23:42:19 +02:00
|
|
|
double latDown = MapUtils.getLatitudeFromTile(zoom, yTileDown);
|
|
|
|
double longDown = MapUtils.getLongitudeFromTile(zoom, xTileRight);
|
|
|
|
double latUp = MapUtils.getLatitudeFromTile(zoom, yTileUp);
|
|
|
|
double longUp = MapUtils.getLongitudeFromTile(zoom, xTileLeft);
|
2010-04-27 14:37:49 +02:00
|
|
|
List<LatLon> objects = points.getObjects(latUp, longUp, latDown, longDown);
|
|
|
|
pointsToDraw.clear();
|
2010-04-30 00:47:07 +02:00
|
|
|
for (LatLon n : objects) {
|
|
|
|
int pixX = MapUtils.getPixelShiftX(zoom, n.getLongitude(), this.longitude, tileSize) + getWidth() / 2;
|
|
|
|
int pixY = MapUtils.getPixelShiftY(zoom, n.getLatitude(), this.latitude, tileSize) + getHeight() / 2;
|
|
|
|
if (pixX >= 0 && pixY >= 0) {
|
2010-04-27 14:37:49 +02:00
|
|
|
pointsToDraw.add(new Point(pixX, pixY));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-26 17:20:50 +02:00
|
|
|
repaint();
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void initUI() {
|
|
|
|
setFocusable(true);
|
|
|
|
addComponentListener(new ComponentAdapter(){
|
|
|
|
public void componentResized(ComponentEvent e) {
|
|
|
|
prepareImage();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
MapMouseAdapter mouse = new MapMouseAdapter();
|
|
|
|
addMouseListener(mouse);
|
|
|
|
addMouseMotionListener(mouse);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void setZoom(int zoom){
|
|
|
|
this.zoom = zoom;
|
|
|
|
prepareImage();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setLatLon(double latitude, double longitude){
|
|
|
|
this.latitude = latitude;
|
|
|
|
this.longitude = longitude;
|
|
|
|
prepareImage();
|
|
|
|
fireMapLocationListeners();
|
|
|
|
}
|
|
|
|
|
|
|
|
public double getLatitude() {
|
|
|
|
return latitude;
|
|
|
|
}
|
|
|
|
|
|
|
|
public double getLongitude() {
|
|
|
|
return longitude;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getZoom() {
|
|
|
|
return zoom;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getMap(){
|
|
|
|
return map;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setMapName(String map){
|
|
|
|
this.map = map;
|
|
|
|
prepareImage();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void addMapLocationListener(IMapLocationListener l){
|
|
|
|
listeners.add(l);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void removeMapLocationListener(IMapLocationListener l){
|
|
|
|
listeners.remove(l);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected void fireMapLocationListeners(){
|
|
|
|
for(IMapLocationListener l : listeners){
|
2010-04-29 01:32:03 +02:00
|
|
|
l.locationChanged(latitude, longitude, null);
|
2010-04-26 17:20:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void processKeyEvent(KeyEvent e) {
|
|
|
|
boolean processed = false;
|
|
|
|
if (e.getID() == KeyEvent.KEY_RELEASED) {
|
|
|
|
if (e.getKeyCode() == 37) {
|
2010-04-27 14:37:49 +02:00
|
|
|
// LEFT button
|
2010-04-27 23:42:19 +02:00
|
|
|
longitude = MapUtils.getLongitudeFromTile(zoom, getXTile()-0.5);
|
2010-04-26 17:20:50 +02:00
|
|
|
processed = true;
|
|
|
|
} else if (e.getKeyCode() == 39) {
|
2010-04-27 14:37:49 +02:00
|
|
|
// RIGHT button
|
2010-04-27 23:42:19 +02:00
|
|
|
longitude = MapUtils.getLongitudeFromTile(zoom, getXTile()+0.5);
|
2010-04-26 17:20:50 +02:00
|
|
|
processed = true;
|
|
|
|
} else if (e.getKeyCode() == 38) {
|
2010-04-27 14:37:49 +02:00
|
|
|
// UP button
|
2010-04-27 23:42:19 +02:00
|
|
|
latitude = MapUtils.getLatitudeFromTile(zoom, getYTile()-0.5);
|
2010-04-26 17:20:50 +02:00
|
|
|
processed = true;
|
|
|
|
} else if (e.getKeyCode() == 40) {
|
2010-04-27 14:37:49 +02:00
|
|
|
// DOWN button
|
2010-04-27 23:42:19 +02:00
|
|
|
latitude = MapUtils.getLatitudeFromTile(zoom, getYTile()+0.5);
|
2010-04-26 17:20:50 +02:00
|
|
|
processed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(e.getID() == KeyEvent.KEY_TYPED){
|
|
|
|
if(e.getKeyChar() == '+'){
|
2010-04-30 00:47:07 +02:00
|
|
|
if(zoom < 18){
|
|
|
|
zoom ++;
|
|
|
|
processed = true;
|
|
|
|
}
|
2010-04-26 17:20:50 +02:00
|
|
|
} else if(e.getKeyChar() == '-'){
|
2010-04-30 00:47:07 +02:00
|
|
|
if(zoom > 1){
|
|
|
|
zoom --;
|
|
|
|
processed = true;
|
|
|
|
}
|
2010-04-26 17:20:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(processed){
|
|
|
|
e.consume();
|
|
|
|
prepareImage();
|
|
|
|
fireMapLocationListeners();
|
|
|
|
}
|
|
|
|
super.processKeyEvent(e);
|
|
|
|
}
|
|
|
|
|
2010-04-27 14:37:49 +02:00
|
|
|
public DataTileManager<LatLon> getPoints() {
|
|
|
|
return points;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setPoints(DataTileManager<LatLon> points) {
|
|
|
|
this.points = points;
|
|
|
|
}
|
|
|
|
|
2010-04-26 17:20:50 +02:00
|
|
|
public class MapMouseAdapter extends MouseAdapter {
|
|
|
|
private Point startDragging = null;
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void mouseClicked(MouseEvent e) {
|
|
|
|
if(e.getButton() == MouseEvent.BUTTON1){
|
|
|
|
requestFocus();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void dragTo(Point p){
|
|
|
|
double dx = (startDragging.x - (double)p.x)/tileSize;
|
|
|
|
double dy = (startDragging.y - (double)p.y)/tileSize;
|
2010-04-27 23:42:19 +02:00
|
|
|
double lat = MapUtils.getLatitudeFromTile(zoom, getYTile() + dy);
|
|
|
|
double lon = MapUtils.getLongitudeFromTile(zoom, getXTile() + dx);
|
2010-04-26 17:20:50 +02:00
|
|
|
setLatLon(lat, lon);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void mouseDragged(MouseEvent e) {
|
|
|
|
if(startDragging != null){
|
|
|
|
if(Math.abs(e.getPoint().x - startDragging.x) + Math.abs(e.getPoint().y - startDragging.y) >= 8){
|
|
|
|
dragTo(e.getPoint());
|
|
|
|
startDragging = e.getPoint();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void mousePressed(MouseEvent e) {
|
|
|
|
if(e.getButton() == MouseEvent.BUTTON3){
|
|
|
|
if(startDragging == null){
|
|
|
|
startDragging = e.getPoint();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@Override
|
|
|
|
public void mouseReleased(MouseEvent e) {
|
|
|
|
if(e.getButton() == MouseEvent.BUTTON3){
|
|
|
|
if(startDragging != null){
|
|
|
|
dragTo(e.getPoint());
|
|
|
|
fireMapLocationListeners();
|
|
|
|
startDragging = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
super.mouseReleased(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|