Skip to content

Pygmalion69/ors-android-client

Repository files navigation

ORS Android Client

JitPack Release

Android client library for the OpenRouteService APIs.

Dependency

The library is published on GitHub Packages:

repositories {
    maven {
        url = uri("https://maven.pkg.github.com/Pygmalion69/ors-android-client")
        credentials {
            username = System.getenv("GPR_USER")    // GitHub username
            password = System.getenv("GPR_TOKEN")   // Personal access token
        }
    }
}

dependencies {
    implementation("org.nitri.ors:ors-android-client:<latest-version>")
}

If you prefer, the library is also available via JitPack:

repositories {
    maven { url = uri("https://jitpack.io") }
}

dependencies {
    implementation("com.github.Pygmalion69:ors-android-client:<latest-version>")
}

Creating a client

val ors = Ors.create("<your_api_key>", context)

Routing with domain objects

val request = RouteRequest(
    coordinates = listOf(
        listOf(8.681495, 49.41461),
        listOf(8.687872, 49.420318)
    )
)

val route: RouteResponse = ors.getRoute(Profile.DRIVING_CAR, request)

Routing with the Kotlin DSL

val route = ors.getRoute(
    Profile.DRIVING_CAR,
    routeRequest {
        start(8.681495, 49.41461)
        end(8.687872, 49.420318)
    }
)

Routing with helper classes

val helper = RouteHelper()
val route = with(helper) {
    ors.getRoute(
        start = 8.681495 to 49.41461,
        end = 8.687872 to 49.420318,
        profile = "driving-car"
    )
}

API Reference

Complete and concise technical documentation: API Reference.

Example

Example code to obtain a GPX using a helper class:

class Directions(val client: OrsClient, private val profile: String) {

    val routeHelper = RouteHelper()

    fun getRouteGpx(coordinates: List<List<Double>>, language: String, result: RouteGpxResult) {
        CoroutineScope(Dispatchers.IO).launch {
            try {
                val gpxXml = with(routeHelper) { client.getRouteGpx(coordinates, language, profile) }
                withContext(Dispatchers.Main) {
                    if (gpxXml.isNotBlank()) {
                        result.onSuccess(gpxXml)
                    } else {
                        result.onError("Empty response body")
                    }
                }
            } catch (e: Exception) {
                withContext(Dispatchers.Main) {
                    result.onError("Failed to fetch GPX: ${e.message}")
                }
            }
        }
    }

    interface RouteGpxResult {
        fun onSuccess(gpx: String)
        fun onError(message: String)
    }
}

Apps

Apps using this library:

About

Openrouteservice Android Client

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Languages