2

I have the Google Map API v2 working fine on Android using a SupportMapFragment in a FragmentActivity.

However, when there is no network connection (and no cached information), obviously, no map can be shown. LogCat gives me an error message confirming this ("Google Maps Android API: Failed to load map. Could not connect to Google servers"), but this is not thrown.

How can I detect this case programmatically, because I would like to forward to a different Activity in this case? The docs claim that if no map could be loaded getMap() called on the SupportMapFragment would return null, but this does not seem to be the case.

I've been searching around for almost two days for a solution, but was not able to find anything. Am I totally overlooking something or is it really not possible to find out whether something was actualy loaded? Any help is greatly appreciated.

Edit:

Here is my current code, the error message appears on the logs after the setContentView is executed (checked with debugger), but it still says that ConnectionResult == SUCESS.

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    SupportMapFragment smf = ((SupportMapFragment) 
    getSupportFragmentManager().findFragmentById(R.id.map));
    map = smf.getMap();

    if (map != null && GooglePlayServicesUtil.isGooglePlayServicesAvailable(this) == 
                                        ConnectionResult.SUCCESS) {
        // doSomething with the map
    } else {
        Toast.makeText(this, "Google Maps not working", Toast.LENGTH_SHORT).show();
        System.out.println(GooglePlayServicesUtil.isGooglePlayServicesAvailable(this));
        // forward to another activity
    }
}
Oliver
  • 43
  • 6

1 Answers1

2

GooglePlayServicesUtil.isGooglePlayServicesAvailable is related to GooglePlayServices.APK available on your device. API will not give you any indication of when tiles are loaded.

You have the options to:

  1. check if any tiles are cached by trying (hackish, simplified): new File(getExternalCacheDir(), "cache_vts_your.package.name.0").exists()
  2. check internet availability with your own code
  3. parse logcat to find the error

Edit:

Note point 3 is deprecated because applications are not allowed to read logs anymore due to security reasons

MaciejGórski
  • 22,187
  • 7
  • 70
  • 94
  • Many thanks for these ideas, just implemented no. 1 and it seems to work very well. However, I somewhat do not understand why Google does not make the error message from the LogCat available programmatically. – Oliver Apr 06 '13 at 10:15
  • @Oliver You would have to use both to know. The first time the app starts you will have empty cache, but tiles will load soon. Added 3rd hack to the answer. – MaciejGórski Apr 06 '13 at 10:29
  • Yes, you are right, but for my purposes it should be fine to see a cached map. Thanks again! Out of curiosity, how would I get the LogCat text programmatically? BTW, I just tried it out, the error message does not appear when cached tiles are available... – Oliver Apr 06 '13 at 13:00
  • Ok, this was pretty easy to google. :-) One can get an InputStream from LogCat as described here: http://www.coderzheaven.com/2011/07/16/how-to-read-logcat-contents-programmatically-in-android. Perhaps I will give this a try as well... – Oliver Apr 06 '13 at 13:01