2012-12-08 3 views
28

Google지도 Android API V2를 사용하는 경우 내 주 활동에 다음 코드를 사용하여 Google Play 서비스가 설치되어 있는지 확인하기 위해 Google Play Services setup documentation을 따르고 있습니다.Google지도 Android API V2 기기에 GoogleMaps가 설치되어 있는지 확인합니다.

@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 버전)에는 GooglePlayService와 Google지도 앱이 모두 누락되어 있음을 알았습니다.

LogCat에서이 오류를보고합니다 : Google지도 Android API : Google지도 응용 프로그램이 없습니다.

질문 질문 - 기기에서 Google지도를 사용할 수 있는지 위와 유사한 검사를 수행하려면 어떻게해야합니까? 둘째, 사용자가 이미 Google지도를 설치 한 경우 설치된 버전이 Android Maps API V2와 호환되는지 확인해야합니다.

업데이트 여기에서 onCreate의 말()에서 호출 내 setupMapIfNeeded() 방법이다.

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

난 그냥 물어 수 rs? – luiscvalmeida

+1

@luiscvalmeida 69는 요청 코드 일뿐입니다 (제가 선택한 것입니다). http://developer.android.com/reference/com/google/android/gms/common/GooglePlayServicesUtil.html#getErrorDialog에서 getErrorDialog 메소드를 참조하십시오. – DiscDev

답변

50

더 많이 파고 들었을 때 나는 Google지도가 설치되어 있는지 PackageManager에게 요청해야한다는 것을 알았습니다. IMO는 Google Maps Android API V2 개발자 가이드에 실제로 포함되어야합니다 ... 개발자를 많이 그리워하고이 경우를 놓치고 사용자를 실망 시켰습니다.

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

내가 짧은 블로그 게시물을 작성 :

여기에 Google지도가 설치되어 있는지 확인하고 (isGoogleMapsInstalled() 참조)가 설치되어 있지 않은 경우 구글지도에 대한 목록 Play 스토어에 사용자를 다시 지시하는 방법 이러한 세부 사항 : 그 69 당신이 getErrorDialog 방법 paramte에이 무슨 How to check if Google Maps are installed and redirect user to the Play Store

+1

건배, 트릭을 아무 문제 없어!이것은 정말로 APi의 일부이거나 적어도 더 잘 문서화되어야합니다. –

+0

도움이 되니 기쁘다. 나는 그것이 최소한 API 문서의 일부가되어야한다는 데 전적으로 동의합니다 ...지도 용 GooglePlayServicesUtil과 비슷한 것을 가지고 있지 않은 이유는 무엇입니까? – DiscDev

+1

감사합니다. 매우 도움이됩니다. –

0

당신은 MapFragment.getMap을 (호출하여 GoogleMap으로 사용할 수 있는지 확인할 수 있습니다) 또는 : 나는 Google지도가 설치되어 있는지 확인하고 사용자에게 경고의 다른 블록을 표시 할 거라고 생각하는 곳이다 MapView.getMap() 메서드를 호출하고 반환 된 객체가 null이 아닌지 확인합니다.

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

아니요, 작동하지 않습니다. Google Play 서비스가 설치되었지만 Google지도가 설치되어 있지 않으면 (https://play.google.com/store/apps/details?id=com.google.android.apps.maps) MapFragment.getMap()을 호출하는 것은 null이 아닙니다. 그러나 어떤 타일도로드되지 않습니다. 확대/축소 버튼이 있고보기가지도처럼 보이지만지도 데이터가 다운로드되지 않고 LogCat에서 다음과 같은 보고서를 보냅니다. E/Google지도 Android API (23235) : Google지도 응용 프로그램이 없습니다. 이를 감지하는 방법이 필요하거나 사용자가 앱이 작동하지 않는다고 불평 할 것입니다. 내 setupMapIfNeeded 메서드로 질문을 업데이트했습니다. – DiscDev

+0

방금 ​​Google지도를 제거하고 맵이 내 앱에서 잘 작동하는지 확인했습니다. 아마도 최신 Google Play 서비스는 Google지도없이지도를 사용할 수 있습니다. – IlyaEremin

1

From Google guide

if (mapIntent.resolveActivity(getPackageManager()) != null) { 
    ... 
} 
관련 문제