2012-12-08 21 views
28

在使用谷歌地圖API的Android V2我正在關注的Google Play Services setup documentation進行檢查,以確保谷歌Play服務,安裝,使用我的主要活動如下代碼:谷歌地圖API的Android V2檢查,如果谷歌地圖安裝在設備

@Override 
public void onResume() 
{ 
     checkGooglePlayServicesAvailability(); 

     super.onResume(); 
} 

public void checkGooglePlayServicesAvailability() 
    { 
     int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this); 
     if(resultCode != ConnectionResult.SUCCESS) 
     { 
      Dialog dialog = GooglePlayServicesUtil.getErrorDialog(resultCode, this, 69); 
      dialog.setCancelable(false); 
      dialog.setOnDismissListener(getOnDismissListener()); 
      dialog.show(); 
     } 

     Log.d("GooglePlayServicesUtil Check", "Result is: " + resultCode); 
    } 

這工作正常。但是,我注意到一些較舊的Android手機(主要是2.2版本)缺少GooglePlayServices以及Google地圖應用程序本身。

的logcat會報告這個錯誤: 谷歌地圖API的Android:谷歌地圖應用程序丟失。

問題 - 我怎麼能執行一個類似的檢查上面的一個谷歌地圖的設備上的可用性?其次,如果用戶已經安裝了Google地圖,我認爲該檢查需要確保他們的安裝版本與Android Maps API的V2兼容。

更新 這是我的setupMapIfNeeded()方法,它在onCreate()的末尾被調用。這是我想,我會想確定是否安裝了谷歌地圖,並提醒用戶,看到別的塊:

private void setUpMapIfNeeded() 
{ 
    // Do a null check to confirm that we have not already instantiated the map. 
    if (mMap == null) 
    { 
     // Try to obtain the map from the SupportMapFragment. 
     mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.basicMap)).getMap(); 

     if (mMap != null) 
     { 
      mMap.setLocationSource(this); 

      mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(44.9800, -93.2636), 10.0f)); 
      setUpMap(); 
     } 
     else 
     { 
      //THIS CODE NEVER EXECUTES - mMap is non-null even when Google Maps are not installed 
      MapConstants.showOkDialogWithText(this, R.string.installGoogleMaps); 
     } 
    } 
} 
+0

可我只是問什麼是69,你對getErrorDialog方法paramte rs? – luiscvalmeida

+1

@luiscvalmeida 69只是請求代碼(我選擇了它)。見http://developer.android.com/reference/com/google/android/gms/common/GooglePlayServicesUtil.html#getErrorDialog – DiscDev

回答

50

好了之後更戳,並慫恿我意識到我只需要問PackageManager如果安裝了谷歌地圖。 IMO應該真的包含在Google Maps Android API V2開發人員指南中......將會有很多開發人員錯過了這種情況,並且讓用戶感到沮喪。

以下是如何檢查是否谷歌地圖的安裝和重新引導用戶到Play商店,如果不是安裝了谷歌地圖列表(見isGoogleMapsInstalled()):

private void setUpMapIfNeeded() { 
    // Do a null check to confirm that we have not already instantiated the map. 
    if (mMap == null) 
    { 
     // Try to obtain the map from the SupportMapFragment. 
     mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.basicMap)).getMap(); 

     if(isGoogleMapsInstalled()) 
     { 
      if (mMap != null) 
      { 
       setUpMap(); 
      } 
     } 
     else 
     { 
      Builder builder = new AlertDialog.Builder(this); 
      builder.setMessage("Install Google Maps"); 
      builder.setCancelable(false); 
      builder.setPositiveButton("Install", getGoogleMapsListener()); 
      AlertDialog dialog = builder.create(); 
      dialog.show(); 
     } 
    } 
} 

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

public OnClickListener getGoogleMapsListener() 
{ 
    return new OnClickListener() 
    { 
     @Override 
     public void onClick(DialogInterface dialog, int which) 
     { 
      Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=com.google.android.apps.maps")); 
      startActivity(intent); 

      //Finish the activity so they can't circumvent the check 
      finish(); 
     } 
    }; 
} 

我寫了一個簡短的博客文章這些細節:How to check if Google Maps are installed and redirect user to the Play Store

+1

歡呼聲,沒有竅門沒有問題!這真的應該是APi的一部分,或者至少有更好的文檔記錄。 –

+0

很有幫助。我完全同意它至少應該成爲API文檔的一部分......爲什麼沒有類似於GooglePlayServicesUtil for maps的內容? – DiscDev

+1

非常感謝,這非常有幫助。 –

0

您可以驗證的GoogleMap可通過調用MapFragment.getMap()或MapView.getMap()方法並檢查返回的對象是否爲空。

public GoogleMap getMap()

The GoogleMap. Null if the view of the fragment is not yet ready. This can happen if the fragment lifecyle have not gone through onCreateView(LayoutInflater, ViewGroup, Bundle) yet. This can also happen if Google Play services is not available. If Google Play services becomes available afterwards and the fragment have gone through onCreateView(LayoutInflater, ViewGroup, Bundle), calling this method again will initialize and return the GoogleMap.

你可以閱讀有關verify map availability here

+4

的getErrorDialog方法不,這是行不通的。在安裝谷歌播放服務,但谷歌地圖沒有安裝(https://play.google.com/store/apps/details?id=com.google.android.apps.maps)調用MapFragment.getMap()不爲null。但是,沒有任何瓷磚會加載。縮放按鈕存在,視圖看起來像地圖,但沒有地圖數據將下載和LogCat報告:E /谷歌地圖Android API(23235):谷歌地圖應用程序丟失。我需要一種方法來檢測這個或用戶會抱怨該應用程序無法正常工作。用我的setupMapIfNeeded方法更新了問題。 – DiscDev

+0

我剛剛嘗試刪除谷歌地圖和地圖工作正常在我的應用程序。可能最新的谷歌播放服務允許使用沒有谷歌地圖的地圖。 – IlyaEremin

1

From Google guide

if (mapIntent.resolveActivity(getPackageManager()) != null) { 
    ... 
}