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

[FEATURE] expose MapLibre through context #522

Open
XanderD99 opened this issue Nov 6, 2024 · 0 comments
Open

[FEATURE] expose MapLibre through context #522

XanderD99 opened this issue Nov 6, 2024 · 0 comments
Labels
enhancement New feature or request

Comments

@XanderD99
Copy link
Contributor

Feature Description

Add child widget option to the map view and expose mapController in context to child widgets.

As example I have done this myself to make it easier to create overlay on top of my map widget to add buttons or other widget that should interact with the map. This kind of removes the need to either create a very big map widget with all elements that interact with it or the struggle of having to pass down the mapcontroller.

I know you can use the static function of the MapLibrePlatform class but with this you don't know which map you are interacting with in the case where you are using multiple maps at once.

This is my map view widget:

class MapView extends StatefulWidget {
  final Widget? child;

  const MapView({super.key, this.child});

  @override
  State<MapView> createState() => MapViewState();

  static MapViewState? of(final context) {
    return context.findAncestorStateOfType<MapViewState>();
  }
}

class MapViewState extends State<MapView> {
  late final MapLibreMapController mapController;

  void _mapCreated(final MapLibreMapController controller) async {
    mapController = controller;
  }

  @override
  void dispose() {
    mapController.dispose();
    return super.dispose();
  }

  Future<bool?> _animateCamera(final CameraUpdate cameraUpdate) {
    return mapController.animateCamera(
      cameraUpdate,
      duration: const Duration(seconds: 1),
    );
  }

  Future<void> zoomIn() async {
    final cameraUpdate = CameraUpdate.zoomIn();
    await _animateCamera(cameraUpdate);
  }

  Future<void> zoomOut() async {
    final cameraUpdate = CameraUpdate.zoomOut();
    await _animateCamera(cameraUpdate);
  }

  Future<void> rotateNorth() async {
    final cameraUpdate = CameraUpdate.bearingTo(0);
    await _animateCamera(cameraUpdate);
  }

  @override
  Widget build(final context) {
    return Stack(
      children: [
        MapLibreMap(
          initialCameraPosition: const CameraPosition(target: LatLng(0, 0)),
          compassEnabled: false,
          myLocationEnabled: true,
          myLocationRenderMode: MyLocationRenderMode.gps,
          myLocationTrackingMode: MyLocationTrackingMode.trackingGps,
          styleString: 'assets/map/style.json',
          onMapCreated: _mapCreated,
          onStyleLoadedCallback: _mapLoaded,
        if (widget.child != null) widget.child!,
      ],
    );
  }
}

Then for example for zoom buttons I created following widget:

class ZoomControls extends StatelessWidget {
  const ZoomControls({super.key});

  @override
  Widget build(final context) {
    return _ControlButtonsSection(
      controls: [
        _ControlButton(
          onPressed: MapView.of(context)?.zoomIn,
          icon: const Icon(Icons.add),
        ),
        _ControlButton(
          onPressed: MapView.of(context)?.zoomOut,
          icon: const Icon(Icons.remove),
        ),
      ],
    );
  }
}

And my app.dart:

class App extends StatelessWidget {
  const App({ Key? key }) : super(key: key);

  @override
  Widget build(BuildContext context){
    return MapView(
      child: Stack(
        children: [
          Align(
            alignment: Alignment.bottomRight,
            child: ZoomControls(),
          )
        ],
      ),
    )
  }
}

Describe alternatives you've considered

No response

Additional context

No response

@XanderD99 XanderD99 added the enhancement New feature or request label Nov 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant