【问题标题】:Is there an API for Google Maps navigation in Android?Android中是否有用于谷歌地图导航的API?
【发布时间】:2011-03-04 09:21:06
【问题描述】:

我正在开发一个使用 Google 地图的 Android 应用程序。它运作良好。但是

当用户点击“Get Directions”加载地图后,Google Maps 会显示方向线,但无法逐个获取书面方向。如果您只是打开 Google 地图并获取路线,您可以在地图和路线列表之间来回切换。

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

【问题讨论】:

    标签: android google-maps


    【解决方案1】:

    获取方向和路线的最佳方式是使用 Google 地图的网络服务。它会为你提供一切。我在我的应用程序中使用了它。

    这里是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);
    

    希望这会对你有所帮助。

    【讨论】:

    • 所以只是打开谷歌地图应用程序?我们不能使用这个 api 在我们自己的应用程序中创建 UI?
    • 如果您想在自己的应用程序中创建带有导航的 UI,那么您必须利用 Google 的 Direction API 并将其与代码中的地图集成。
    • 这仅在设备上安装了 Google 地图应用程序时才有效。此应用程序可能不存在,例如在较新的华为设备上,或者当用户卸载地图应用程序时。所以这个答案并不涵盖所有用户案例。
    【解决方案2】:

    添加到 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);
    

    这是我在我的应用程序中使用的

    【讨论】:

      【解决方案3】:

      这应该对Routing / Driving directions on Android by mobile.synyx.de有帮助

      阅读 -> 从谷歌地图获取地理点

      【讨论】:

        【解决方案4】:

        自 2018 年起,Google 已发布 Google 地图导航 SDK。您可以在 I/O'18 上看到以下 Google Maps Platform 演示文稿,其中 Google Maps 产品经理提到了 SDK:

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

        很遗憾,此 SDK 目前尚未公开发布。您必须联系销售团队才能购买此 SDK 的许可证。正如我所见,目前只有 Uber 或 Lyft 等大型拼车公司在其应用程序中安装了此 SDK。

        或者,您可以使用Google Maps URLs 在导航模式下打开 Google 地图应用。 Google 地图网址构建了一个通用的跨平台网址来启动 Google 地图,因此您可以根据自己的意图使用它们。

        例如

        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);
        

        【讨论】:

          【解决方案5】:

          为了获得(当前)导航逐向书面指示,唯一的方法是解析迄今为止的通知。

          为了解决这个问题,我编写了一个开源库,可以在此过程中为您提供帮助(但它需要通知访问):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")
              }
          }
          

          它还提供了一个实用程序类来将此类事件公开为可以轻松传递给活动的包裹。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多