Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- object PolygonParser {
- fun getPolygon(hashMap:List<HashMap<String, String>>, isLine:Boolean = false):PolylineOptions{
- val gapLen = 10
- val dot = Dot()
- val gap = Gap(gapLen.toFloat())
- val dottedPattern = Arrays.asList(dot, gap)
- val points = ArrayList<LatLng>()
- val lineOptions = PolylineOptions()
- for (j in hashMap.indices) {
- val point = hashMap[j]
- val lat = point["lat"]!!.toDouble()
- val lng = point["lng"]!!.toDouble()
- val position = LatLng(lat, lng)
- points.add(position)
- }
- lineOptions.addAll(points)
- lineOptions.width(12f)
- if(!isLine)
- lineOptions.pattern(dottedPattern)
- lineOptions.color(CommonUtil.getColor(R.color.colorNewRed))
- lineOptions.jointType(JointType.ROUND)
- lineOptions.geodesic(true)
- return lineOptions
- }
- fun parse(response: RouteResponse): List<List<HashMap<String, String>>> {
- val routes = ArrayList<List<HashMap<String, String>>>()
- try {
- val jRoutes = response.routes
- jRoutes.map { route ->
- val jLegs = route.legs
- val path = ArrayList<HashMap<String, String>>()
- jLegs.map { leg ->
- val jSteps = leg.steps
- jSteps.map { step ->
- val polyline = step.polyline.points
- val list = decodePoly(polyline)
- list.map { poly ->
- val hm = HashMap<String, String>()
- hm["lat"] = (poly as LatLng).latitude.toString()
- hm["lng"] = poly.longitude.toString()
- path.add(hm)
- }
- }
- routes.add(path)
- }
- }
- } catch (e: Exception) {
- e.printStackTrace()
- }
- return routes
- }
- private fun decodePoly(encoded: String): List<*> {
- val poly = ArrayList<LatLng>()
- var index = 0
- val len = encoded.length
- var lat = 0
- var lng = 0
- while (index < len) {
- var b: Int
- var shift = 0
- var result = 0
- do {
- b = encoded[index++].toInt() - 63
- result = result or (b and 0x1f shl shift)
- shift += 5
- } while (b >= 0x20)
- val dlat = if (result and 1 != 0) (result shr 1).inv() else result shr 1
- lat += dlat
- shift = 0
- result = 0
- do {
- b = encoded[index++].toInt() - 63
- result = result or (b and 0x1f shl shift)
- shift += 5
- } while (b >= 0x20)
- val dlng = if (result and 1 != 0) (result shr 1).inv() else result shr 1
- lng += dlng
- val p = LatLng(lat.toDouble() / 1E5,
- lng.toDouble() / 1E5)
- poly.add(p)
- }
- return poly
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement