Search code examples
androidroutespolylinegraphhoppermapsforge

Remove previous route in graphhopper android?


I am using graphhopper and mapsforge to show the route in my android app.The route is shown in my mapView from the polyline but when I change the location of second point the new route is shown above the previous route.So I need to delete this previous route when a new route is calculated. The code is as follows:

GraphHopper localGraphHopper = new GraphHopper().forMobile();
localGraphHopper.setCHShortcuts(true, true);
localGraphHopper.load(getFolderPath());

GHRequest localGHRequest = new GHRequest(paramDouble1, paramDouble2, paramDouble3, paramDouble4);
GHRequest a = localGHRequest.setAlgorithm("dijkstrabi");
GHResponse localGHResponse = localGraphHopper.route(localGHRequest);

int i = localGHResponse.getPoints().getSize();
PointList localPointList = localGHResponse.getPoints();
Polyline localPolyline = new Polyline(createPaint(AndroidGraphicFactory.INSTANCE.createColor(Color.RED), 4, Style.STROKE), AndroidGraphicFactory.INSTANCE);
this.latLongs_track = localPolyline.getLatLongs();

for (int j = 0;; j++)
{
    if (j >= i)
    {
        this.mapView.getLayerManager().getLayers().add(localPolyline);
        LatLong localLatLong = new LatLong((paramDouble1 + paramDouble3) / 2.0D, (paramDouble2 + paramDouble4) / 2.0D);
        this.mapView.getModel().mapViewPosition.setCenter(localLatLong);
        return;
    }
    this.latLongs_track.add(new LatLong(localPointList.getLatitude(j), localPointList.getLongitude(j)));
}

Solution

  • I have found the answer for my problem and am posting here

    Polyline localPolyline;
    public void calcPath( final double fromLat, final double fromLon,
            final double toLat, final double toLon )
    {
        log("calculating path ...");
        new AsyncTask<Void, Void, GHResponse>()
        {
            float time;
    
            protected GHResponse doInBackground( Void... v )
            {
                StopWatch sw = new StopWatch().start();
                GHRequest req = new GHRequest(fromLat, fromLon, toLat, toLon).
                        setAlgorithm(AlgorithmOptions.DIJKSTRA_BI);
    
                req.getHints()
                .put("instructions", "false");
                GHResponse resp = hopper.route(req);
                time = sw.stop().getSeconds();
                return resp;
            }
    
            protected void onPostExecute( GHResponse resp )
            {
                if (!resp.hasErrors())
                {
                    if(localPolyline!=null){
                        mapView.getLayerManager().getLayers().remove(localPolyline);
                    }
                    else{
                        log("here");
                    }
                    log("from:" + fromLat + "," + fromLon + " to:" + toLat + ","
                            + toLon + " found path with distance:" + resp.getDistance()
                            / 1000f + ", nodes:" + resp.getPoints().getSize() + ", time:"
                            + time + " " + resp.getDebugInfo());
                    logUser("the route is " + (int) (resp.getDistance() / 100) / 10f
                            + "km long, time:" + resp.getMillis() / 60000f + "min, debug:" + time);
                    localPolyline=createPolyline(resp);
                    mapView.getLayerManager().getLayers().add(localPolyline);
                } else
                {
                    logUser("Error:" + resp.getErrors());
                }
            }
        }.execute();
    }
    

    Thanks to everybody.