googlemaps / android-maps-compose

Jetpack Compose composables for the Maps SDK for Android
https://developers.google.com/maps/documentation/android-sdk/maps-compose
Apache License 2.0
1.12k stars 131 forks source link

Map Snapshots #86

Open MobileAppsGO opened 2 years ago

MobileAppsGO commented 2 years ago

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

jpoehnelt commented 2 years ago

@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.

recoverrelax commented 2 years ago

I would also want this feature. Thanks!

arriolac commented 2 years ago

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.

recoverrelax commented 2 years ago

any updates on this?

arriolac commented 2 years ago

@recoverrelax you can use MapEffect to get the snapshot.

Something like:

GoogleMap {
   MapEffect { map ->
      map.snapshot { snapshot ->
         // Got snapshot here
      }
   }
}
perdona commented 1 year ago

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.

arriolac commented 1 year ago

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'?

perdona commented 1 year ago

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.

pranandpatil commented 1 year ago

Any update on this? Is snapshot functionality is available ?

petros-chariskos commented 9 months ago

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@233610103@233610101065.570495220.570495220:3)
        at com.google.maps.api.android.lib6.phoenix.cb.run(:com.google.android.gms.policy_maps_core_dynamite@233610103@233610101065.570495220.570495220: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?

easydev991 commented 7 months ago

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

alexandrupele commented 6 months ago

@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.

petros-chariskos commented 6 months ago

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
            }
        }
    }
}
JuanFranCrater commented 5 months ago

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

Badranh commented 5 months ago

Would be awesome to have this feature +1

alexandrupele commented 4 months ago

@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.

petros-chariskos commented 4 months ago

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 commented 4 months ago

[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.

alexandrupele commented 3 months ago

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

alexandrupele commented 3 months ago

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