Skip to content

MapEventClick does not give correct Offset #476

@jobes

Description

@jobes

Platforms

Android

Version of flutter-maplibre

maplibre: ^0.3.3+1

Bug Description

When clicking on map, i got event from onEvent. If this event is MapEventClick, and devicePixelRatio is 1 then i got all my features on that point. But when my devicePixelRatio is higher, then i got features from other place on map.

Steps to Reproduce

click on map, check features where clicked, with devicePixelRatio that is not 1 i got wrong features

Expected Results

I should got correct features for point. I suspect

screenPoint: screenLocation.toOffset() / pixelRatio,
that is should use multiply instead of divide.

Actual Results

I got wrong features. I can workaround it by calling

final newPoint = Offset(
                      event.screenPoint.dx /
                          MediaQuery.of(context).devicePixelRatio /
                          MediaQuery.of(context).devicePixelRatio,
                      event.screenPoint.dy /
                          MediaQuery.of(context).devicePixelRatio /
                          MediaQuery.of(context).devicePixelRatio,
                    );

so it is square wrong. Most likely / is used instead of *

Code Sample

working code with workaround, newPoint transformation needed.

onEvent: (event) async {
                if (event is MapEventClick) {
                  debugPrint('Map clicked at ${event.point}');
                  if (_mapController != null) {
                    final newPoint = Offset(
                      event.screenPoint.dx /
                          MediaQuery.of(context).devicePixelRatio /
                          MediaQuery.of(context).devicePixelRatio,
                      event.screenPoint.dy /
                          MediaQuery.of(context).devicePixelRatio /
                          MediaQuery.of(context).devicePixelRatio,
                    );
                    final features = _mapController!.featuresAtPoint(newPoint);
                    for (var feature in features) {
                      final properties = feature.properties;
                      final featureType = properties['feature_type'];
                      final country = properties['country'];
                      final sourceId = properties['source_id'];
                      if (featureType != null ||
                          country != null ||
                          sourceId != null) {
                        debugPrint(
                          'Feature Clicked: info: $featureType, $country, $sourceId',
                        );
                      } else {
                        debugPrint('Feature Clicked: $feature');
                      }
                    }
                  }
                }
              }

Sub-issues

Metadata

Metadata

Assignees

No one assigned

    Labels

    androidAndroid specific issuebugSomething isn't working

    Projects

    Status

    Done

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions