OSMDroid PathOverlay

17,284

Solution 1

It will draw a series of straight lines for you on top of the map, so you need to know the latitude and longitude of all your road junctions (and everywhere they bend away from a straight line). Add all these points to the overlay. As an example, this code will draw a rectangular box in central London.

public class OsmdroidDemoMap extends Activity {

    private MapView mMapView;
    private MapController mMapController;
    int mIncr = 10000;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.osm_main);
        mMapView = (MapView) findViewById(R.id.mapview);
        mMapView.setTileSource(TileSourceFactory.MAPNIK);
        mMapView.setBuiltInZoomControls(true);
        mMapView.setMultiTouchControls(true);
        mMapController = mMapView.getController();
        mMapController.setZoom(13);
        GeoPoint gPt0 = new GeoPoint(51500000, -150000);
        GeoPoint gPt1 = new GeoPoint(gPt0.getLatitudeE6()+ mIncr, gPt0.getLongitudeE6());
        GeoPoint gPt2 = new GeoPoint(gPt0.getLatitudeE6()+ mIncr, gPt0.getLongitudeE6() + mIncr);
        GeoPoint gPt3 = new GeoPoint(gPt0.getLatitudeE6(), gPt0.getLongitudeE6() + mIncr);
        mMapController.setCenter(gPt0);
        PathOverlay myPath = new PathOverlay(Color.RED, this);
        myPath.addPoint(gPt0);
        myPath.addPoint(gPt1);
        myPath.addPoint(gPt2);
        myPath.addPoint(gPt3);
        myPath.addPoint(gPt0);
        mMapView.getOverlays().add(myPath);
    }
}

.

Solution 2

Here is the tutorial how to draw the road with Polyline in OSMBonusPack: https://github.com/MKergall/osmbonuspack/wiki/Tutorial_1

It is pretty easy and I have successfully used this in my app.

My code based on that tutorial is looking like this:

    RoadManager roadManager = new OSRMRoadManager();

    ArrayList<GeoPoint> track = new ArrayList<>();
    // TODO: Fill the list with your track points

    Road road = roadManager.getRoad(track);
    Polyline roadOverlay = RoadManager.buildRoadOverlay(road, context);
    mapView.getOverlays().add(roadOverlay);
    mapView.invalidate();
Share:
17,284
silentw
Author by

silentw

Just an average programmer learning with others!

Updated on June 14, 2022

Comments

  • silentw
    silentw almost 2 years

    Today I'm looking forward of how to use PathOverlay in OSMDroid.

    I can't find any explanation of how it works.

    I need to create a suggested route (not like navigation system), just a stroke begining at a point, do a "circuit" and then return to the starting point.

    Just like this (drawn in google maps):

    Circuit

    I'm here to ask what's the correct way to do this, specifying a custom path, doing the turns I want.

    Thanks!

  • silentw
    silentw about 12 years
    Do we have to add a GeoPoint on every single turn we want to make?
  • NickT
    NickT about 12 years
    Yes, it only draws straight lines, so every slight bend will need a new point.
  • silentw
    silentw about 12 years
    Hmm, okay. Thank you for your answer!
  • Jeff Allen
    Jeff Allen over 8 years
    PathOverlay is deprecated. It would be helpful to have an example with Polyline.