简体   繁体   English

如何在 Android 中实现谷歌路线 API

[英]How to implement google directions API in Android

I'm a beginner in the field of android and currently working on a cab app .我是 android 领域的初学者,目前正在开发 cab 应用程序。 In this app I need to implement Map and directions API, I went through couple of tutorials and got several APIs but i dont know how to draw the returned data on the map so guys please help.. need some guidance.在这个应用程序中,我需要实现地图和路线 API,我浏览了几个教程并获得了几个 API,但我不知道如何在地图上绘制返回的数据,所以请大家帮忙.. 需要一些指导。 Thank you谢谢

For the moment i've extracted the latlng of source and destination and have created a seperate MapActivity for showing directions.目前我已经提取了源和目的地的纬度,并创建了一个单独的 MapActivity 来显示方向。

Google Directions API gives you a polyline (a list of LatLng points to draw a nice curvy line) of the returned route in an encoded format in "overview_polyline" and "polyline" fields in the JSON it returns. Google Directions API在它返回的 JSON 中的"overview_polyline""polyline"字段中以编码格式为您提供返回路线的折线(用于绘制漂亮曲线的 LatLng 点列表)。

By decoding this data you can create a Polyline object which is then drawn on the GoogleMap .通过解码这些数据,您可以创建一个Polyline对象,然后将其绘制在GoogleMap Writing your own decoder isn't too difficult, but there's also android-maps-utils library that contains a method for this and many other fancy things you might want to use on Google Maps related things.编写您自己的解码器并不太难,但还有android-maps-utils 库,其中包含一个方法,以及您可能想在 Google Maps 相关事物上使用的许多其他奇特事物。

An example use of this library would be something like这个库的一个例子使用是这样的

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM