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

[BUG] onSymbolTapped not working #487

Open
ishafiul opened this issue Aug 20, 2024 · 2 comments
Open

[BUG] onSymbolTapped not working #487

ishafiul opened this issue Aug 20, 2024 · 2 comments
Labels
bug Something isn't working

Comments

@ishafiul
Copy link
Contributor

Platforms

all

Version of flutter maplibre_gl

0.20.0

Bug Description

I was trying to add a SymbolLayer to place some symbols. Now, I need to add a callback function to perform some operations when those symbols are tapped. However, for some reason, the tap event is not being registered. When I use controller.addSymbol, it works, but it doesn't work for controller.addSymbolLayer.

Steps to Reproduce

  1. setup MapLibreMap
  2. after map load add layers with symbol info
  3. call onSymbolTapped event the

Expected Results

When a symbol is tapped, the expected result is for the onSymbolTapped function to be called.

Actual Results

it will print tapped from controller.onSymbolTapped.add

Code Sample

  // Generate features for all markers
  final List<Map<String, dynamic>> features = markers.map((marker) {
    final coordinates = [marker.geometry.longitude, marker.geometry.latitude];

    // Ensure geometry coordinates are valid
    if (coordinates.contains(null)) {
      throw Exception("Invalid geometry coordinates");
    }

    return {
      'type': 'Feature',
      'geometry': {'type': 'Point', 'coordinates': coordinates},
      'properties': {
        'title': "...", // Custom properties for each marker
      },
    };
  }).toList();

// Add GeoJSON source
  await controller.addSource(
    'markerSource',
    GeojsonSourceProperties(
      data: {
        'type': 'FeatureCollection',
        'features': features,
      },
    ),
  );

// Generate a random string for the marker name
  final name = generateRandomString(5);

// Ensure base marker is created successfully
  await _baseMarker(controller: controller, name: name);

// Add the symbol layer
  await controller.addLayer(
    'markerSource', // Source ID
    'markerLayer', // Layer ID
    SymbolLayerProperties(
      iconImage: name,
      iconSize: 0.18,
      iconAnchor: 'bottom',
      textField: ' ',
      iconAllowOverlap: true,
    ),
    enableInteraction: true,
  );

  controller.onSymbolTapped.add(
        (argument) {
      print("tapped");
    },
  );
@ishafiul ishafiul added the bug Something isn't working label Aug 20, 2024
@Josh-Dovey
Copy link

Josh-Dovey commented Aug 21, 2024

Have you tried using onFeatureTapped? As you are using geoJSON this may be the method you are looking for.

onFeatureTapped works for all kinds of geoJSON features. So say you added geoJSON points, lines, polygons etc, the onFeatureTapped would run for all of those layers.

After adding your geoJSON layers to the map, run the following code to add tap events to your symbols.

controller.onFeatureTapped.add((dynamic feature, Point<double> point, LatLng latLng) async {

}

@ishafiul
Copy link
Contributor Author

Have you tried using onFeatureTapped? As you are using geoJSON this may be the method you are looking for.

onFeatureTapped works for all kinds of geoJSON features. So say you added geoJSON points, lines, polygons etc, the onFeatureTapped would run for all of those layers.

After adding your geoJSON layers to the map, run the following code to add tap events to your symbols.

controller.onFeatureTapped.add((dynamic feature, Point<double> point, LatLng latLng) async {

}

Thanks, it's working. Inside onFeatureTapped, queryRenderedFeatures is helping me solve this problem.

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

2 participants