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

addOnMapClickListener not defined #765

Open
saghro opened this issue Nov 11, 2024 · 1 comment
Open

addOnMapClickListener not defined #765

saghro opened this issue Nov 11, 2024 · 1 comment

Comments

@saghro
Copy link

saghro commented Nov 11, 2024

Error: Method addOnMapClickListener not defined for class MapController.

Environment
Flutter version: 3.24.4
Mapbox Maps Flutter SDK version: 2.3.0
Platform: iOS 13.0

Expected Behavior
The map should listen for click events and trigger the _handleMapClick callback when the user taps on the map.

Initialization with Click Listener

void initialize() {
  try {
    // Configuring basic gesture settings for the map
    mapboxMap.gestures.updateSettings(
      GesturesSettings(
        rotateEnabled: true,
        pinchToZoomEnabled: true,
        scrollEnabled: true,
        doubleTapToZoomInEnabled: true,
        doubleTouchToZoomOutEnabled: true,
      ),
    );
    
    // Adding the correct listener for map clicks
    mapboxMap.addOnMapClickListener(_handleMapClick);
  } catch (e) {
    debugPrint('Error initializing map click handler: $e');
  }
}

void dispose() {
  try {
    // Properly removing the listener on disposal
    mapboxMap.removeOnMapClickListener(_handleMapClick);
  } catch (e) {
    debugPrint('Error disposing map click handler: $e');
  }
}

// Callback function to handle map clicks
void _handleMapClick(Point<double> point) {
  // Handle the map click event here
  debugPrint('Map clicked at: $point');
}
@evil159
Copy link
Contributor

evil159 commented Nov 13, 2024

Hi @saghro, you can subscribe to map tap(click) events by providing your callback function to onTapListener constructor parameter of MapWidget, please refer to gestures_example.dart for more details.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants