2

我正在寻找一种以编程方式将航点添加到使用JXMapKit(在 Java Swing 工具包上运行)显示的地图的方法。我想在列表中提供地理坐标列表。

4

1 回答 1

3

您必须提供Waypoints通过 aWaypointPainter并将此绘制器传递给 JXMapViewer。默认情况下WaypointPainter接受 a Set<Waypoint>,所以我们可以WaypointPainer用我们自己的接受 a 的类来扩展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);
            }
        });
    }
}
于 2013-04-23T20:02:31.080 回答