映射意图多点

时间:2013-10-24 18:10:37

标签: android google-maps

我正在尝试向Google地图发送一个意图,以显示多个点之间的行车路线。我使用的是here列出的方法,但似乎效果不佳。应用程序的最终功能是动态创建地图的URL,但为了测试,我创建了一个带有一堆随机点的静态:

https://maps.google.com/maps?saddr=San+Francisco,+CA&daddr=Los+Angeles,+CA+to:Phoenix,+AZ+to:Houston,+TX+to:Jacksonville,+FL+to:New+York,+NY+to:Buffalo,+NY+to:Chicago,+IL+to:Seattle,+WA+to:San+Jose,+CA

我的确切代码是:

 String mapUrl = "https://maps.google.com/maps?saddr=San+Francisco,+CA&daddr=Los+Angeles,+CA+to:Phoenix,+AZ+to:Houston,+TX+to:Jacksonville,+FL+to:New+York,+NY+to:Buffalo,+NY+to:Chicago,+IL+to:Seattle,+WA+to:San+Jose,+CA";
 Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(mapUrl));
 startActivity(i);

如果我从“使用完整操作”菜单将其发送到浏览器,则它可以正常工作。 (FWIW,如果我只是将url复制并粘贴到桌面上的Web浏览器中,它也能正常工作)另一方面,如果我将它发送到Maps应用程序,那么它的行为就不正常了。在我运行2.2和2.3的一些旧设备上,它显示了行车路线,但只标记了2个点,其中一个甚至不是我在网址中传递的点之一,而且行车路线很疯狂多个路线在不同地点之间来回传递。在任何4.X设备上,它都不显示任何内容,并显示“网络故障”或“未找到路由”。我没有任何3.X设备,但我希望它的行为方式与其中之一相似。

我不想使用MapView,因为这对我的应用程序的基于地图的次要要求来说太过分了。我只是希望能够向地图应用程序发送一个意图,该应用程序显示两个位置之间的路线,两个位置之间有航路点。我在新的Maps api的文档中找不到任何明确的答案,这可能吗?

3 个答案:

答案 0 :(得分:3)

使用此代码:

Uri gmmIntentUri = Uri.parse("https://www.google.com/maps/dir/?api=1&origin=18.519513,73.868315&destination=18.518496,73.879259&waypoints=18.520561,73.872435|18.519254,73.876614|18.52152,73.877327|18.52019,73.879935&travelmode=driving");
                    Intent intent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
                    intent.setPackage("com.google.android.apps.maps");
                    try {
                        startActivity(intent);
                    } catch (ActivityNotFoundException ex) {
                        try {
                            Intent unrestrictedIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
                            startActivity(unrestrictedIntent);
                        } catch (ActivityNotFoundException innerEx) {
                            Toast.makeText(this, "Please install a maps application", Toast.LENGTH_LONG).show();
                        }
                    }
  

以下是参考链接:   https://developers.google.com/maps/documentation/urls/guide

答案 1 :(得分:1)

final String uri = "http://maps.google.com/maps?daddr=Gachibowli"+"+to:Lingampally+to:Nizampet+to:kukatpally+to:moosapet+to:Nizampet+to:srnagar+to:ameerpet+to:jubileehills+to:kothaguda";
final Intent intent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse(uri));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setClassName("com.google.android.apps.maps", "com.google.android.maps.MapsActivity");
if (intent.resolveActivity(getActivity().getPackageManager()) != null) {
    startActivity(intent);
}

如果您想使用lat lng修改,请执行以下操作:

final String uri = "http://maps.google.com/maps?daddr=12.972442,77.580643"+"+to:12.9747,77.6094+to:12.9365,77.5447+to:12.9275,77.5906+to:12.9103,77.645";
final Intent intent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse(uri));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setClassName("com.google.android.apps.maps", "com.google.android.maps.MapsActivity");
if (intent.resolveActivity(MainActivity.this.getPackageManager()) != null) {
    startActivity(intent);
}

*使用" +到"你可以添加多个点。

答案 2 :(得分:0)

是的,请查看此链接Launching Google Maps Directions via an intent on Android

他们已经提到了这两种情况。