我正在寻找一种以编程方式将路标添加到使用JXMapKit
(在Java Swing工具包上运行)显示的地图的方法。我想在列表中提供地理坐标列表。
答案 0 :(得分:3)
您必须通过Waypoints
提供WaypointPainter
并将此画家传递给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);
}
});
}
}