RoadManager for osmdroid error
Asked Answered
R

4

7

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
        }           
    }

}

}//===================================================================================================
Rayraya answered 19/1, 2014 at 4:29 Comment(1)
@Rayraya did you solve the issue? I have a same issue.Lynelllynelle
F
5

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.

Fluvial answered 19/2, 2014 at 22:40 Comment(3)
3) Your device has no network connectivity. hi @Fluvial can we draw rout offline?Inwrap
All OSMBonusPack RoadManagers are online services. For offline routing, you can look at GraphHopper for Android.Fluvial
@Fluvial I am getting the following error: Value <html> of type java.lang.String cannot be converted to JSONObject - do you know what might be wrong? I am running the request in an asynch task and I have pasted the link from the logcat in the browser. It seemed good to me. Also, the result is not always a straight line. Sometimes it works, but it seems random to me.Ostia
B
4

I think it is better to use AsyncTasks in this case:

/**
     * 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);
                                                    }
    }

then call it new UpdateRoadTask().execute(waypoints);

Brunabrunch answered 22/4, 2015 at 13:33 Comment(0)
C
3
new Thread(new Runnable()
{
    public void run() 
    {
        RoadManager roadManager = new OSRMRoadManager();
        ArrayList<GeoPoint> waypoints = new ArrayList<GeoPoint>();
        GeoPoint startPoint = new GeoPoint(source_lat, source_longi);               
        waypoints.add(startPoint);
        GeoPoint endPoint = new GeoPoint(desti_lat,desti_longi);
        waypoints.add(endPoint);                    
        try 
        {
            road = roadManager.getRoad(waypoints);
        } 
        catch (Exception e)
        {
            e.printStackTrace();
        }

        runOnUiThread(new Runnable() 
        {
            public void run() 
            {
                if (road.mStatus != Road.STATUS_OK)
                {
                      //handle error... warn the user, etc. 
                }

                Polyline roadOverlay = RoadManager.buildRoadOverlay(road, Color.RED, 8, context);
                map.getOverlays().add(roadOverlay);                 
            }
        });
    }
}).start(); 

And i am use two jar files 1)slf4j-android-1.5.8.jar and 2)osmdroid-android-4.2.jar and osmbonuspack library.
Condenser answered 18/3, 2015 at 6:19 Comment(0)
E
1

A strange error I found regarding this is as follows: Firstly I mention following line of code for taking directions for the vehicle "BIKE"

((OSRMRoadManager) roadManager).setMean(OSRMRoadManager.MEAN_BY_BIKE);

Now when it was first called it follows the following URL: https://routing.openstreetmap.de/routed-car/route/v1/driving/68.8889678000,23.2151582000;73.1808008000,22.3110728000?alternatives=false&overview=full&steps=true

Now when calling the second time{same MEAN_BY_BIKE}, it is following this URL: :https://routing.openstreetmap.de/routed-bike/route/v1/driving/68.8889678000,23.2151582000;73.1808008000,22.3110728000?alternatives=false&overview=full&steps=true

So the issue is that no response is for the "routed-bike" and it is calling automatically itself when called for second time.

So as a solution I changed my code to the following:

((OSRMRoadManager) roadManager).setMean(OSRMRoadManager.MEAN_BY_CAR);

You can check your LogCat for the same.

Emmie answered 30/7, 2021 at 14:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.