elementHitTest method

  1. @override
bool elementHitTest(
  1. CircleMarker<R> element, {
  2. required Point<double> point,
  3. required LatLng coordinate,
})

Method invoked by hitTest for every element (each of elements in reverse order) that requires testing

Not all elements will require testing. For example, testing is skipped if a hit has already been found on another element, and the HitDetectableElement.hitValue is null on this element.

point (OffsetToPointExtension.toPoint) and coordinate (MapCamera.pointToLatLng) are provided for simplicity.

Avoid performing calculations that are not dependent on element. Instead, override hitTest, store the necessary calculation results in (late non-nullable) members, and call super.hitTest(position) at the end. To calculate the camera origin in this way, instead mix in HitTestRequiresCameraOrigin, which makes the origin available through the hitTestCameraOrigin member.

Should return whether an element has been hit.

Implementation

@override
bool elementHitTest(
  CircleMarker<R> element, {
  required Point<double> point,
  required LatLng coordinate,
}) {
  final circle = element; // Should be optimized out by compiler, avoids lint

  final center = camera.getOffsetFromOrigin(circle.point);
  final radius = circle.useRadiusInMeter
      ? (center -
              camera.getOffsetFromOrigin(
                  _distance.offset(circle.point, circle.radius, 180)))
          .distance
      : circle.radius;

  return pow(point.x - center.dx, 2) + pow(point.y - center.dy, 2) <=
      radius * radius;
}