Advertisement
Uranzev

Route Parser

Apr 2nd, 2021
298
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 3.12 KB | None | 0 0
  1. object PolygonParser {
  2.  
  3.     fun getPolygon(hashMap:List<HashMap<String, String>>, isLine:Boolean = false):PolylineOptions{
  4.         val gapLen = 10
  5.         val dot = Dot()
  6.         val gap = Gap(gapLen.toFloat())
  7.         val dottedPattern = Arrays.asList(dot, gap)
  8.  
  9.         val points = ArrayList<LatLng>()
  10.         val lineOptions = PolylineOptions()
  11.  
  12.         for (j in hashMap.indices) {
  13.             val point = hashMap[j]
  14.  
  15.             val lat = point["lat"]!!.toDouble()
  16.             val lng = point["lng"]!!.toDouble()
  17.             val position = LatLng(lat, lng)
  18.  
  19.             points.add(position)
  20.         }
  21.  
  22.         lineOptions.addAll(points)
  23.         lineOptions.width(12f)
  24.         if(!isLine)
  25.             lineOptions.pattern(dottedPattern)
  26.         lineOptions.color(CommonUtil.getColor(R.color.colorNewRed))
  27.         lineOptions.jointType(JointType.ROUND)
  28.         lineOptions.geodesic(true)
  29.         return lineOptions
  30.     }
  31.  
  32.     fun parse(response: RouteResponse): List<List<HashMap<String, String>>> {
  33.         val routes = ArrayList<List<HashMap<String, String>>>()
  34.  
  35.         try {
  36.             val jRoutes = response.routes
  37.  
  38.             jRoutes.map { route ->
  39.                 val jLegs = route.legs
  40.                 val path = ArrayList<HashMap<String, String>>()
  41.                 jLegs.map { leg ->
  42.                     val jSteps = leg.steps
  43.                     jSteps.map { step ->
  44.                         val polyline = step.polyline.points
  45.                         val list = decodePoly(polyline)
  46.                         list.map { poly ->
  47.                             val hm = HashMap<String, String>()
  48.                             hm["lat"] = (poly as LatLng).latitude.toString()
  49.                             hm["lng"] = poly.longitude.toString()
  50.                             path.add(hm)
  51.                         }
  52.                     }
  53.                     routes.add(path)
  54.                 }
  55.             }
  56.         } catch (e: Exception) {
  57.             e.printStackTrace()
  58.         }
  59.         return routes
  60.     }
  61.  
  62.     private fun decodePoly(encoded: String): List<*> {
  63.  
  64.         val poly = ArrayList<LatLng>()
  65.         var index = 0
  66.         val len = encoded.length
  67.         var lat = 0
  68.         var lng = 0
  69.  
  70.         while (index < len) {
  71.             var b: Int
  72.             var shift = 0
  73.             var result = 0
  74.             do {
  75.                 b = encoded[index++].toInt() - 63
  76.                 result = result or (b and 0x1f shl shift)
  77.                 shift += 5
  78.             } while (b >= 0x20)
  79.             val dlat = if (result and 1 != 0) (result shr 1).inv() else result shr 1
  80.             lat += dlat
  81.  
  82.             shift = 0
  83.             result = 0
  84.             do {
  85.                 b = encoded[index++].toInt() - 63
  86.                 result = result or (b and 0x1f shl shift)
  87.                 shift += 5
  88.             } while (b >= 0x20)
  89.             val dlng = if (result and 1 != 0) (result shr 1).inv() else result shr 1
  90.             lng += dlng
  91.  
  92.             val p = LatLng(lat.toDouble() / 1E5,
  93.                     lng.toDouble() / 1E5)
  94.             poly.add(p)
  95.         }
  96.  
  97.         return poly
  98.     }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement