Detect When Android V2 Maps Has Loaded

Detect when Android v2 maps has loaded

OnMapLoadedCallback doesn't fire until after the tiles on map are loaded. Only fires once so you'll have to call it nine times to take nine snapshots.

When you have a reference to the map set the call back.

mMap.setOnMapLoadedCallback(this);

When the onMapLoaded event fires take the snapshot.

@Override
public void onMapLoaded() {
if (mMap != null) {
mMap.snapshot(this);
}
}

See the documentation for further information: https://developers.google.com/maps/documentation/android/reference/com/google/android/gms/maps/GoogleMap.OnMapLoadedCallback

Good Luck

**** history of waiting for this feature to be implemented.

Updates:
Status: Fixed
Labels: Fixed-Oct2013

Comment #3 on issue 5779 by schr...@google.com: Ability to be notified when the map is fully loaded/rendered
http://code.google.com/p/gmaps-api-issues/issues/detail?id=5779

A map loaded callback interface has been added in the latest release of the Google Maps Android API v2.

See the documentation for further information: https://developers.google.com/maps/documentation/android/reference/com/google/android/gms/maps/GoogleMap.OnMapLoadedCallback

Thanks to everyone for starring issue 5779 and 61675
Danny117

**** edit ****

Please star this issue if you come here. http://code.google.com/p/android/issues/detail?id=61675
Your star clicks will motivate google to move on this issue.

The map update 13 causes a problem with google play services so you can't use the new OnMapLoadedCallback yet. You can compile against it but the map api generates an error message in the logcat Google Play services out of date. Requires 4030500 but found 3266130

* edit *
Brand new OnMapLoadedCallback

Updates:
Status: Fixed
Labels: Fixed-Oct2013

Comment #3 on issue 5779 by schr...@google.com: Ability to be notified when the map is fully loaded/rendered
http://code.google.com/p/gmaps-api-issues/issues/detail?id=5779

A map loaded callback interface has been added in the latest release of the Google Maps Android API v2.

See the documentation for further information: https://developers.google.com/maps/documentation/android/reference/com/google/android/gms/maps/GoogleMap.OnMapLoadedCallback

Thanks to everyone for starring issue 5779.
Danny117

**** The following is deprecated *****

Looks like its a wait for update solution. I clicked the star for you as I was going to work on this feature in my own api v2 map.

edit***
This is the proposed new callback for when the map is rendered.
Everyone that visits please follow link and star this issue.
http://code.google.com/p/gmaps-api-issues/issues/detail?id=5779

*** previous snapshop callback will not be changed.
Everyone that visits please follow link and star this issue. http://code.google.com/p/gmaps-api-issues/issues/detail?id=5712

https://developers.google.com/maps/documentation/android/reference/com/google/android/gms/maps/GoogleMap.SnapshotReadyCallback

How can I detect pogrammatically whether a Google Map was actually loaded on Android with API v2 (e.g. when network is not available)?

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

How can I check wether a (Google maps API) map has loaded?

You can try using a OnCameraChangeListener on your map. The onCameraChange call will be called when the map's tiles are initially loaded.

  this.map.setOnCameraChangeListener(new OnCameraChangeListener() {

public void onCameraChange(CameraPosition arg0) {
isMapReady = true;
map.setOnCameraChangeListener(null);
}
});

Android Java Google Maps, need to check if map is loaded fully before executing dialog

How about GoogleMap.OnMapLoadedCallback?

How to know Google map has loaded our current location by using google map api in android?

I think you're looking for this callback

com.google.android.gms.maps.GoogleMap.OnMapLoadedCallback

this is fired when

Callback interface for when the map has finished rendering. This occurs after all tiles required to render the map have been fetched, and all labeling is complete. This event will not fire if the map never loads due to connectivity issues, or if the map is continuously changing and never completes loading due to the user constantly interacting with the map.

Emphasis mine.

You can implement the callback and inside you call the code that takes the screenhsot.


If you need to do something after the map is loaded, and you want to know when that happens, you can implement a custom callback interface that you can manually fire when you are done with whatever it is you need to do before taking the screenshot.

How to detect if google map loaded successfully

Listen for the "tilesloaded" event instead. It's the last event to fire when a map (successfully) loads and you are actually showing a map.

If there's no map (e.g. you haven't set the width/height explicitly), this event won't fire even though idle does.

You can use the Map Events example and throttle your connection from within the Network tab to do some tests. Here's the same example with width/height not set for #map.

Is there a way to get notified when the GoogleMap is ready in a MapView object in the new Google Maps Android API v2?

To use the MapView directly you need to make sure you forwarding the Activity lifecycle methods through to the MapView.

E.g. to set it up, you'd do this in your onCreate()

 @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.raw_mapview_demo);

mMapView = (MapView) findViewById(R.id.map);
mMapView.onCreate(savedInstanceState);

setUpMapIfNeeded();
}

private void setUpMapIfNeeded() {
if (mMap == null) {
mMap = ((MapView) findViewById(R.id.map)).getMap();
if (mMap != null) {
setUpMap();
}
}
}

The code is taken from Google's sample code that is included with the Play Services lib. You also need to route onDestroy(), onResume(), and onPause().

FYI you need to check for a null pointer for both the MapView and MapFragment as the user may not have Google Play Services installed. If it isn't installed the user should get a prompt to install the APK and then you can return to this Activity.

Hope that helps.

Ryan

Google Maps Android API V2 check if GoogleMaps are installed on device

Alright after more poking and prodding I realized I just need to ask PackageManager if google maps are installed. IMO this should really be included in the Google Maps Android API V2 developers guide...there are going to be lots of devs that miss this case and have frustrated users.

Here's how to check if Google Maps are installed and re-direct the user to the Play Store listing for google maps if it's not installed (see 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();
}
};
}

I wrote up a short blog post with these details: How to check if Google Maps are installed and redirect user to the Play Store



Related Topics



Leave a reply



Submit