JXMapKit OpenStreetmaps创建路线

时间:2011-05-05 07:52:01

标签: java netbeans routing openstreetmap

我正在使用JXMapKit和openStreetMap作为提供者,我正在尝试找到一种在地图上显示路线的方法。然后使用这些路线,以便车辆可以跟随它们。无论如何都有这样做或有其他选择吗?

提前致谢。

1 个答案:

答案 0 :(得分:1)

是的,你可以。摘自此链接:http://www.naxos-software.com/blog/index.php?/archives/92-TracknMash-Openstreetmap-Karten-in-JavaSwing-mit-JXMapViewer.html

您编写自己的Painter类并重写paint方法以绘制线条:

public void paint... {
g = (Graphics2D) g.create();
    // convert from viewport to world bitmap
    Rectangle rect = object.getViewportBounds();
    g.translate(-rect.x, -rect.y);

    //set the line properties
    g.setColor(Color.RED);
    g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g.setStroke(new BasicStroke(2));

    // do the drawing
    int lastX = -1;
    int lastY = -1;
    for (GeoPosition point : points) {
        Point2D pt = object.getTileFactory().geoToPixel(point, object.getZoom());
        if (lastX != -1 && lastY != -1) {
            g.drawLine(lastX, lastY, (int) pt.getX(), (int) pt.getY());
        }
        lastX = (int) pt.getX();
        lastY = (int) pt.getY();
    }
}

其中points是您的路线的GeoPosition对象列表。

然后将此Painter作为OverlayPainter添加到MapKit:

myMapKit.getMainMap().setOverlayPainter(new MyPainter(myMockPoints));