flutter-mapbox-gl / maps

A Mapbox GL flutter package for creating custom maps
Other
1.04k stars 503 forks source link

CameraPosition not updating #1236

Closed sakyoyuto closed 1 year ago

sakyoyuto commented 1 year ago

Hi. Thank you for the wonderful package.

I have one concern. The content of cameraPosition of MapBoxMapController is not updated even if the map is moved. Zoom, target, etc. are not updated as initialPosition.

If this is something that doesn't update, is there a way to get the map's center point and zoom?

Thank you.

スクリーンショット 2022-11-25 17 33 02

My doctor

Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 3.3.8, on macOS 12.6.1 21G217 darwin-arm, locale ja-JP)
[✓] Android toolchain - develop for Android devices (Android SDK version 31.0.0)
[✓] Xcode - develop for iOS and macOS (Xcode 14.1)
[✓] Chrome - develop for the web
[✓] Android Studio (version 2020.3)
[✓] VS Code (version 1.73.1)
[✓] Connected device (4 available)
[✓] HTTP Host Availability

• No issues found!
aardrop commented 1 year ago

Do you have a listener to the camera position set up?

mapController.addListener(() {
      final cameraPosition = mapController.cameraPosition;
      print('camera move: $cameraPosition');
    });

this works for us.

sakyoyuto commented 1 year ago

Thank you for your reply.

The same code is executed, but with the following results.

mapController!.addListener(() {
      final cameraPosition = mapController!.cameraPosition;
      print('camera move: $cameraPosition');
    });

Returns the same value as the first.

flutter: camera move: CameraPosition(bearing: 0.0, target: LatLng(35.656554, 139.74777700000004), tilt: 0.0, zoom: 14.0)
flutter: map camera idle
flutter: camera move: CameraPosition(bearing: 0.0, target: LatLng(35.656554, 139.74777700000004), tilt: 0.0, zoom: 14.0)
flutter: camera move: CameraPosition(bearing: 0.0, target: LatLng(35.656554, 139.74777700000004), tilt: 0.0, zoom: 14.0)
flutter: map camera idle
flutter: camera move: CameraPosition(bearing: 0.0, target: LatLng(35.656554, 139.74777700000004), tilt: 0.0, zoom: 14.0)

I am currently using an iPhone 14 Pro.

aardrop commented 1 year ago

Can you share your MapboxMap construction, onMapCreated function, and versions?

sakyoyuto commented 1 year ago

MapBox Version

mapbox_gl: ^0.16.0

MapBoxMap

MapboxMap(
          accessToken: MapBoxConfiguration.token,
          styleString: MapBoxConfiguration.style,
          onMapCreated: (controller) {
            controller.addListener(() {
              print(controller.cameraPosition);
            });
          },
          initialCameraPosition: const CameraPosition(
            target: LatLng(35.656554, 139.747777),
            zoom: MapDefaultConfig.zoom,  // zoom = 14
          ),
          compassEnabled: true,
          onCameraIdle: () {
            print('map camera idle');
          },
          myLocationEnabled: true,
          myLocationRenderMode: MyLocationRenderMode.NORMAL,
          myLocationTrackingMode: MyLocationTrackingMode.Tracking,
          compassViewPosition: CompassViewPosition.TopLeft,
          compassViewMargins: const Point(16, 240),
        ),

Thank you.

aardrop commented 1 year ago

And which device/OS are you trying to run this on?

I would try moving the controller variable outside of the MapboxMap widget to its parent and using the onMapCreated function to set that parent variable. I believe the onMapCreated function only runs once, and even though you are adding a listener, it might not be persisting the changes within that variable. See this for a reference. See the onMapCreated function in this file. You can create a process that sets that parent variable and then creates the listener event. At least that's what we do, and it works.

sakyoyuto commented 1 year ago

Thank you for your cooperation.

trackCameraPosition: true,

We found the cause. I forgot to set trackingCameraPosition to true. I am very sorry...

Thank you again for your help. I will close this thread.