JXMapViewer (openstreetmap) Drow Directions between 2 point

421 views Asked by At

I have this Class That Draws a line between specified points or locations in the map And it works !

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);

    }
}

but I want to draw a route that can follow by car. Not just a straight line that cut across all the building !.

When I select two points on the map, I need to see a suggested path or path to reach, not just a line !

i need like this image

enter image description here

Any Help ?

0

There are 0 answers