0

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 ?

Adeeb Mark
  • 67
  • 7
  • 2
    You have to get the meta information for each Open Street tile and use a depth-first search or an [A* search algorithm](https://en.wikipedia.org/wiki/A*_search_algorithm) to find the shortest path. – Gilbert Le Blanc Apr 21 '21 at 21:03
  • can you please explain more with class or code example? – Adeeb Mark Apr 22 '21 at 05:42
  • 1
    Alternatively use one of the [OSM-based online routers](https://wiki.openstreetmap.org/wiki/Routing/online_routers). – scai Apr 22 '21 at 06:20
  • so how can i modify my code for it ? – Adeeb Mark Apr 22 '21 at 14:10
  • What have you tried so far? Also see the answers for https://meta.stackoverflow.com/q/261592/1340631. – scai Apr 23 '21 at 07:40
  • Trust me, I know the rues in stackoverflow ! And I searched again and again, I did not get what I wanted, so I decided to ask this question. If you have something useful that is not just a link to rues and advice that I know, then Give it ! @scai – Adeeb Mark Apr 23 '21 at 19:13
  • 1
    @AdeebMark I think I already did. Choose one of the online routers. Then use their API to obtain a route between your two points. Parse the returned route and draw a line for each segment. – scai Apr 26 '21 at 09:24

0 Answers0