Android, Java + Goople Map: Straight routes useless. Is it possible to eliminate them?

Video of problem

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: map with markers 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();
    }

Where is my bad?

New contributor

user26528377 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật