0

我知道使用 Google 的方向网络服务可以获得坐标之间的方向。但是还有其他方法可以获得同样准确的路线吗?

我发现一些关于 SOhttp://maps.google.com/用于获得方向的问题。但随后也发现很少有其他问题的答案表明它不再受支持。

我很困惑,因为这是我第一次处理 Android 版 Google 地图。

是否有任何内置的 android sdk 方法来获取方向?

4

2 回答 2

2

您可以向谷歌地图应用程序发送意图,并让该应用程序为您完成所有工作,但它没有记录,可能永远不会。

为此,您可以像这样给它一个纬度/经度

Intent NavIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("google.navigation:q=" +latitude +","+longitude));
startActivity(NavIntent);

或使用相同的意图但使用地址,看看地图是否可以解析地址。(我没有地址的例子)。

除了使用 google 方向 API 或其他 3rd 方方向网络服务之外,实际上没有其他方法可以在您的应用程序中获取方向,除非您Open Street Maps使用航路点查看自己计算方向的位置,但这非常复杂

于 2013-07-29T17:18:19.317 回答
0

我选择在我的应用程序中使用谷歌地图应用程序的意图,这里的理念是 Android 应用程序应该利用和补充其他功能

Intent i = new Intent(Intent.ACTION_VIEW,Uri.parse(getDirectionUrl(srcLat, srcLng, dstLat, dstLng)));
if (isGoogleMapsInstalled(this)) {
    i.setComponent(new ComponentName("com.google.android.apps.maps", "com.google.android.maps.MapsActivity"));
}
startActivity(i);

构建路线 URL 的方法:

public static String getDirectionUrl (double srcLat, double srcLng, double dstLat, double dstLng) {
    //return google map url with directions 
    StringBuilder urlString = new StringBuilder();
    urlString.append("http://maps.google.com/maps?f=d&saddr=")
    .append(srcLat)
    .append(",")
    .append(srcLng)
    .append("&daddr=")
    .append(dstLat)
    .append(",")
    .append(dstLng);
    return urlString.toString();          
}

测试地图是否安装的方法:

public static boolean isGoogleMapsInstalled(Context c)  {
        try
        {
            @SuppressWarnings("unused")
            ApplicationInfo info = c.getPackageManager().getApplicationInfo("com.google.android.apps.maps", 0 );
            return true;
        } 
        catch(PackageManager.NameNotFoundException e)
        {
            return false;
        }
    }

缺点是 Google 可能会更改 URL 结构。

于 2013-07-29T18:08:47.710 回答