Skip to content

Instantly share code, notes, and snippets.

@WLun001
Created May 20, 2018 05:51
Show Gist options
  • Save WLun001/5a04cf6a898c32e732e0804555e0e83a to your computer and use it in GitHub Desktop.
Save WLun001/5a04cf6a898c32e732e0804555e0e83a to your computer and use it in GitHub Desktop.
GET request with custom query text on Android Kotlin Anko
// example with Google Maps
// https://developers.google.com/maps/documentation/distance-matrix/intro
// format : origins=place_id:ChIJ3S-JXmauEmsRUcIaWtf4MzE
companion object {
const val googleApiKey = "123"
const val distanceURL = "https://maps.googleapis.com/maps/api/distancematrix/json?"
}
doAsync {
val placeID = "123"
val location = Uri.encode("3.041803,101.793075")
val uriBuilder = Uri.parse(distanceURL)
.buildUpon()
.encodedQuery("""origins=$location&destinations=place_id:${placeId}&key=$googleApiKey
""".trimIndent())
Log.d("URL", uriBuilder.toString())
val result = URL(uriBuilder.toString()).readText()
val distance = JSONObject(result).getJSONArray("rows")
.getJSONObject(0).getJSONArray("elements")
.getJSONObject(0).getJSONObject("distance")
.getString("text")
Log.d("Distance", distance.toString())
onComplete {
progress.incrementProgressBy(1)
uiThread {
if (progress.isShowing && progress.progress == progress.max) {
progress.dismiss()
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment