OpenRouteService for Android Studio

Hi,
I’m new to OpenRouteService and this question may seem stupid for some people. I’m struggling to use OpenRouteService on and Android Studio app, the main issue for me is the lack of info/guidance to get to work on it. Everywhere I look there’s a ton of info about how to install stuff, but none of it says if it is for Android Studio (maybe it’s my lack of knowledge). If anybody knows how to get this to work on Android Studio painlessly please let me know, I tried to search for a Gradle type installation since it is the easy way and didn’t find anything.

The only function I need from ORS is to ask for a route between two coordinates and get the directions and stuff. Im working on a OSM based app. I’m really really new to this stuff, be kind please.

Thanks.

Hi,

we don’t offer Android compatible packages.

However, doesn’t sound like you really need that. If you can live without offline navigation (i.e. your app would have to be connected to the internet to be able to calculate directions), then just call our APIs from within your app:

https://openrouteservice.org/dev/#/api-docs/v2/directions/{profile}/geojson/post

Note, if you use our services, you’ll have to live with a rate limit of 2500/day. In case you set up your own ORS instance on a server (affordable if the area you need is not too big, e.g. 1-2 countries), you can request without limits:

Not sure, if GraphHopper’s Android app offers offline navigation by now… Likely only with pre-built and distributed graphs (not like Google where you can run offline navigation dynamically for small areas):

AsyncTask.execute(new Runnable(){
@Override
public void run(){
try {    

URL url = new URL(<API_LINK_HERE_AS_ON_DASHBOARD>);
                                // Create connection
                                HttpsURLConnection myConnection = (HttpsURLConnection) url.openConnection();

                                myConnection.setRequestMethod("POST");
                                myConnection.setRequestProperty("Authorization", <YOUR_API_KEY_HERE>);
                                myConnection.setRequestProperty("Accept", <CHECK_DASHBOARD_OF SPECIFIC_API_FOR_THIS_HEADER>);
                                myConnection.setRequestProperty("Content-Type",  <CHECK_DASHBOARD_OF SPECIFIC_API_FOR_THIS_HEADER>);
          myConnection.setDoOutput(true);
                                String jsonInputString = "{\"locations\":[[9.70093,48.477473],[9.207916,49.153868]]}";//EXAMPLE INPUT FOR API (CHANGE IT AS PER YOUR API (ALSO NOTE THE QUOTES ARE AFTER '\' ))
                                try(OutputStream os = myConnection.getOutputStream()) {
                                    byte[] input = jsonInputString.getBytes("utf-8");
                                    os.write(input, 0, input.length);
if(myConnection.getResponseCode()==200){

try(BufferedReader br = new BufferedReader(new InputStreamReader(myConnection.getInputStream(), "utf-8"))) {
                                            final StringBuilder response = new StringBuilder();
                                            String responseLine = null;
                                            while ((responseLine = br.readLine()) != null) {
                                                response.append(responseLine.trim());
                                            }
                                            System.out.println(response.toString());
                                            runOnUiThread(new Runnable() {
                                                @Override
                                                public void run() {
                                                    Toast.makeText(appointment.this, response.toString(), Toast.LENGTH_LONG).show();
                                                }
                                            });
                                        }

}


}else{

runOnUiThread(new Runnable(){
@Override
public void run() {
Toast.makeText(appointment.this,&#32;"SOME&#32;ERROR&#32;OCCURED",&#32;Toast.LENGTH_SHORT).show();
}
});

}

}
}catch(final Exception e){

runOnUiThread(new Runnable(){
@Override
public void run(){
Toast.makeText(appointment.this,e.toString(),Toast.LENGTH_LONG).show();
//FURTHER RENDER THE DATA FROM HERE ONWARDS
}
});

}
}
});Preformatted text

@anurag-176

Is that the snippet to call ORS from Android?

Hello. I am trying to make an android app that includes routing, and i want to use ORS for the navigation part. Could you provide me with information on how this can be possible? Do i also need a separate map API, to display the map?

Hey,

you can apply for an API key here. With this, you can either use one of our SDKs for python, JavaScript or R or write queries yourself - have a look at the example code in the playground that double as the API Documentation.

The ors API will only return the requested routes or isochrones, and not the underlying map, so you’ll need seperate logic to display that.

Best regards

Hello and thanks for the answer,

Do you have any resources on how i could display the route on a map, for ex OpenStreetMaps? As i couldn’t find something useful yet.

Thanks again,
Jerry

Probably something like this:
https://osmdroid.github.io/osmdroid/Map-Sources.html

1 Like

ORS returns either a GeoJSON, GPX or encoded polyline which in contrast to google’s encoded polyline also contains elevation data by default.

So when you are working with encoded polylines be aware of the fact you might have to parse them differently

Take a look at the osmdroid docs on how to add polygons. If you use the ORS geojson response you can probably just extract the coordinates and put them in the array list of that example (be aware of lat, long order needed).

1 Like

Hello,

In the API dashboard, if i get the code in Java, and call the API in my app as is, what exactly will it return? Also, the part:
<dependency> <groupId>org.glassfish.jersey.core</groupId> <artifactId>jersey-client</artifactId> <version>2.8</version> </dependency> <dependency> <groupId>org.glassfish.jersey.media</groupId> <artifactId>jersey-media-json-jackson</artifactId> <version>2.8</version> </dependency>

what purpose serves and at what file should it be added? (There is no pom.xml file in Android App Studio). Is this logic functional for an android app?

Thanks in advance

Hi @JerryPapada,

the code snippet from the playground is for basic Maven projects where you add the dependencies to the pom.xml file.
Not sure how to do that in Android studio. Maybe look for how to do API requests in an Android Studio App.

What it returns? If you get the request to work, you will also get a valid response which you can inspect during debugging or print to output. But it will be a response object containing the same response as you get from the API playground.

Best regards