jxmapviewer(openstreetmap)两点之间的drow方向

bfrts1fy  于 2021-07-09  发布在  Java
关注(0)|答案(0)|浏览(250)

我有一个类,它在Map中的指定点或位置之间画一条线,它可以工作!

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.geom.Point2D;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JFrame;

import org.jxmapviewer.JXMapKit;
import org.jxmapviewer.JXMapKit.DefaultProviders;
import org.jxmapviewer.JXMapViewer;
import org.jxmapviewer.painter.Painter;
import org.jxmapviewer.viewer.GeoPosition;

public class Starter {
    public static void main(final String[] args) {    
        final JFrame f = new JFrame();
        f.setSize(500, 300);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        final JXMapKit jXMapKit1 = new JXMapKit();
        jXMapKit1.setDefaultProvider(DefaultProviders.OpenStreetMaps);
        jXMapKit1.setCenterPosition(new GeoPosition(5.41984, 100.33924));
        jXMapKit1.setZoom(3);

        final List<GeoPosition> region = new ArrayList<GeoPosition>();
        region.add(new GeoPosition(5.42031, 100.34389));
        region.add(new GeoPosition(5.41984, 100.33924));
        region.add(new GeoPosition(5.42300, 100.33456));

        final Painter<JXMapViewer> lineOverlay = new Painter<JXMapViewer>() {

            @Override
            public void paint(Graphics2D g, final JXMapViewer map, final int w, final int h) {
                g = (Graphics2D) g.create();
                // convert from viewport to world bitmap
                final Rectangle rect = jXMapKit1.getMainMap().getViewportBounds();
                g.translate(-rect.x, -rect.y);

                // do the drawing
                g.setColor(Color.RED);
                g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
                g.setStroke(new BasicStroke(2));

                int lastX = -1;
                int lastY = -1;
                for (final GeoPosition gp : region) {
                    // convert geo to world bitmap pixel
                    final Point2D pt = jXMapKit1.getMainMap().getTileFactory().geoToPixel(gp, jXMapKit1.getMainMap().getZoom());
                    if (lastX != -1 && lastY != -1) {
                        g.drawLine(lastX, lastY, (int) pt.getX(), (int) pt.getY());
                    }
                    lastX = (int) pt.getX();
                    lastY = (int) pt.getY();
                }

                g.dispose();

            }

        };

        jXMapKit1.getMainMap().setOverlayPainter(lineOverlay);

        f.setContentPane(jXMapKit1);
        f.setVisible(true);

    }
}

但我想画一条可以开车的路线。不仅仅是一条横穿整栋楼的直线!。
当我在Map上选择两个点时,我需要看到一条建议的路径或要到达的路径,而不仅仅是一条线!
我需要像这样的图像

有什么帮助吗?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题