android - 如何在Android中实现Google Directions API

标签 android google-maps-android-api-2 directions

我是 Android 领域的初学者,目前正在开发出租车应用程序。在这个应用程序中,我需要实现 map 和方向 API,我浏览了几个教程并获得了几个 API,但我不知道如何在 map 上绘制返回的数据,所以请大家帮忙......需要一些指导。谢谢

目前,我已经提取了源和目的地的纬度,并创建了一个单独的 MapActivity 来显示方向。

最佳答案

Google Directions API为您提供 encoded format 中返回路线的折线(用于绘制漂亮曲线的 LatLng 点列表)在它返回的 JSON 中的 "overview_polyline""polyline" 字段中。

通过解码此数据,您可以创建 Polyline然后在 GoogleMap 上绘制的对象。编写自己的解码器并不太困难,但还有 android-maps-utils library其中包含用于此目的的方法以及您可能想在 Google map 相关的事物上使用的许多其他奇特事物的方法。

该库的使用示例如下

GoogleMap map = ...;
String encodedPolyline = readFromJson();
Polyline line = map.addPolyline(new PolylineOptions()
    .addAll(PolyUtil.decode(encodedPolyline))
    .width(5)
    .color(Color.RED));

关于android - 如何在Android中实现Google Directions API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32819979/

相关文章:

google-maps-android-api-2 - "Google Maps Mobile SDK for Work"的 gradle 依赖项是什么?

Swift iOS 谷歌地图,坐标路径

java - Android工作室: Read data from Google spreadsheet using retrofit2 (GET)

android - 如何防止在设置中明确共享偏好

android - D/skia : --- Failed to create image decoder with message 'unimplemented'

android - 膨胀谷歌地图 v2 fragment 导致 ClassNotFoundException

java - 如何在constraintlayout中实现可滚动和放大缩小

android - 如何在android中更改Google map api v2上特定区域的颜色

directions - 默认情况下,我们如何从Google Direction API获取从A点到B点的最短距离路线

google-maps - Google map API [方向 API] 航点限制?