I write solutions to the problems I can't find much about elsewhere on the Web, also some code/script snippets that are absolutely awesome and make my life easier. Will be glad if someone finds these posts interesting and helpful!

Friday, May 6, 2011

Cloudmade routing on OSMDroid

Another mystery without much relevant results on Google search that turned out to be quite an easy task.
Cloudmade seems to make a lot of effort to provide developers with a lot of mapping features, and as I haven't found anywhere on the site how to pay them, I take it that their service is free. It seems also that they use OpenStreetMaps as a back-end, therefore (here goes the disclaimer) ROUTING IS NOT VERY ACCURATE. At least not yet, but it's being improved a lot; I've added a Starbucks shop about a month ago, and now it's visible on the map!
Among other services Cloudmade offers Routing HTTP API, which seems to be a breeze for use within HTML code (they support JSONP-style callback for script injection to go around cross-origin resource sharing restrictions). This is the one to be used in the open map view with open map controller, because Google Maps doesn't allow routing anywhere outside their domain (Google even removed routing API from Android right after the very first release!).
My implementation of the BlueLine does everything from requesting directions, all the way to drawing them on the map.
Create a class extending PathOverlay with the following code:

The Cloudmade API key goes into the manifest file within the application element like here

And that's pretty much it. It's very easy to make it work now with the application:

That's really it! Hope it helps.


  1. Hi Elijah, I use your code in my android application. There is no error but I can not see route. I do not understand what the problem is. On the other hand start point is GPS location point, it is Ok, but I do not understand how do we determine the destination points. Please say what are my mistakes. Thank you for your atttention.

  2. To determine a destination point from an address you can use Geocoding (http://code.google.com/apis/maps/documentation/geocoding/). That is, if that's your problem. Other than that, just for the sake of an example, drop a random coordinates pair into this method.
    To be honest I don't really see what is the exact issue you are running into. Would you like me to take a look at your code?

  3. Hi Elijah, I created a class (BlueLine Class) with your code and then I try to use this class in my application code. My code is running but the route application could not start. I get start point coordinates by GPS, I used random coordinates pair for destination point for testing but the route application could not start too. I would like you to examine my code. But I need your mail adress because I can not send my code in this platform because of limitation. My email adress is hzselvi@yahoo.com. Thank you for your attention.

  4. Just bear in mind that start and end coordinates have to have the same format (if I remember correctly, it has to be in the same format as the one used by the GPS receiver, e.g. [37.9, -122.5] for San Francisco). Just watch out to avoid converting them to/from microdegrees, as the Android platform uses latter.
    As a test, create a link to the routing service with required coordinates and insert it into the browser to see what the service returns.

  5. For testing, I create a link and insert it into the browser and service returns gpx file to me. But I can not succeed in android application. There is no error, application runs well but route line can not be shown. I think I have a defect but I do not know what it is.
    And I can not understand this sentence. Which thread?
    "the routing will be requested from another thread, need to make coordinates accessible from there".
    Thank you for your interest.

    I can not understand this sentence.

  6. this example is missing the Draw method, right?

  7. I have tried to implement your codes into my program sir Elijah. But i encountered a lot of issues during execution example, force close. I posted a question on stackoverflow, please check it out: http://stackoverflow.com/questions/8728737/shortest-path-calculation-on-maps-using-osmdroid-library

    Sir please help me this. here's my email: lordzden_1991@yahoo.com