JXMap Viewer добавляет ссылку между путевыми точкамиJAVA

Программисты JAVA общаются здесь
Ответить
Anonymous
 JXMap Viewer добавляет ссылку между путевыми точками

Сообщение Anonymous »

`Здравствуйте, я пытаюсь создать код, в котором линии рисуются между разными аэропортами Франции, но проблема в том, что я могу рисовать путевые точки, но не могу провести линии между некоторыми из них.
Я использую два типа файлов: файл .txt, в котором я извлекаю координаты, используемые в координатах карты, и ArrayList, который присваивает код (строку) каждой путевой точке. У меня также есть файл .csv, в котором я получаю 2 строки. Эти две строки представляют собой путевые точки для связи, и эти строки соответствуют двум кодам, которые обычно уже используются для названия путевой точки.
Я пытаюсь создать линию между различными путевыми точками. на основе файлов .csv.
Мой код:

Код: Выделить всё

package saej.lectureCSV;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.geom.Point2D;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.swing.JFrame;
import org.jxmapviewer.JXMapViewer;
import org.jxmapviewer.OSMTileFactoryInfo;
import org.jxmapviewer.input.PanMouseInputListener;
import org.jxmapviewer.input.ZoomMouseWheelListenerCenter;
import org.jxmapviewer.painter.CompoundPainter;
import org.jxmapviewer.painter.Painter;
import org.jxmapviewer.viewer.DefaultTileFactory;
import org.jxmapviewer.viewer.GeoPosition;
import org.jxmapviewer.viewer.TileFactoryInfo;
import org.jxmapviewer.viewer.Waypoint;
import org.jxmapviewer.viewer.WaypointPainter;
import javax.swing.event.MouseInputListener;
import org.jxmapviewer.viewer.DefaultWaypoint;

public class AffichageMap {

private Map coordinates;
private JXMapViewer mapViewer;
private Set waypoints;
private ArrayList code;
private Map vol;
private String nom;
private Map codeWaypointMap;

public AffichageMap(Map coordinates, ArrayList code) {
if (coordinates != null) {
this.coordinates = coordinates;
} else {
this.coordinates = new HashMap();
}

if (code != null) {
this.code = code;
} else {
this.code = new ArrayList();
}
this.waypoints = new HashSet();
this.codeWaypointMap = new HashMap();
initMapViewer();
ajouterWaypoints();
afficherWaypoints();
}

public AffichageMap(Map vol, String nom) {
if (vol != null) {
this.vol = vol;
} else {
this.vol = new HashMap();
}

if (nom != null) {
this.nom = nom;
} else {
this.nom = "";
}

this.codeWaypointMap = new HashMap();
this.waypoints = new HashSet();
initMapViewer();

ajouterWaypointsFromVol();
}

private void initMapViewer() {
mapViewer = new JXMapViewer();
TileFactoryInfo info = new OSMTileFactoryInfo();
DefaultTileFactory tileFactory = new DefaultTileFactory(info);
mapViewer.setTileFactory(tileFactory);

mapViewer.setZoom(8);
mapViewer.setAddressLocation(new GeoPosition(46.1753, 2.4650));
MouseInputListener mil = new PanMouseInputListener(mapViewer);
mapViewer.addMouseListener(mil);
mapViewer.addMouseMotionListener(mil);
mapViewer.addMouseWheelListener(new ZoomMouseWheelListenerCenter(mapViewer));
}

private void ajouterWaypoints() {
if (coordinates == null || coordinates.isEmpty()) {
System.out.println("No coordinates provided.");
return;
}

int index = 0;
for (Map.Entry entry : coordinates.entrySet()) {
double lat = entry.getKey();
double lon = entry.getValue();
String codeStr;
if (index < code.size()) {
codeStr = code.get(index);
} else {
codeStr = "";
}
CodeWaypoint waypoint = new CodeWaypoint(new GeoPosition(lat, lon), codeStr);

waypoints.add(waypoint);
codeWaypointMap.put(codeStr, waypoint);
index++;
System.out.println(codeStr);
}

// Debugging output
System.out.println("Waypoints added:");
for (String key : codeWaypointMap.keySet()) {
System.out.println("Code: " + key + ", Position: " + codeWaypointMap.get(key).getPosition());
}
}

private void ajouterWaypointsFromVol() {
if (vol == null || vol.isEmpty()) {
System.out.println("No vol data provided.");
return;
}

for (Map.Entry  entry : vol.entrySet()) {
String code1 = entry.getKey();
String code2 = entry.getValue();
CodeWaypoint wp1 = codeWaypointMap.get(code1);
CodeWaypoint wp2 = codeWaypointMap.get(code2);

if (wp1 != null && wp2 != null && !wp1.equals(wp2)) {
System.out.println("Adding edge between: " + wp1.getCode() + " and " + wp2.getCode());
LinePainter linePainter = new LinePainter(wp1.getPosition(), wp2.getPosition());

Painter currentPainter = mapViewer.getOverlayPainter();
List
> painters = new ArrayList();
if (currentPainter instanceof CompoundPainter) {
painters.addAll(((CompoundPainter) currentPainter).getPainters());
} else if (currentPainter != null) {
painters.add((Painter) currentPainter);
}
painters.add(linePainter);

CompoundPainter compoundPainter = new CompoundPainter(painters);
mapViewer.setOverlayPainter(compoundPainter);
} else {
if (wp1 == null) {
System.out.println("Waypoint not found for code: " + code1);
}
if (wp2 == null) {
System.out.println("Waypoint not found for code: " + code2);
}
if (wp1 != null && wp2 != null && wp1.equals(wp2)) {
System.out.println("Waypoints are the same for codes: " + code1 + " and " + code2);
}
}
}

mapViewer.repaint();
}

private void afficherWaypoints() {
WaypointPainter waypointPainter = new WaypointPainter();
waypointPainter.setWaypoints(waypoints);
mapViewer.setOverlayPainter(waypointPainter);
}

public void JFrameMap() {
JFrame frame = new JFrame("Map");
frame.getContentPane().add(mapViewer);
frame.setSize(800, 600);
frame.setVisible(true);
}

public void ajouterArete() {
ajouterWaypointsFromVol();
}

public void afficherArete() {
ajouterWaypointsFromVol(); // Ensure edges are displayed
}
}

class CodeWaypoint extends DefaultWaypoint {

private String code;

public CodeWaypoint(GeoPosition position, String code) {
super(position);
this.code = code;
}

public String getCode() {
return code;
}
}

class LinePainter implements Painter {

private final GeoPosition gp1;
private final GeoPosition gp2;

public LinePainter(GeoPosition gp1, GeoPosition gp2) {
this.gp1 = gp1;
this.gp2 = gp2;
}

@Override
public void paint(Graphics2D g, JXMapViewer map, int width, int height) {
System.out.println("Painting line between: " + gp1 + " and " + gp2);
Point2D pt1 = map.getTileFactory().geoToPixel(gp1, map.getZoom());
Point2D pt2 = map.getTileFactory().geoToPixel(gp2, map.getZoom());

pt1.setLocation(pt1.getX() - map.getViewportBounds().getX(), pt1.getY() - map.getViewportBounds().getY());
pt2.setLocation(pt2.getX() - map.getViewportBounds().getX(), pt2.getY() - map.getViewportBounds().getY());

g.setColor(Color.RED);
g.setStroke(new BasicStroke(2));
g.drawLine((int) pt1.getX(), (int) pt1.getY(), (int) pt2.getX(), (int) pt2.getY());
}
}

`


Подробнее здесь: https://stackoverflow.com/questions/785 ... -waypoints
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «JAVA»