我在这里遵循
https://code.google.com/p/osmbonuspack/wiki/Tutorial_1教程,但我遇到一个错误,它没有正确显示正确的路由.它只显示从A点到B点的直线.
我想要实现的是从这些点显示正确的路线.我猜错误是它无法识别任何节点.
一个类似的问题也被问到,如果我没有很好地解释我的问题,我假设我有同样的问题.
类似的问题可以在这里找到:OSMDroid Routing problems when following a tutorial
这是使用RoadManager的代码的一部分
这是代码的一部分.
try { //get current longlat gpsLocator.getLocation(); cur_loc_lat =gpsLocator.getLatitude(); cur_loc_long =gpsLocator.getLongitude(); } catch (Exception e) { // TODO: handle exception } //--- Create Another Overlay for multi marker anotherOverlayItemArray = new ArrayList<OverlayItem>(); anotherOverlayItemArray.add(new OverlayItem( "UST","UST",new GeoPoint( testlat,testlong))); //--- Create Another Overlay for multi marker anotherOverlayItemArray.add(new OverlayItem( locDefine[0],new GeoPoint( sel_latitude,sel_longitude))); ItemizedIconOverlay<OverlayItem> anotherItemizedIconOverlay = new ItemizedIconOverlay<OverlayItem>( TomWalks.this,anotherOverlayItemArray,myOnItemGestureListener); myOpenMapView.getOverlays().add(anotherItemizedIconOverlay); //--- //Add Scale Bar ScaleBarOverlay myScaleBarOverlay = new ScaleBarOverlay(TomWalks.this); myOpenMapView.getOverlays().add(myScaleBarOverlay); try { //1 Routing via road manager RoadManager roadManager = new MapQuestRoadManager(); roadManager.addRequestOption("routeType=pedestrian"); /* roadManager.addRequestOption("units=m"); roadManager.addRequestOption("narrativeType=text"); roadManager.addRequestOption("shapeFormat=raw"); roadManager.addRequestOption("direction=0"); */ //Then,retrieve the road between your start and end point: ArrayList<GeoPoint> waypoints = new ArrayList<GeoPoint>(); waypoints.add(new GeoPoint(testlat,testlong)); waypoints.add(new GeoPoint(sel_latitude,sel_longitude)); //end point Road road = roadManager.getRoad(waypoints); // then,build an overlay with the route shape: PathOverlay roadOverlay = RoadManager.buildRoadOverlay(road,myOpenMapView.getContext()); roadOverlay.setColor(Color.GREEN); //Add Route Overlays into map myOpenMapView.getOverlays().add(roadOverlay); myOpenMapView.invalidate();//refesh map final ArrayList<ExtendedOverlayItem> roadItems = new ArrayList<ExtendedOverlayItem>(); ItemizedOverlayWithBubble<ExtendedOverlayItem> roadNodes = new ItemizedOverlayWithBubble<ExtendedOverlayItem>(TomWalks.this,roadItems,myOpenMapView); myOpenMapView.getOverlays().add(roadNodes); myOpenMapView.invalidate();//refesh map int nodesize=road.mNodes.size(); double length = road.mLength; Drawable marker = getResources().getDrawable(R.drawable.marker_node); Toast.makeText(TomWalks.this," Distance : " + length + " Nodes : "+nodesize,Toast.LENGTH_SHORT).show(); for (int i=0; i<road.mNodes.size(); i++) { RoadNode node = road.mNodes.get(i); ExtendedOverlayItem nodeMarker = new ExtendedOverlayItem("Step "+i,"",node.mLocation,TomWalks.this); nodeMarker.setMarkerHotspot(OverlayItem.HotspotPlace.CENTER); nodeMarker.setMarker(marker); roadNodes.addItem(nodeMarker); nodeMarker.setDescription(node.mInstructions); nodeMarker.setSubDescription(road.getLengthDurationText(node.mLength,node.mDuration)); Drawable icon = getResources().getDrawable(R.drawable.marker_node); nodeMarker.setImage(icon); }//end for myOpenMapView.getOverlays().add(roadNodes); myOpenMapView.invalidate();//refesh map } catch (Exception e) { // TODO: handle exception Toast.makeText(TomWalks.this,e.getMessage(),Toast.LENGTH_SHORT).show(); } myMapController.setCenter(new GeoPoint( sel_latitude,sel_longitude)); } catch (Exception e) { // TODO: handle exception } } } }//===================================================================================================
解决方法
我认为在这种情况下使用AsyncTasks会更好:
/** * Async task to get the road in a separate thread. */ private class UpdateRoadTask extends AsyncTask<Object,Void,Road> { protected Road doInBackground(Object... params) { @SuppressWarnings("unchecked") ArrayList<GeoPoint> waypoints = (ArrayList<GeoPoint>)params[0]; RoadManager roadManager = new OSRMRoadManager(); return roadManager.getRoad(waypoints); } @Override protected void onPostExecute(Road result) { road = result; // showing distance and duration of the road Toast.makeText(getActivity(),"distance="+road.mLength,Toast.LENGTH_LONG).show(); Toast.makeText(getActivity(),"durée="+road.mDuration,Toast.LENGTH_LONG).show(); if(road.mStatus != Road.STATUS_OK) Toast.makeText(getActivity(),"Error when loading the road - status="+road.mStatus,Toast.LENGTH_SHORT).show(); Polyline roadOverlay = RoadManager.buildRoadOverlay(road,getActivity()); map.getOverlays().add(roadOverlay); map.invalidate(); //updateUIWithRoad(result); } }
然后将其称为new UpdateRoadTask().execute(waypoints);