0

I'm using jxmapviewer to make satellite tracking tracking system. I draw the path first and then pointing the satellite over it using waypointer. The code for the above is -

public class MapJPanel extends JPanel implements Runnable {

    private static final long serialVersionUID = 1L;
    JXMapViewer mapViewer;
    TileFactoryInfo info;
    DefaultTileFactory tileFactory;
    private List<GeoPosition> track;
    private List<double[]> data;
    private double[] latlng;
    static boolean active = true;

    MapJPanel() {
       setLayout(new BorderLayout());
       mapViewer = new JXMapViewer();
       info = new VirtualEarthTileFactoryInfo(VirtualEarthTileFactoryInfo.HYBRID);
       tileFactory = new DefaultTileFactory(info);
       mapViewer.setTileFactory(tileFactory);
       GeoPosition geo = new GeoPosition(0, 50);
       mapViewer.setAddressLocation(geo);
       mapViewer.setZoom(17);
       MouseInputListener mouseInputListener = new PanMouseInputListener(mapViewer);
       mapViewer.addMouseListener(mouseInputListener);
       mapViewer.addMouseMotionListener(mouseInputListener);
       mapViewer.addMouseWheelListener(new ZoomMouseWheelListenerCenter(mapViewer));
       // Adding panel
       add(mapViewer, BorderLayout.CENTER);
       track = new ArrayList<GeoPosition>();
       latlng = new double[2];
       data = new ArrayList<double[]>();
    }

    public void setPath(List<double[]> data) {
       int n = data.size();
       System.out.println(n);
       track.clear();
       for (int i = 0; i < n; i++) {
          track.add(new GeoPosition(data.get(i)[0], data.get(i)[1]));
       }
       RoutePainter routePainter = new RoutePainter(track);
       List<Painter<JXMapViewer>> painters = new ArrayList<Painter<JXMapViewer>>();
       painters.add(routePainter);
       CompoundPainter<JXMapViewer> painter = new CompoundPainter<JXMapViewer>(painters);
       mapViewer.setOverlayPainter(painter);
       mapViewer.zoomToBestFit(new HashSet<GeoPosition>(track), 1.0);
    }

    public void plotSat(double[] latlng) {
       WaypointPainter<Waypoint> waypointPainter = new WaypointPainter<Waypoint>();
       Set<Waypoint> waypoints = new HashSet<Waypoint>(Arrays.asList(new DefaultWaypoint(latlng[0], latlng[1])));
       waypointPainter.setWaypoints(waypoints);
       List<Painter<JXMapViewer>> painters = new ArrayList<Painter<JXMapViewer>>();
       painters.add(waypointPainter);
       CompoundPainter<JXMapViewer> painter = new CompoundPainter<JXMapViewer>(painters);
       mapViewer.setOverlayPainter(painter);
    }

    @Override
    public void run() {
       active = true;
       while (active == true) {
          satPostion(latlng);
          this.plotSat(latlng);
          try {
             Thread.sleep(2000);
          } catch (InterruptedException e) {
             e.printStackTrace();
             return;
          }
       }
    }

    public void stopThread() {
       active = false;
    }
 }

This is Routepainter Class-

public class RoutePainter implements Painter<JXMapViewer> {
    private Color color = Color.RED;
    private boolean antiAlias = true;

    private List<GeoPosition> track;

    public RoutePainter(List<GeoPosition> track) {
        // copy the list so that changes in the
        // original list do not have an effect here
        this.track = new ArrayList<GeoPosition>(track);
    }

    @Override
    public void paint(Graphics2D g, JXMapViewer map, int w, int h) {
        g = (Graphics2D) g.create();

        // convert from viewport to world bitmap
        Rectangle rect = map.getViewportBounds();
        g.translate(-rect.x, -rect.y);

        if (antiAlias)
            g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        // do the drawing
        g.setColor(Color.BLACK);
        g.setStroke(new BasicStroke(4));

        drawRoute(g, map);

        // do the drawing again
        g.setColor(color);
        g.setStroke(new BasicStroke(2));

        drawRoute(g, map);

        g.dispose();
    }

    private void drawRoute(Graphics2D g, JXMapViewer map) {
        int lastX = 0;
        int lastY = 0;

        boolean first = true;

        for (GeoPosition gp : track) {
            // convert geo-coordinate to world bitmap pixel
            Point2D pt = map.getTileFactory().geoToPixel(gp, map.getZoom());

            if (first) {
                first = false;
            } else {
                g.drawLine(lastX, lastY, (int) pt.getX(), (int) pt.getY());
            }

            lastX = (int) pt.getX();
            lastY = (int) pt.getY();
        }
    }
}

I have write this code by taking help from https://github.com/msteiger/jxmapviewer2/blob/master/examples/src/sample2_waypoints/Sample2.java The problem with this code is it removes the track as I insert a waypoint. Is there any way to hold it, while updating satellite position (i.e. updating the waypoint).

0 Answers0