3

If I intentionally disconnect device from internet or put wrong API key then call SupportMapFragment.getMapAsync GoogleMaps just shows gray screen

GoogleMaps with error

And seems like there's no way to get more or less descriptive error from GoogleMaps SDK.


I tried to invoke MapsInitializer.initialize(context) but it always returns ConnectionResult.SUCCESS even if device is offline then MapFragment just shows gray screen.

So is there any way to get some descriptive error from Google Maps SDK rather just a gray screen?

Eugene Brusov
  • 17,146
  • 6
  • 52
  • 68

2 Answers2

1

Anyway, you can enable for your key some API with access via URL (Directions, Static maps etc.), e.g. for Places API:

https://maps.googleapis.com/maps/api/place/details/json?place_id=ChIJN1t_tDeuEmsRUsoyG83frY4&fields=name,rating,formatted_phone_number&key=YOUR_API_KEY

and try to use it via HttpURLConnection request. In HttpURLConnection response you got what you need. In case of not valid API key something like:

{
   "error_message" : "You must use an API key to authenticate each request to Google Maps Platform APIs. For additional information, please refer to http://g.co/dev/maps-no-account",
   "html_attributions" : [],
   "status" : "REQUEST_DENIED"
}

Update:

Mobile Native Static Maps (a Google map object in a Maps SDK for Android or Maps SDK for iOS mobile application) has no usage limits, so is no need to check exceeding map usage limits. Other issues like no internet connection, no Google Play Services installed etc. you can test other ways. E.g. this for Internet connection test (also you can ping https://www.google.com/maps), that for detecting installed Google Play Services and anyway you can detect "gray screen" before show it to user via, for example, set camera position to place with exactly known "non-gray" - color (sea, or forest) position and check screen pixel color. For getting bitmap of map in background you can use solution like that.

Andrii Omelchenko
  • 13,183
  • 12
  • 43
  • 79
  • Thanks for the advice! It answers the question but only partially. Still don't know how to handle exceeding map usage limits. I hardly believe that Places API limits somehow relates to GoogleMaps API usage limits. Also still have no clue how to handle other possible issues listed in https://developers.google.com/android/reference/com/google/android/gms/common/ConnectionResult.html – Eugene Brusov Jan 22 '20 at 12:09
  • Seems Maps SDK for Android has no usage limits itself. Limits has [certain APIs](https://developers.google.com/maps/faq#usage_apis) – Andrii Omelchenko Jan 22 '20 at 12:45
  • To check gray area you can simply do `googleMap.snapshot { bitmap -> }` – Simon Jan 28 '20 at 13:49
  • @AndriiOmelchenko Well, it still doesn't matter if gmaps have limits or not. And this answer is still just a workaround that doesn't work if some team member accidentally revokes gmaps key or Google does it, or something else happens. Instead of going through all possible stuff that might happen I just wanna to have simple mechanism to notify mobile devs about gmaps not showing and just grayed out with some kind of error code or something like that like any other API does. Sad to say but seems like it's not possible to get error details from gmaps API. – Eugene Brusov Feb 10 '20 at 18:44
  • @EugeneBrusov Yes, its just workaround. But if your goal is not to get error details from gmaps API but detect "gray screen" before show it to user you can successfully use it :) – Andrii Omelchenko Feb 10 '20 at 18:49
  • @AndriiOmelchenko Thanks I totally agree with you on that point. But the question was 'Is there any way to get some descriptive error from Google Maps SDK rather just a gray screen'. Anyway I appreciate your input! – Eugene Brusov Feb 10 '20 at 18:53
  • @EugeneBrusov You're welcome! If you find the answer - add it :) – Andrii Omelchenko Feb 10 '20 at 18:56
  • @EugeneBrusov You have so many scenarios.If team member revokes key etc. Dont you have different environments and at least one key which no one can revoke. If you are making a paid application don't you put credit card for Extra use. This is the limit for use https://cloud.google.com/maps-platform/pricing/sheet/?_ga=2.42852321.535492201.1581444008-1762000081.1574711893 – Jin Thakur Feb 11 '20 at 18:02
  • There is limit on usage API QPS limit Directions 50 QPS Distance Matrix 1000 QPS Elevation 100 QPS Geocoding 50 QPS Geolocation 100 QPS Dynamic Maps 500 QPS Static Maps 500 QPS Street View Image API 500 QPS Time Zone 500 QPS Places (JavaScript and web) 100 QPS Roads 500 QPS https://cloud.google.com/maps-platform/pricing/sheet/?_ga=2.42852321.535492201.1581444008-1762000081.1574711893 – Jin Thakur Feb 11 '20 at 18:03
0

MapsInitializer.initialize(context) Initializes the Google Maps SDK for Android so that its classes are ready for use. If you are using MapFragment or MapView and have already obtained a (non-null) GoogleMap by calling getMapAsync() on either of these classes, then it is not necessary to call this.

check for google map object is not null

@Override
    public void onMapReady(GoogleMap map) {
if(map!=null)
        map.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
  else

{ //not working } }

Try this on ready event.

 public boolean isInternetAvailable() {
        try {
            InetAddress ipAddr = InetAddress.getByName("google.com"); 
            //You can replace it with your name
                return !ipAddr.equals("");

            } catch (Exception e) {
                return false;
        }
    }
Jin Thakur
  • 2,711
  • 18
  • 15
  • check all the sample demo https://github.com/googlemaps/android-samples/blob/7e9b01cf8288f44b75303ce1b2d8cd64c5cb76cf/ApiDemos/java/app/src/main/java/com/example/mapdemo/RetainMapDemoActivity.java – Jin Thakur Jan 31 '20 at 18:09
  • Not sure if it adds any value to previous answer. Map object is always NOT-null even if grayed-out and checking internet connection does not work if some team member revokes gmap keys or Google does it, or something else happened that cannot be handled by simple internet availability check. – Eugene Brusov Feb 10 '20 at 18:50