【问题标题】:Remove previous route in graphhopper android?删除graphhopper android中的先前路线?
【发布时间】:2015-02-09 14:54:08
【问题描述】:

我正在使用 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)));
}

【问题讨论】:

  • 你有什么问题?
  • 创建新路线时需要删除之前的路线

标签: android routing polyline graphhopper mapsforge


【解决方案1】:

使用此功能:

 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找到所有来源

【讨论】:

    【解决方案2】:

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

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

    谢谢大家。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-07-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-19
      相关资源
      最近更新 更多