0

我正在使用graphhopper和mapsforge在我的android应用程序中显示路线。路线从折线显示在我的mapView中,但是当我更改第二点的位置时,新路线显示在前一条路线上方。所以我需要删除这个计算新路线时的先前路线。代码如下:

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)));
}
4

2 回答 2

0

我找到了我的问题的答案,并在这里发布

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();
}

谢谢大家。

于 2014-12-16T07:08:09.283 回答
0

使用这个功能:

 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()){
                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);
                mapView.getLayerManager().getLayers().add(createPolyline(resp));
                //mapView.redraw();
            } else{
                logUser("Error:" + resp.getErrors());
            }
            shortestPathRunning = false;
        }
    }.execute();
}

您可以在https://github.com/graphhopper/graphhopper找到所有来源

于 2014-12-11T07:44:08.530 回答