android-maps-compose icon indicating copy to clipboard operation
android-maps-compose copied to clipboard

Map Snapshots

Open MobileAppsGO opened this issue 2 years ago • 20 comments

Please add support for taking map snapshots: GoogleMap::snapshot(@NonNull GoogleMap.SnapshotReadyCallback callback)

MobileAppsGO avatar Mar 31 '22 14:03 MobileAppsGO

@MobileAppsGO Thank you for opening this issue. 🙏 Please check out these other resources that might be applicable:

This is an automated message, feel free to ignore.

jpoehnelt avatar Mar 31 '22 14:03 jpoehnelt

I would also want this feature. Thanks!

recoverrelax avatar Mar 31 '22 15:03 recoverrelax

Quickest is to expose a function in CameraPositionState although that won't to be the right place to do so. A state holder object like GoogleMapState that contains CameraPositionState etc. would be a better approach. Would be worth addressing that for the next major version change.

arriolac avatar May 18 '22 01:05 arriolac

any updates on this?

recoverrelax avatar Aug 02 '22 14:08 recoverrelax

@recoverrelax you can use MapEffect to get the snapshot.

Something like:

GoogleMap {
   MapEffect { map ->
      map.snapshot { snapshot ->
         // Got snapshot here
      }
   }
}

arriolac avatar Aug 02 '22 16:08 arriolac

Is this going to be an official feature? It would be very nice to have a map span to base snapshot on, cause CameraPosition is very limited to handle multiple markers with a good framing displaying all markers.

perdona avatar Oct 06 '22 02:10 perdona

Is this going to be an official feature? It would be very nice to have a map span to base snapshot on, cause CameraPosition is very limited to handle multiple markers with a good framing displaying all markers.

Can you clarify what you mean by 'It would be very nice to have a map span to base snapshot on'?

arriolac avatar Oct 06 '22 15:10 arriolac

Yes.. I think we should have other options of zoom, since in my case I need to frame multiple markers, and set the snapshot with a zoom that will show every marker wihtout being too far.

perdona avatar Oct 06 '22 15:10 perdona

Any update on this? Is snapshot functionality is available ?

pranandpatil avatar Mar 05 '23 22:03 pranandpatil

I have been using in production the recommend by @arriolac code snippet:

GoogleMap {
   MapEffect { map ->
      map.snapshot { snapshot ->
         // Got snapshot here
      }
   }
}

and is working. The downside is that in very rare cases and completely randomly it throws:

    java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.graphics.Bitmap.isMutable()' on a null object reference
    	at android.graphics.Canvas.<init>(Canvas.java:113)
    	at com.google.maps.api.android.lib6.impl.a.a(:com.google.android.gms.policy_maps_core_dynamite@[email protected]:3)
    	at com.google.maps.api.android.lib6.phoenix.cb.run(:com.google.android.gms.policy_maps_core_dynamite@[email protected]:9)
    	at android.os.Handler.handleCallback(Handler.java:958)
    	at android.os.Handler.dispatchMessage(Handler.java:99)
    	at android.os.Looper.loopOnce(Looper.java:205)
    	at android.os.Looper.loop(Looper.java:294)
    	at android.app.ActivityThread.main(ActivityThread.java:8194)
    	at java.lang.reflect.Method.invoke(Native Method)
    	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:552)
    	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:971)

maybe because MapEffect is still experimental? Will it be any update any time soon?

petros-chariskos avatar Nov 07 '23 13:11 petros-chariskos

We are still waiting for the stable official implementation, correct?

easydev991 avatar Jan 03 '24 12:01 easydev991

@petros-chariskos i twas able to fix that NPE by putting that MapEffect behind a callback of "onMapLoaded". It's a parameter for GoogleMap. Try it, I hope it helps. I never seen the NPE ever since.

alexandrupele avatar Jan 18 '24 10:01 alexandrupele

thank you @alexandrupele, I am already doing that and still getting the NPE. I can give the code snippet in case there is something wrong with my implementation but I think it's pretty straight forward.

var isMapLoaded by remember { mutableStateOf(false) }
val mapBitmap: MutableState<Bitmap?> = remember { mutableStateOf(null) }

GoogleMap(
    modifier = Modifier,
    cameraPositionState = cameraPositionState,
    properties = mapProperties,
    uiSettings = uiSettings,
    onMapLoaded = {
        isMapLoaded = true
    }
) {
    MapMarker(
        context = LocalContext.current,
        state = rememberMarkerState(position = LatLng(latLng.latitude, latLng.longitude)),
        iconResourceId = iconResourceId
    )

    if (isMapLoaded) {
        MapEffect { map ->
            map.snapshot { snapshot ->
                mapBitmap.value = snapshot
            }
        }
    }
}

petros-chariskos avatar Jan 19 '24 14:01 petros-chariskos

Hi! Is this feature still being working on? Or any other way to access the googleMap instance without the use of MapEffect?Thanks!

JuanFranCrater avatar Feb 16 '24 10:02 JuanFranCrater

Would be awesome to have this feature +1

Badranh avatar Feb 26 '24 09:02 Badranh

@petros-chariskos try updating to compose maps 4.3.3. I see they've fixed a few things about the phase of the composition at which point the map is rendered. Let me know if it fixes it for you. I've also seen this crash again, now I'm hoping it goes away with the new version. I had 4.3.0 before.

alexandrupele avatar Mar 08 '24 16:03 alexandrupele

Thanks @alexandrupele, I have updated the compose maps to 4.3.3 and this release is already in production since a couple of days ago. I hope this solves it, otherwise I will update the thread.

petros-chariskos avatar Mar 08 '24 16:03 petros-chariskos

[Thread update] With com.google.maps.android:maps-compose in 4.3.3 the NPE: Attempt to invoke virtual method 'boolean android.graphics.Bitmap.isMutable()' on a null object reference still appears in the logs in production.

petros-chariskos avatar Mar 14 '24 08:03 petros-chariskos

I still get this crash. Anyone found a solution for it?

alexandrupele avatar Apr 23 '24 21:04 alexandrupele

@petros-chariskos check this thread until the end, i think the answer is there: https://issuetracker.google.com/issues/290806747

alexandrupele avatar Apr 25 '24 15:04 alexandrupele