'Why is the polyline not drawn on the map

I am trying to draw a route between two coordinates. The project consists of several fragments, when the fragment with the map is loaded, the route should be drawn immediately by coordinates.

fun setLatLng(origin: LatLng, destination: LatLng){
    Log.d(TAG, "setLatLng: получение From: $origin, To: $destination")
    this.origin = origin
    this.destination = destination

    val url: String = getDirectionsUrl(origin, destination)!!
    val downloadTask = DownloadTask()
    downloadTask.execute(url)
}

@Throws(IOException::class)
private fun downloadUrl(strUrl: String): String?{
    var data = ""
    var iStream: InputStream? = null
    var urlConnection: HttpURLConnection? = null
    try {
        val url = URL(strUrl)
        urlConnection = url.openConnection() as HttpURLConnection
        urlConnection.connect()
        iStream = urlConnection!!.inputStream
        val br = BufferedReader(InputStreamReader(iStream))
        val sb = StringBuffer()
        var line: String? = ""
        while (br.readLine().also { line = it } != null){
            sb.append(line)
        }
        data = sb.toString()
        br.close()
    }catch (e: java.lang.Exception){
        Log.d(TAG, "downloadUrl: ${e.toString()}")
    }finally {
        iStream!!.close()
        urlConnection!!.disconnect()
    }
    return data
}

private fun getDirectionsUrl(origin: LatLng, dest: LatLng): String? {

    val str_origin =
        "origin=" + origin.latitude.toString() + "," + origin.longitude
    val str_dest =
        "destination=" + dest.latitude.toString() + "," + dest.longitude
    val key = "key=$apiKey"
    val parameters = "$str_origin&$str_dest&$key"
    val output = "json"
    return "https://maps.googleapis.com/maps/api/directions/$output?$parameters"
}
inner class DownloadTask: AsyncTask<String?, Void?, String?>(){

override fun onPostExecute(result: String?) {
    super.onPostExecute(result)
    val parserTask = ParserTask()
    parserTask.execute(result)
}

override fun doInBackground(vararg url: String?): String? {
    var data = ""
    try{
        data = downloadUrl(url[0].toString()).toString()
    }catch (e: Exception){}
    return data
}

}

inner class ParserTask: AsyncTask<String?, Int?, List<List<HashMap<String, String>>>?>(){
override fun doInBackground(vararg jsonData: String?): List<List<HashMap<String, String>>>? {
    val jObject: JSONObject
    var routes: List<List<HashMap<String, String>>>? = null
    try{
        jObject = JSONObject(jsonData[0])
        val parser = DataParser()
        routes = parser.parse(jObject)
    }catch (e: java.lang.Exception){
        e.printStackTrace()
    }
    return routes
}

override fun onPostExecute(result: List<List<HashMap<String, String>>>?) {
    val points = ArrayList<LatLng?>()
    val lineOptions = PolylineOptions()
    for(i in result!!.indices){
        val path = result[i]
        for (j in path.indices){
            val point = path[j]
            val lat = point["lat"]!!.toDouble()
            val lng = point["lng"]!!.toDouble()
            val position = LatLng(lat, lng)
            points.add(position)
        }
        lineOptions.addAll(points)
        lineOptions.width(8f)
        lineOptions.color(Color.RED)
        lineOptions.geodesic(true)
    }
    if (points.size != 0) mMap.addPolyline(lineOptions)
    builder()
}

} The onPostExecute function should draw the route. If you put a dot on the function in debug mode, the application will not stop, but, judging by the fact that the builder method is triggered at the end of the function, the function itself is triggered.



Solution 1:[1]

The problem was in the api key. Decided to replace it.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 lian. lun