在Android中在Heremaps SDK上启动Route之前,将行程持续时间摘要作为提示消息 [英] Trip Duration Summary as a tip message before starting Route on Heremaps SDK in Android

查看:82
本文介绍了在Android中在Heremaps SDK上启动Route之前,将行程持续时间摘要作为提示消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在要求在地图片段上绘制路线时在android上的提示消息上显示总行程持续时间",如图所示. 总旅行时间

I wanted to show Total Trip duration on tip message on android here maps sdk when requested to draw route on map fragment as show in image. Total trip duration

推荐答案

此处是示例代码,可用于呈现2个位置(源和目的地,它们的路线以及在它们的路线中间的标记).您必须将图像替换为源位置和目标位置,并且对于气泡,您可以单击注释中的链接以使用显示计算出的距离(包括正在进行的流量)的文本生成位图(或其他在线指南).

Here is a sample code that you can use to render 2 locations, source and destination, their route, and a marker in the middle of their route. You have to replace the images for your source and destination locations and for your bubble you can follow the link in the comment to generate a bitmap (or any other guide online) with the text that displays the calculated distance (including the ongoing traffic).

private fun renderLocations() {
    val locations = arrayOf(
        GeoCoordinate(37.4219999, -122.0862462),
        GeoCoordinate(33.9880667, -118.4854341)
    )

    val sourceMarker = MapMarker(locations[0], Image().apply {
        try {
            // set source marker image
            setImageResource(android.R.drawable.ic_menu_compass)
        } catch (throwable: Throwable) { }
    })

    val destinationMarker = MapMarker(locations[1], Image().apply {
        try {
            // set destination marker image
            setImageResource(android.R.drawable.ic_menu_camera)
        } catch (throwable: Throwable) { }
    })

    map.addMapObjects(listOf(sourceMarker, destinationMarker))

    calculateRoute(locations[0], locations[1])
}

private fun calculateRoute(source: GeoCoordinate, destination: GeoCoordinate) {
    CoreRouter().apply {

        dynamicPenalty = DynamicPenalty().apply {
            trafficPenaltyMode = Route.TrafficPenaltyMode.OPTIMAL
        }

        calculateRoute(

            RoutePlan().apply {

                routeOptions = RouteOptions().apply {
                    transportMode = RouteOptions.TransportMode.CAR
                    setHighwaysAllowed(true)
                    routeType = RouteOptions.Type.SHORTEST // other alternatives: FASTEST, BALANCED
                    routeCount = 1
                }

                addWaypoint(RouteWaypoint(source))

                addWaypoint(RouteWaypoint(destination))

            }, object : Router.Listener<List<RouteResult>, RoutingError> {
                override fun onCalculateRouteFinished(p0: List<RouteResult>?, p1: RoutingError?) {
                    p1?.let {
                        if (it == RoutingError.NONE) {
                            p0?.let { result ->
                                if (result.isNotEmpty()) {
                                    // I only show the first result, maybe you want to show all routes
                                    val routeResult = result[0].route

                                    val durationWithDelayInSeconds = routeResult.getTtaIncludingTraffic(Route.WHOLE_ROUTE).duration

                                    val totalWayPoints = routeResult.routeGeometry?.size
                                    val middlePoint: GeoCoordinate? = routeResult.routeGeometry?.get(totalWayPoints!! / 2)

                                    middlePoint?.let {
                                        map.addMapObject(MapMarker(it/*, Image().apply {
                                            // Draw yourbitmap. A good resource can be found here: https://medium.com/@travells2323/android-draw-text-to-bitmap-8251f6d79150
                                        }*/
                                        ))
                                    }

                                    mapRoute?.let {
                                        map.removeMapObject(it)
                                    }

                                    mapRoute = null
                                    mapRoute = MapRoute(routeResult)

                                    mapRoute?.let {
                                        map.addMapObject(it)
                                        it.renderType = MapRoute.RenderType.USER_DEFINED
                                        it.color = Color.BLUE
                                        it.isTrafficEnabled = true
                                    }
                                }
                            }
                        }
                    }
                }

                override fun onProgress(p0: Int) {
                    /* The calculation progress can be retrieved in this callback. */
                }
            })
    }
}

以上代码的最终结果为

用所需的图像替换图像后,您将获得想要的最终结果.

Once you replace the images with the ones you want, you will have the end result you want to achieve.

这篇关于在Android中在Heremaps SDK上启动Route之前,将行程持续时间摘要作为提示消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆