4

I'm trying to draw a polyline on a google maps using the Android GPX Parser library, I have little experience in programming, the code I'm using reads the .gpx file and provides the result in the android studio logcat but does not draw the polyline on the map. manually defined as "doulbe LatitudeL [] = {Lat, Lat, Lat ...}" and "double LongitudeL [] = {Lng, Lng, Lng ...}" The code works and draws the polyline correctly. A tip would be of great help.

public class PolyGpxActivity2 extends AppCompatActivity implements OnMapReadyCallback, GoogleMap.OnPolylineClickListener {

private static final int COLOR_BLUE_ARGB = 0xffF9A825;

private static final int POLYLINE_STROKE_WIDTH_PX = 10;
private static final int PATTERN_GAP_LENGTH_PX = 20;
private static final PatternItem DOT = new Dot();
private static final PatternItem GAP = new Gap(PATTERN_GAP_LENGTH_PX);

// Create a stroke pattern of a gap followed by a dot.
private static final List<PatternItem> PATTERN_POLYLINE_DOTTED = Arrays.asList(GAP, DOT);

static final String TAG = PolyGpxActivity.class.getSimpleName();

// consider injection with, eg. Dagger2
GPXParser mParser = new GPXParser();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Retrieve the content view that renders the map.
    setContentView(R.layout.activity_maps);

    // Get the SupportMapFragment and request notification when the map is ready to be used.
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(map);
    mapFragment.getMapAsync(this);
}

@Override
public void onMapReady(GoogleMap googleMap) {

    Gpx parsedGpx = null;
    try {
        InputStream in = getAssets().open("test.gpx");
        parsedGpx = mParser.parse(in);
    } catch (IOException | XmlPullParserException e) {
        e.printStackTrace();
    }
    if (parsedGpx != null) {
        // log stuff
        List<Track> tracks = parsedGpx.getTracks();
        for (int i = 0; i < tracks.size(); i++) {
            Track track = tracks.get(i);
            //Log.d(TAG, "track " + i + ":");
            List<TrackSegment> segments = track.getTrackSegments();
            for (int j = 0; j < segments.size(); j++) {
                TrackSegment segment = segments.get(j);

                //Log.d(TAG, "  segment " + j + ":");
                for (TrackPoint trackPoint : segment.getTrackPoints()) {
                    // Log.d(TAG, "    point: lat " + trackPoint.getLatitude() + ", lon " + trackPoint.getLongitude());

                    double LatitudeL[] = {trackPoint.getLatitude()};
                    double LongitudeL[] = {trackPoint.getLongitude()};

                    //double LatitudeL[] = {-27.7138761151582, -27.713891873136163, -27.713900171220303, -27.713972255587578, -27.713974770158529, -27.713967896997929, -27.713976949453354, -27.713974099606276, -27.713983403518796, -27.713974267244339, -27.713965885341167, -27.713977117091417, -27.71568794734776, -27.715969746932387, -27.719649067148566, -27.748650452122092};
                    //double LongitudeL[] = {-48.703893441706896, -48.703868128359318, -48.703850442543626, -48.703786907717586, -48.703775340691209, -48.703786404803395, -48.703784644603729, -48.70379202067852, -48.70379101485014, -48.703799732029438, -48.703791350126266, -48.703792775049806, -48.703289357945323, -48.703037817031145, -48.701020879670978, -48.710173834115267};

                    Log.d(TAG, Arrays.toString(LatitudeL) + ", " + Arrays.toString(LongitudeL));

                    List<LatLng> points = new ArrayList<LatLng>();

                    for (int k = 0; k < LatitudeL.length; k++) {
                        points.add(new LatLng(LatitudeL[k], LongitudeL[k]));
                    }
                    if (points.size() > 1) {

                        PolylineOptions polyline_options = new PolylineOptions()
                                .addAll(points);

                        Polyline polyline1 = googleMap.addPolyline(polyline_options);
                        polyline1.setTag("A");
                        stylePolyline(polyline1);
                    }
                    // Position the map's camera near Alice Springs in the center of Australia,
                    // and set the zoom factor so most of Australia shows on the screen.
                    googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(-27.778812730684876, -48.50724393501878), 12));

                    // Set listeners for click events.
                    googleMap.setOnPolylineClickListener(this);
                }
            }
        }
    } else {
        Log.e(TAG, "Error parsing gpx track!");
    }
}

/**
 * Styles the polyline, based on type.
 * @param polyline The polyline object that needs styling.
 */
private void stylePolyline(Polyline polyline) {
    String type = "";
    // Get the data object stored with the polyline.
    if (polyline.getTag() != null) {
        type = polyline.getTag().toString();
    }

    switch (type) {
        // If no type is given, allow the API to use the default.
        case "A":
            // Use a custom bitmap as the cap at the start of the line.
            polyline.setStartCap(
                    new CustomCap(
                            BitmapDescriptorFactory.fromResource(R.drawable.ic_arrow), 10));
            polyline.setEndCap(
                    new CustomCap(
                            BitmapDescriptorFactory.fromResource(R.drawable.ic_arrow), 10));
            break;
        case "B":
            // Use a round cap at the start of the line.
            polyline.setStartCap(new RoundCap());
            break;
    }

    polyline.setEndCap(new RoundCap());
    polyline.setWidth(POLYLINE_STROKE_WIDTH_PX);
    polyline.setColor(COLOR_BLUE_ARGB);
    polyline.setJointType(JointType.ROUND);
}

/**
 * Listens for clicks on a polyline.
 * @param polyline The polyline object that the user has clicked.
 */
@Override
public void onPolylineClick(Polyline polyline) {
    // Flip from solid stroke to dotted stroke pattern.
    if ((polyline.getPattern() == null) || (!polyline.getPattern().contains(DOT))) {
        polyline.setPattern(PATTERN_POLYLINE_DOTTED);
    } else {
        // The default pattern is a solid stroke.
        polyline.setPattern(null);
    }

    Toast.makeText(this, "Route type " + polyline.getTag().toString(),
            Toast.LENGTH_SHORT).show();
}

}

Naldo
  • 41
  • 2

0 Answers0