我正在寻找一种方法,以编程方式将路点添加到使用JXMapKit (在Java Swing工具包上运行)显示的地图中。我想提供一个列表中的地理坐标列表。
发布于 2013-04-24 04:02:31
您必须通过WaypointPainter提供Waypoints,并将此管理器传递给JXMapViewer。默认情况下,WaypointPainter接受Set<Waypoint>,因此我们可以使用自己的类来扩展WaypointPainer,而不是接受List。
import org.jdesktop.swingx.JXMapKit;
import org.jdesktop.swingx.JXMapViewer;
import org.jdesktop.swingx.mapviewer.DefaultWaypoint;
import org.jdesktop.swingx.mapviewer.Waypoint;
import org.jdesktop.swingx.mapviewer.WaypointPainter;
import javax.swing.*;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
class CustomPainter extends WaypointPainter<JXMapViewer> {
public void setWaypoints(List<? extends Waypoint> waypoints) {
super.setWaypoints(new HashSet<Waypoint>(waypoints));
}
}
public class Waypoints {
public static void main(String[] args) {
List<DefaultWaypoint> waypoints = new ArrayList<DefaultWaypoint>();
waypoints.add(new DefaultWaypoint(51.5, 0));
JXMapKit jxMapKit = new JXMapKit();
jxMapKit.setDefaultProvider(JXMapKit.DefaultProviders.OpenStreetMaps);
CustomPainter painter = new CustomPainter();
painter.setWaypoints(waypoints);
jxMapKit.getMainMap().setOverlayPainter(painter);
final JFrame frame = new JFrame();
frame.add(jxMapKit);
frame.setSize(300, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
frame.setVisible(true);
}
});
}
}https://stackoverflow.com/questions/16171611
复制相似问题