简体   繁体   中英

Drawing line between 2+ markers/wievpoints (not dynamically) in a MapBox on Android

dev spirits :)

Since yesterday I'm diggning through the google and github to find an answer to the above question. I cannot find correct solution on mapbox documentation page as well.

Ok, I have found the solution! Beside the PolyLine there have to be Markers placed on the map as well. Nevertheless, if we would like to use all the markers that represents waypoint it will be to crowded, so only the origin and destination markers will be enough. Also, lats and lngs for markers have to be taken from the Array (Don't know why, but the line is not visible if we use ArrayList).

positions = PolylineUtils.decode(attraction.getWaypoints(),5);
latLng = new LatLng[positions.size()];
for (int i = 0; i < positions.size(); i++) {
    latLng[i] = new LatLng(
            positions.get(i).latitude(),
            positions.get(i).longitude());
}


mapboxMap.addMarker(new MarkerOptions().setPosition(new LatLng(latLng[0].getLatitude(),latLng[0].getLongitude())));
mapboxMap.addMarker(new MarkerOptions().setPosition(new LatLng(latLng[latLng.length-1].getLatitude(),latLng[latLng.length-1].getLongitude())));


// Draw Points on MapView
mapboxMap.addPolyline(new PolylineOptions()
        .add(latLng)
        .color(Color.parseColor("#38afea"))
        .width(5));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM