Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
189 views
in Technique[技术] by (71.8m points)

android - RoadManager for osmdroid error

I am following a tutorial here https://code.google.com/p/osmbonuspack/wiki/Tutorial_1 but I have encountered an error that it doesn't show the correct route correctly. It just shows a straight line from Point A to Point B.

What I want to achieve is to show the correct route from these points. I'm guessing the error is that it doesn't recognize any nodes to go through.

A similar question has been also asked and I am assuming I have the same problem if I haven't explained my question well.

Similar question can be found here: OSMDroid Routing problems when following a tutorial

Here is a part of my code using RoadManager

Here is a part of the code.

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], "UST", 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
        }           
    }

}

}//===================================================================================================
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Let's try to provide a complete answer to this quite frequent question.

Basically, when you get the "straight line", it means that the RoadManager got an error.

So, first of all, in your code, you should check the result of getRoad, this way:

if (road.mStatus != Road.STATUS_OK){
  //handle error... warn the user, etc. 
}

Now, where this error is coming from? => You must search in the logcat. You should find the full url that has been sent, and probably a stacktrace about the error.

I strongly recommend that you copy/paste this full url in a browser , and check the result.

Here are the typical errors, by decreasing probability:

1) You didnt' read carefully the "Important note" at the beginning of the Tutorial_0, and you are trying to do a Network call in the main thread, with an SDK >= 3.0. => Read this "Important note".

2) You asked for a route that is not possible (really not possible, or because of weird positions, or because of setting unsupported options). => This is easy to check by copy/pasting the full url in a web browser, and looking at the answer.

3) Your device has no network connectivity.

4) The routing service changed its API (this happened, more than once...). => Could be checked by copy/pasting the full url in a browser. In this case, raise an Issue in OSMBonusPack project, so that we can take it into account ASAP.

5) The routing service is down. => Easy to check by copy/pasting the full url in a browser.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...