在Android中,是否有用于Google地图导航的API?

18

我正在开发一个Android应用程序,其中使用了Google Maps。它运行良好。但是,在加载地图后,当用户单击“获取方向”时,Google Maps会出现方向线,但没有办法获得逐步书写的指示。如果您只是打开Google Maps并获取方向,您可以在地图和方向列表之间切换。

是否有任何API可用于获取Android设备默认Google地图中提供的所有功能?

5个回答

40

获取方向和路线的最佳方式是使用Google Maps的Web服务。它将提供您所需的一切。我已在我的应用程序中使用过它。

以下是一个示例,其中saddr =起始地址,daddr =目标地址(即纬度和经度)。您还可以传递字符串作为地址,而不是lat / lng。

final Intent intent = new Intent(Intent.ACTION_VIEW,Uri.parse("http://maps.google.com/maps?" + "saddr="+ latitude + "," + longitude + "&daddr=" + latitude + "," + longitude));
    intent.setClassName("com.google.android.apps.maps","com.google.android.maps.MapsActivity");
                        startActivity(intent);

希望这能帮到您。


1
那么这只是打开谷歌地图应用程序吗?我们不能使用此API在我们自己的应用程序中创建UI吗? - user25
1
如果您想在自己的应用程序中创建带有导航的用户界面,则必须利用Google的Direction API并将其与代码中的地图集成。 - Scorpion
只有当设备上安装了谷歌地图应用程序时才能使用此功能。例如,在较新的华为设备上可能没有这个应用程序,或者用户卸载了地图应用程序。因此,此答案并不涵盖所有用户情况。 - Pointer Null

8

在 Scorpion 的代码基础上(该代码完美运行),您可能需要获取当前位置。

    Location currentLocation = googleMap.getMyLocation();
    double latitudeCurr = currentLocation.getLatitude();
    double longitudeCurr = currentLocation.getLongitude();

saddr 表示起始地址

daddr 表示目的地地址

    final Intent intent = new Intent(Intent.ACTION_VIEW,
    Uri.parse("+http://maps.google.com/maps?"  "saddr="
    + latitudeCurr + "," + longitudeCurr + "&daddr="
    + latitude + "," + longitude));
    intent.setClassName("com.google.android.apps.maps",
    "com.google.android.maps.MapsActivity");
    startActivity(intent);

这是我在应用程序中使用的东西。


4

3
截至2018年,谷歌已经宣布了Google Maps Navigation SDK。您可以在I/O'18上查看以下Google Maps平台演示文稿,其中Google Maps产品经理提到了SDK:

https://youtu.be/XVjyIA3f_Ic?t=20m31s

很遗憾,目前此SDK不对公众开放。您需要联系销售团队以购买此SDK的许可证。目前我所看到的只有像Uber或Lyft这样的大型共享乘车公司在其应用程序中拥有此SDK。

或者,您可以使用Google Maps URLs在导航模式下打开Google Maps应用程序。 Google Maps URLs构建了一个通用的、跨平台的URL来启动Google Maps,因此您可以将它们与您的意图一起使用。

例如:

String url = "https://www.google.com/maps/dir/?api=1&destination=Madrid,Spain&origin=Barcelona,Spain&travelmode=driving&dir_action=navigate";           
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);

1
为了获得(当前)导航逐步书面指示,目前唯一的方法是解析通知。

为了处理这个问题,我编写了一个开源库,可能会在此过程中帮助您(但需要通知访问权):GMapsParser

您可以通过执行以下操作来获取服务状态:

class NavigationListenerEmitter : NavigationListener() {
    override fun onNavigationNotificationAdded(navNotification: NavigationNotification) {
        Log.d("notificationListener", "Got initial notification $navNotification")
    }

    override fun onNavigationNotificationUpdated(navNotification : NavigationNotification) {
        Log.d("notificationListener", "Got notification update $navNotification")
    }

    override fun onNavigationNotificationRemoved(navNotification : NavigationNotification) {
        Log.d("notificationListener", "Removed notification $navNotification")
    }
}

它还提供了一个实用程序类,以公开这些事件作为包裹,可以轻松地传递给活动。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接