2011-07-03 26 views
6

當我使用此代碼谷歌地圖意向選擇對話框

string url = "somegooglemapsurl.com"; 
    Intent mapLauncherIntent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse(url)); 
    startActivity(mapLauncherIntent); 

選擇對話框彈出,詢問我是否要打開此地圖在地圖應用程序或瀏覽器。我希望從我的應用程序活動到Google地圖的過渡無縫。我怎樣才能禁止這個對話框,並告訴android打開地圖活動中的地圖?

編輯:當我進入谷歌地圖時,我想打開一個方向提示,使用公共蒸騰在某個位置。我可以通過Google地圖網址來完成此操作,但網址會顯示選擇對話框。

回答

17

我還沒有找到一個完美的解決方案,但這將至少打開地圖與正確的目的地和公共交通預先選定。然後,所有用戶所要做的就是點擊方向按鈕。

它還會檢查谷歌地圖是否已安裝,如果願意,可以選擇使用。

Intent intent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse("http://maps.google.com/maps?daddr=0,0%20(Imaginary%20Place)&dirflg=r")); 
if (isAppInstalled("com.google.android.apps.maps")) { 
    intent.setClassName("com.google.android.apps.maps", "com.google.android.maps.MapsActivity"); 
} 
startActivity(intent); 

 

// helper function to check if Maps is installed 
private boolean isAppInstalled(String uri) { 
    PackageManager pm = getApplicationContext().getPackageManager(); 
    boolean app_installed = false; 
    try { 
     pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES); 
     app_installed = true; 
    } catch (PackageManager.NameNotFoundException e) { 
     app_installed = false; 
    } 
    return app_installed; 
} 
+0

只是一個說明 - 強制街景需要'com.google.android.street'包和'com.google.android.street.Street'活動;意圖在http://stackoverflow.com/questions/3447723/about-android-google-api-streetview中描述 – tomash

0

首先,我要指出擺脫那個選擇對話框不一定是件好事。您希望爲用戶提供儘可能多的處理數據的選擇,這是Android生態系統之美的一部分。這就是說,它是不公平不理解你的應用程序進行判斷,所以...

可以使用geo協議:

http://developer.android.com/guide/appendix/g-app-intents.html

而是通過類似maps.google.com/blablabla格式的URI是這樣的geo:0,0?q=my+street+address

例子:

Intent mapLauncherIntent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse("geo:0,0?q=1111+imaginary+dr")); 
+0

我看了看,但我需要加載谷歌地圖只能用公交路線。我知道你可以通過谷歌地圖網址來做到這一點,但你可以通過地理位置來做到這一點嗎? – HighLife

+0

完整規範在這裏:http://tools.ietf.org/html/draft-mayrhofer-geo-uri-00我不確定,但我懷疑它。我有另一個想法,我會研究。 – Computerish

+0

對不起,我現在沒有更多時間看它。我相信如果你找出地圖活動的名字,你可以使用下面的代碼開始它:http://developer.android.com/reference/android/content/Intent.html#setClassName(java.lang.String,java.lang .String)'com.google.android.maps'是包名,但我不確定活動名稱。然後,這是一個搞清楚如何添加合適的演員的問題。不知道該怎麼做。 – Computerish

6

這裏是我迄今爲止所發現的不同的方法:

1.直接啓動導航:

這明確意圖將直接啓動谷歌導航:

Intent intent = new Intent(android.content.Intent.ACTION_VIEW, 
Uri.parse("google.navigation:q=my+street+address"); 
startActivity(intent); 

這將直接啓動導航(不幸的是沒有給用戶選擇交通工具)

2.讓用戶選擇的交通方式:

這將啓動谷歌導航應用,但讓用戶選擇啓動導航前的運輸方式:

Intent intent = new Intent(android.content.Intent.ACTION_VIEW, 
Uri.parse("http://maps.google.com/maps/?daddr=my+street+address"); 
intent.setClassName("com.google.android.apps.maps", "com.google.android.maps.MapsActivity"); 
startActivity(intent); 

3.啓動谷歌地圖上:

Intent intent = new Intent(android.content.Intent.ACTION_VIEW, 
Uri.parse("geo:0,0?q=my+street+address"); 
intent.setClassName("com.google.android.apps.maps", "com.google.android.maps.MapsActivity"); 
startActivity(intent); 

這將打開地圖,在這裏用戶可以啓動導航(並選擇運輸方式)。

在任何情況下,您都應該使用PackageManager和queryIntentActivities()或異常處理來處理用戶未安裝Google Maps/Navigation的情況。

在我的應用程序中,我使用的方法2工作得很好。希望這可以幫助。

附加組件:這是檢查應用程序是否安裝的方法。我在調用intent.setClassName()之前檢查是否安裝了「com.google.android.apps.maps」。

public static boolean isAppInstalled(String uri) { 
    PackageManager pm = getContext().getPackageManager(); 
    boolean app_installed = false; 
    try { 
     pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES); 
     app_installed = true; 
    } catch (PackageManager.NameNotFoundException e) { 
     app_installed = false; 
    } 
    return app_installed; 
} 
+0

我想爲用戶選擇合適的交通方式。 – HighLife