Skip to content

Instantly share code, notes, and snippets.

@shivaluma
Created December 9, 2019 13:51
Show Gist options
  • Save shivaluma/f31821c678f128a00d63f0cec81dd976 to your computer and use it in GitHub Desktop.
Save shivaluma/f31821c678f128a00d63f0cec81dd976 to your computer and use it in GitHub Desktop.
fun drawPath(src: LatLng, dest: LatLng) {
addMarker(mGoogleMap,src,"My location",R.drawable.ic_startpoint)
addMarker(mGoogleMap,dest,"Destination",R.drawable.ic_endpoint)
doAsync {
val path: MutableList<List<LatLng>> = ArrayList()
val urlDirections = "https://maps.googleapis.com/maps/api/directions/json?origin=${src.latitude},${src.longitude}&destination=${dest.latitude},${dest.longitude}&key=${Constant.ggMapApiKey}"
val directionsRequest = object : StringRequest(Request.Method.GET, urlDirections, Response.Listener<String> {
response ->
val jsonResponse = JSONObject(response)
// Get routes
val routes = jsonResponse.getJSONArray("routes")
val legs = routes.getJSONObject(0).getJSONArray("legs")
val steps = legs.getJSONObject(0).getJSONArray("steps")
for (i in 0 until steps.length()) {
val points = steps.getJSONObject(i).getJSONObject("polyline").getString("points")
path.add(PolyUtil.decode(points))
}
for (i in 0 until path.size) {
mGoogleMap.addPolyline(PolylineOptions().addAll(path[i]).color(Color.BLUE).width(5.0f))
}
}, Response.ErrorListener {
_ ->
}){}
val requestQueue = Volley.newRequestQueue(this)
requestQueue.add(directionsRequest)
}.execute()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment