Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

}

}//===================================================================================================
like image 906
user3211403 Avatar asked Jan 19 '14 04:01

user3211403


3 Answers

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.

like image 93
MKer Avatar answered Nov 15 '22 00:11

MKer


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

like image 26
khansa amrouni Avatar answered Nov 15 '22 01:11

khansa amrouni


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.
like image 30
Nehalkumar Avatar answered Nov 15 '22 00:11

Nehalkumar