I'm working on Android development using Kotlin, and encountering an issue with a lateinit var named polyline. The error I'm getting suggests that even though I assigned a value to polyline inside a function called from the onCreate, the app is still crashing with the message that polyline has not been initialized.
Pls how do I go about this? Is there something I'm missing?
See the code below. I removed all unneccessary codes for this question
class CustomerMapActivity : AppCompatActivity(), OnMapReadyCallback {
private lateinit var polyline: String
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityCustomerMapBinding.inflate(layoutInflater)
setContentView(binding.root)
val pickUpLocation = "Hall 2 Car Park, Benin City, Nigeria"
val dropOffLocation = "Nadia Bakery, Benin City, Nigeria"
val apiKey = R.string.google_api_key.toString()
routesDetails(pickUpLocation, dropOffLocation, apiKey)
}
override fun onMapReady(mMap: GoogleMap) {
drawRouteOnMap()
}
private fun routesDetails(origin: String, destination: String, apiKey: String){
val routeService = ServiceBuilder.buildService(RouteService::class.java)
val requestCall = routeService.getRoutes(origin, destination, apiKey)
requestCall.enqueue(object: Callback<RoutesResponse> {
override fun onResponse(call: Call<RoutesResponse>, response: Response<RoutesResponse>) {
if (response.isSuccessful) {
if (routeResponse != null && routeResponse.routes.isNotEmpty()){
polyline = response.body()?.routes?.get(0)?.overview_polyline?.points.toString()
}else{
}
}
}
override fun onFailure(call: Call<RoutesResponse>, t: Throwable) {
}
})
}
private fun drawRouteOnMap() {
val decodedPolyline = PolyUtil.decode(polyline)
}