Eugene Brusov
Eugene Brusov

Reputation: 17846

Can I somehow get errors occur during getMapAsync call?

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?

Upvotes: 3

Views: 173

Answers (2)

Jin Thakur
Jin Thakur

Reputation: 2773

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

Upvotes: 0

Andrii Omelchenko
Andrii Omelchenko

Reputation: 13343

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.

Upvotes: 1

Related Questions