搜索
首页Javajava教程如何使用 Google Maps API v2 获取路线规划行车路线?

How to Get Turn-by-Turn Driving Directions Using Google Maps API v2?

使用 Google Maps API v2 获取行车路线

获取两个位置之间的行车路线是许多地图应用程序的常见要求。但是,您提供的代码仅在点之间绘制一条直线,而不是提供逐向导航。

使用 Android GoogleDirection Library 的解决方案

要检索行车路线,您可以使用 Android GoogleDirectionLibrary,这是 akexorcist 最近发布的库。以下是使用该库的修改后的代码片段:

import com.akexorcist.googledirection.DirectionCallback;
import com.akexorcist.googledirection.GoogleDirection;
import com.akexorcist.googledirection.model.Direction;
import com.akexorcist.googledirection.model.Leg;
import com.akexorcist.googledirection.model.Route;
import com.akexorcist.googledirection.util.DirectionConverter;

...

// Replace with your API key
String apiKey = "YOUR_API_KEY";

GoogleDirection.withServerKey(apiKey)
        .from(new LatLng(12.917745600000000000,77.623788300000000000))
        .to(new LatLng(12.842056800000000000,7.663096499999940000))
        .execute(new DirectionCallback() {
            @Override
            public void onDirectionSuccess(Direction direction, String rawBody) {
                if (direction.isOK()) {
                    Route route = direction.getRouteList().get(0);
                    Leg leg = route.getLegList().get(0);
                    
                    // Draw the path (Polylines)
                    List<latlng> directionPositionList = DirectionConverter.decodePoly(leg.getPolylinePoint());
                    Polyline line = mMap.addPolyline(new PolylineOptions()
                            .addAll(directionPositionList)
                            .width(5)
                            .color(Color.RED));
                    
                    // Display turn-by-turn instructions
                    String[] instructions = DirectionConverter.provideInstructionList(leg);
                    for (String instruction : instructions) {
                        Log.d("Direction", instruction);
                    }
                } else {
                    // Handle error
                }
            }

            @Override
            public void onDirectionFailure(Throwable t) {
                // Handle error
            }
        });</latlng>

使用此修改后的代码,您应该能够获取两个位置之间的行车路线,包括逐向指示。

以上是如何使用 Google Maps API v2 获取路线规划行车路线?的详细内容。更多信息请关注PHP中文网其他相关文章!

声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
1 个月前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
1 个月前By尊渡假赌尊渡假赌尊渡假赌
威尔R.E.P.O.有交叉游戏吗?
1 个月前By尊渡假赌尊渡假赌尊渡假赌

热工具

VSCode Windows 64位 下载

VSCode Windows 64位 下载

微软推出的免费、功能强大的一款IDE编辑器

螳螂BT

螳螂BT

Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

功能强大的PHP集成开发环境

Dreamweaver Mac版

Dreamweaver Mac版

视觉化网页开发工具

MinGW - 适用于 Windows 的极简 GNU

MinGW - 适用于 Windows 的极简 GNU

这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。