The problem is here:
Straight routes useless. Is it possible to eliminate them? there are straight lines that i wouldn't want.
Input from rest_api it's a simple json in this link:
https://drive.google.com/file/d/1S2_xOB9Bj6EqiFwuRNGV5JvrjPjZoQD0/view?usp=sharing
Output: i want map with lines and marker and without ANY straight lines.
Code :
onMapReady(@NonNull GoogleMap map) {
googleMap = map;
googleMap.getUiSettings().setMapToolbarEnabled(false);
googleMap.getUiSettings().setZoomControlsEnabled(false);
routePoints = new ArrayList<>();
noRoutePoints = new ArrayList<>();
String idTappaSelezionata = MainActivity.idUltimaTappaSelezionata;
if (idTappaSelezionata != null) {
mapView.setVisibility(View.GONE);
StringRequest getPercorsoRequest = new StringRequest(Request.Method.POST, Config.GET_PERCORSO_URL,
response -> {
Logger.show("Mappa getData response: " + response + " | Url: " + Config.GET_PERCORSO_URL);
try {
JSONObject responseObject = new JSONObject(response);
JSONArray percorsoArray = responseObject.getJSONArray("percorso");
if (percorsoArray.length() == 0) {
clearRoutes();
} else {
parseAndDisplayRoute(percorsoArray);
}
mapView.setVisibility(View.VISIBLE);
} catch (JSONException e) {
// handle exception
}
},
error -> {
// handle error
}) {
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<>();
params.put("user_id", MainActivity.userLoggedId);
params.put("id_tappa", idTappaSelezionata);
params.put("id_via", MainActivity.idUltimaViaSelezionata);
return params;
}
};
SingletonVolley.getInstance(getActivity()).addToRequestQueue(getPercorsoRequest);
} else {
showNoRouteDialog();
}
}
private void clearRoutes() {
getActivity().runOnUiThread(() -> {
routePoints.clear();
noRoutePoints.clear();
});
}
private void parseAndDisplayRoute(JSONArray percorsoArray) throws JSONException {
for (int i = 0; i < percorsoArray.length(); i++) {
Gson gson = new Gson();
String tmpPercorso = percorsoArray.getJSONObject(i).toString();
Percorso newP = gson.fromJson(tmpPercorso, Percorso.class);
if ("rosso".equals(newP.getColore())) {
routePoints.add(new LatLng(newP.getLatitude(), newP.getLongitude()));
} else if ("blu".equals(newP.getColore())) {
noRoutePoints.add(new LatLng(newP.getLatitude(), newP.getLongitude()));
}
}
getActivity().runOnUiThread(() -> {
if (!routePoints.isEmpty()) {
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(routePoints.get(0), 14));
}
if (routePoints.size() > 1 && noRoutePoints.size() > 1) {
addPolylines();
}
addMarker();
setPolylineClickListener();
});
}
private void addPolylines() {
List<LatLng> percorso = PolyUtil.simplify(routePoints, 0.0001); // Adjusted tolerance
PolylineOptions percorsoPolyline = new PolylineOptions()
.addAll(percorso)
.clickable(true)
.width(10)
.color(Color.RED);
googleMap.addPolyline(percorsoPolyline);
// List<LatLng> simplifiedNoRoute = PolyUtil.simplify(noRoutePoints, 0.0001); // Adjusted tolerance
/*PolylineOptions polylineOptionsRosso = new PolylineOptions()
.addAll(simplifiedRoute)
.clickable(true)
.width(10)
.color(Color.RED);
googleMap.addPolyline(polylineOptionsRosso);
PolylineOptions polylineOptionsBlu = new PolylineOptions()
.addAll(simplifiedNoRoute)
.clickable(true)
.width(10)
.color(Color.BLUE);
googleMap.addPolyline(polylineOptionsBlu);*/
getExperiences(googleMap);
}
private void addMarker() {
LatLng markerMe = routePoints.get(0);
int customMarkerHeight = 100;
int customMarkerWidth = 100;
BitmapDrawable bitmapDrawable = (BitmapDrawable) getResources().getDrawable(R.drawable.me);
Bitmap customMarkerBitmap = Bitmap.createScaledBitmap(bitmapDrawable.getBitmap(), customMarkerWidth, customMarkerHeight, false);
BitmapDescriptor customMarkerMe = BitmapDescriptorFactory.fromBitmap(customMarkerBitmap);
MarkerOptions markerOp = new MarkerOptions()
.position(markerMe)
.icon(customMarkerMe)
.title("Marker " + getId());
googleMap.addMarker(markerOp);
}
private void setPolylineClickListener() {
googleMap.setOnPolylineClickListener(polyline -> {
List<LatLng> path = polyline.getPoints();
String navigationUri = "http://maps.google.com/maps?daddr=" + path.get(path.size() - 1).latitude + "," + path.get(path.size() - 1).longitude;
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(navigationUri));
intent.setPackage("com.google.android.apps.maps");
startActivity(intent);
});
}
private void showNoRouteDialog() {
mapView.setVisibility(View.GONE);
String message = "Non è stato selezionato nessun percorso. Premere OK per scegliere un percorso.";
androidx.appcompat.app.AlertDialog.Builder dialog = new androidx.appcompat.app.AlertDialog.Builder(getActivity());
dialog.setTitle("Le Vie di Francesco");
dialog.setMessage(message);
dialog.setPositiveButton("OK", (dialogInterface, i) -> {
((MainActivity) getActivity()).caricaFragment("menu_home", new Vie(), "Vie");
});
dialog.setCancelable(false);
dialog.show();
}
Why do you see those straight lines that have nothing to do with the route?
Thanks for any help.