4

I'm currently using polyline to draw a gpx track on OSM map.

I'm wondering if there's a way to draw dotted lines instead of solid lines using osmdroid ? Here is my code :

        Double firstLat, lastLat, firstLong, lastLong;
        firstLat = lastLat = firstLong = lastLong = -100.0;


        for (Track track : gpx.getTracks()) {

                for (TrackSegment trackSegment : track.getTrackSegments()) {


                        Polyline line = new Polyline(mMap);
                        ArrayList<GeoPoint> trajet = new ArrayList<>();

                        for (TrackPoint trackPoint : trackSegment.getTrackPoints()) {
                            GeoPoint geoPoint = new GeoPoint(trackPoint.getLatitude(), trackPoint.getLongitude());
                            trajet.add(geoPoint);

                            if (firstLat == -100.0) { firstLat = trackPoint.getLatitude(); }

                            if (lastLat == -100.0) { lastLat = trackPoint.getLatitude(); }

                            if (firstLong == -100.0) { firstLong = trackPoint.getLongitude(); }

                            if (lastLong == -100.0) { lastLong = trackPoint.getLongitude(); }

                            if (trackPoint.getLatitude() < firstLat) { firstLat = trackPoint.getLatitude(); }

                            if (trackPoint.getLatitude() > lastLat) { lastLat = trackPoint.getLatitude(); }

                            if (trackPoint.getLongitude() < firstLong) { firstLong = trackPoint.getLongitude(); }

                            if (trackPoint.getLongitude() > lastLong) { lastLong = trackPoint.getLongitude(); }
                        }



                        line.setPoints(trajet);
                        line.setInfoWindow(null);
                        line.setColor(Color.rgb(0,191,255));

                        mMap.getOverlays().add(0, line);
                        mMap.invalidate();
        }

Thank you !

EDIT : Using line.getPaint().setPathEffect(new DashPathEffect(new float[]{10, 20}, 0)); works for this issue.

Flint12
  • 71
  • 10
  • In general one should look at the documentation of the said library and the said class and check for a method to accomplish the desired action or effect. In this case it's osmdroid's [Polyline](https://osmdroid.github.io/osmdroid/javadocAll/org/osmdroid/views/overlay/Polyline.html). There's only `setColor()` and `setWidth()`. Or one might simply search for "osmdroid dotted line" and find [a discussion about it in osmdroid issue tracker](https://github.com/osmdroid/osmdroid/issues/1187). Looks like it's not currently possible. – Markus Kauppinen Jun 20 '19 at 12:19
  • But in case of osmdroid, if there's a feature you are missing it's always worth looking at the [osmdroid Bonus Pack](https://github.com/MKergall/osmbonuspack). Though I don't think it changes the `Polyline` in any way. – Markus Kauppinen Jun 20 '19 at 12:20
  • Thank you @MarkusKauppinen for your answer, I'm gonna take a look at these links. – Flint12 Jun 20 '19 at 12:23

1 Answers1

3

Code working using getpaint().setPathEffect():

Double firstLat, lastLat, firstLong, lastLong;
        firstLat = lastLat = firstLong = lastLong = -100.0;


        for (Track track : gpx.getTracks()) {

                for (TrackSegment trackSegment : track.getTrackSegments()) {


                        Polyline line = new Polyline(mMap);
                        ArrayList<GeoPoint> trajet = new ArrayList<>();

                        for (TrackPoint trackPoint : trackSegment.getTrackPoints()) {
                            GeoPoint geoPoint = new GeoPoint(trackPoint.getLatitude(), trackPoint.getLongitude());
                            trajet.add(geoPoint);

                            if (firstLat == -100.0) { firstLat = trackPoint.getLatitude(); }

                            if (lastLat == -100.0) { lastLat = trackPoint.getLatitude(); }

                            if (firstLong == -100.0) { firstLong = trackPoint.getLongitude(); }

                            if (lastLong == -100.0) { lastLong = trackPoint.getLongitude(); }

                            if (trackPoint.getLatitude() < firstLat) { firstLat = trackPoint.getLatitude(); }

                            if (trackPoint.getLatitude() > lastLat) { lastLat = trackPoint.getLatitude(); }

                            if (trackPoint.getLongitude() < firstLong) { firstLong = trackPoint.getLongitude(); }

                            if (trackPoint.getLongitude() > lastLong) { lastLong = trackPoint.getLongitude(); }
                        }



                        line.setPoints(trajet);
                        line.setInfoWindow(null);
                        line.setColor(Color.rgb(0,191,255));
                        line.getPaint().setPathEffect(new DashPathEffect(new float[]{10, 20}, 0));


                        mMap.getOverlays().add(0, line);
                        mMap.invalidate();
        }
Flint12
  • 71
  • 10