mapbox / mapbox/mapbox-maps-flutter
Tapping on an unclustered layer item when using clusters
Nobody has claimed this yet.
- Dominant language
- Dart
- Stars
- 380
- Forks
- 204
- PR merge metrics
- No merged PRs in 30d
Description
Has anyone found out a way when using clustering to be able to tap a unclustered item? I have tried everything and I can't figure out how to be able to make it so I can attach a method to a circle that is unclustered? I don't see a way to add clustering to point annotations I have tried but I am unable to get it to work for only the unclustered items of a group.
Here is my code:
void _addGeoJsonSourceWithClustering() async {
final mapClusterProvider = Provider.of<MapClusterProvider>(context, listen: false);
final geoJson = mapClusterProvider.createGeoJson();
String sourceId = "data";
Map<String, dynamic> sourceData = {
"type": "geojson",
"data": json.decode(geoJson),
"cluster": true,
"clusterRadius": 50,
"clusterMaxZoom": 14,
};
String encodedSourceData = jsonEncode(sourceData);
if (!(await mapboxMap?.style.styleSourceExists(sourceId) ?? true)) {
await mapboxMap?.style.addStyleSource(sourceId, encodedSourceData);
print('"data" source added to the map style');
_addClusteringLayers(sourceId);
}
}
void _addClusteringLayers(String sourceId) async {
// Add clusters layer
var layer = '''
{
"id": "clusters",
"type": "circle",
"source": "$sourceId",
"filter": ["has", "point_count"],
"paint": {
"circle-color": [
"step",
["get", "point_count"],
"#51bbd6",
100,
"#f1f075",
750,
"#f28cb1"
],
"circle-radius": [
"step",
["get", "point_count"],
20,
100,
30,
750,
40
]
}
}
''';
await mapboxMap?.style.addStyleLayer(layer, null);
print('"clusters" layer added to the map style');
// Add cluster count layer
var clusterCountLayer = '''
{
"id": "cluster-count",
"type": "symbol",
"source": "$sourceId",
"filter": ["has", "point_count"],
"layout": {
"text-field": "{point_count_abbreviated}",
"text-font": ["DIN Offc Pro Medium", "Arial Unicode MS Bold"],
"text-size": 12
}
}
''';
await mapboxMap?.style.addStyleLayer(clusterCountLayer, null);
print('"cluster-count" layer added to the map style');
// Add unclustered point layer
var unclusteredLayer = '''
{
"id": "unclustered-point",
"type": "circle",
"source": "$sourceId",
"filter": ["!", ["has", "point_count"]],
"paint": {
"circle-color": "#f62323",
"circle-radius": 40,
"circle-stroke-width": 1,
"circle-stroke-color": "#fff"
}
}
''';
await mapboxMap?.style.addStyleLayer(unclusteredLayer, null);
print('"unclustered-point" layer added to the map style');
}
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by tracing _addGeoJsonSourceWithClustering and _addClusteringLayers, especially the unclustered-point layer and its source/filter configuration. Check how this Flutter Mapbox setup exposes layer interaction, then verify that an unclustered item can trigger the intended tap behavior without affecting clustered items.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- dart, flutter
- Domain
- mobile-dev
- Issue type
- Feature
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100