Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

moveCamera() doesn't work in onMapCreated callback #371

Open
l2-map-dev opened this issue Jan 18, 2024 · 6 comments
Open

moveCamera() doesn't work in onMapCreated callback #371

l2-map-dev opened this issue Jan 18, 2024 · 6 comments
Labels
bug Something isn't working

Comments

@l2-map-dev
Copy link

I want to initialize a map with an initial bounding box, and since initialCameraPosition appears to only support specifying a center position and zoom level, it appears the appropriate way to accomplish this is by calling moveCamera() in the onMapCreated callback like so:

onMapCreated: (mapController) {
  mapController
      .moveCamera(
    CameraUpdate.newLatLngBounds(newBounds),
}

However, this call to moveCamera doesn't seem to do anything; the camera position remains at whatever the initial camera position was set to when creating the widget.

Sleeping with a future before running the update resolves the issue, but this is obviously hacky and I'd like to find a more reliable solution.

onMapCreated: (mapController) {

Future.delayed(const Duration(milliseconds: 200), () {
  mapController
      .moveCamera(
    CameraUpdate.newLatLngBounds(newBounds),
})
}

I've also tried using

WidgetsBinding.instance.addPostFrameCallback((_) {

mapController
      .moveCamera(
    CameraUpdate.newLatLngBounds(newBounds)

});

to delay calling moveCamera() until after the widget has been initialized, and oddly, using this method, the camera moves partially to the new position, but stops before reaching the new bounding box.

I've tried all of the above in onStyleLoaded() as well with no luck. It seems as though even at the point onMapCreated() or onStyleLoaded() are run, some state still isn't initialized which prevents the camera update from functioning. Can anyone offer some help here?

@m0nac0
Copy link
Collaborator

m0nac0 commented Jan 18, 2024

Can you try the onStyleLoadedCallback, please?

@l2-map-dev
Copy link
Author

Thanks for the reply! As I mentioned above, I did give that a try and unfortunately it had the same result. Even in the onStyleLoadedCallback, the camera remains at its initial position. I used mapController.getVisibleRegion() to verify this and the bounds of the map are identical before and after the call to moveCamera.

@josxha josxha added the bug Something isn't working label May 20, 2024
@LouisRaverdy
Copy link

This issue specifically affects iOS devices, while it functions perfectly on Android devices. The problem originates from the onMapCreated and onStyleLoaded callbacks, which are triggered prematurely on iOS. The question is, what is not being waited for? Maybe @josxha, do you have an idea ?

@josxha
Copy link
Collaborator

josxha commented Oct 14, 2024

Apparently, onMapCreated() and onStyleLoaded aren't save to call operations on the map from on iOS. I haven't looked at the at the ios implementation in depth. On android the onMapCreated() callback get's awaited internally to ensure the map has loaded. This callback does not exist in the iOS SDK. There seems to be some code in swift to work around this but it's currently not clear to me why it is done the way it is.

case "map#waitForMap":
if isMapReady {
result(nil)
// only call map#onStyleLoaded here if isMapReady has happend and isFirstStyleLoad is true
if isFirstStyleLoad {
isFirstStyleLoad = false
if let channel = channel {
onStyleLoadedCalled = true
channel.invokeMethod("map#onStyleLoaded", arguments: nil)
}
}
} else {
mapReadyResult = result
}

@LouisRaverdy
Copy link

LouisRaverdy commented Oct 14, 2024

I see now, thanks for the clarification. The problem is that there isn't a proper solution, which is really frustrating. The only workaround so far is to add a delay.

@josxha
Copy link
Collaborator

josxha commented Oct 14, 2024

Yep, I agree. Have you tried to enforce the viewport of the map with the cameraTargetBounds parameter and changing the bounds afterwards to unbound? (Problably again after a delay)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants