我正在使用graphhopper和mapsforge在我的Android应用程序中显示路线。该路线显示在折线的地图视图中,但是当我更改第二个点的位置时,新路线显示在上一条路线的上方。因此,在计算新路线时,我需要删除以前的路线。代码如下:
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)));
}
使用此函数:
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 找到的所有来源
我已经找到了问题的答案,并在这里发帖
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();
}
谢谢大家。