0

I'm building an app with Flutter and am trying to listen to the pan gestures of the map to get the center of the map view and place a pin on the center. The code I've tried is as follows:

void _setPanGestureHandler({HereMapController mapController}) {
    
    _hereMapController.gestures.panListener = PanListener.fromLambdas(
        lambda_onPan: (GestureState gestureState, Point2D panStartPoint,
            Point2D panEndPoint, double panVelocity) {
      if (gestureState == GestureState.begin) {
        print("start pan");
      } else if (gestureState == GestureState.end) {
        var centerPointLat = _hereMapController.viewportSize.width / 2;
        var centerPointLong = _hereMapController.viewportSize.height / 2;
        GeoCoordinates geoCoordinates = _hereMapController
            .viewToGeoCoordinates(Point2D(centerPointLat, centerPointLong));
        if (geoCoordinates == null) {
          return;
        }
        _addPoiMapMarker(geoCoordinates);
        _getAddressForCoordinates(geoCoordinates);
      } else if (gestureState == GestureState.update) {
        print("pan updated");
      } else if (gestureState == GestureState.cancel) {
        print("pan cancelled");
      }
    });
  }

The code is part of the search_app example and I've just added a panGesture listener to it.

On panning, I get the following in the debug console

W/PanGestureDetector(21446): [WARN ] PanGestureDetector - Invalid panning of zero duration but nonzero length, skipping
W/PanGestureDetector(21446): [WARN ] PanGestureDetector - Invalid panning of zero duration but nonzero length, skipping

Please let me know how I can solve this problem.

1 Answers1

1

The code snippet from your question is not part of the search_app example, so I assume it is your code. However, what you are trying to do will not work properly:

  • _hereMapController.viewportSize is giving you the size in pixels of the map view. It is not providing geographic coordinates. Although you can use viewToGeoCoordinates() to convert pixel points to geographic coordinates, there is an easier way: The MapCamera gives you always the current center location for free via the targetCoordinates property.
  • The pan gesture handler is executed multiple times. It would not be advisable to call _addPoiMapMarker() each time the GestureState.end event occurs. Instead, you can reposition an existing marker by setting new coordinates.

From your code it looks like you want to get the address of the map view's center. I assume you do not want to recenter a marker each time the map has stopped moving. So, in your case it may be better to draw a fixed widget at the center of your map view, regardless of it's current coordinates - then it will not move or lag behind any events, e.g. you may get from a MapCameraObserver.

The log messages you are receiving are only warnings, so they should not have an effect on the pan gesture event you are receiving.

Nusatad
  • 3,231
  • 3
  • 11
  • 17
  • unfortunately, I cannot find targetCoordinates under MapCamera object. Also, how can I draw a pin statically over the center of the map? Do I use Stack to achieve this? I'm using the Explore edition's SDK – krishna ravichander May 31 '21 at 11:33
  • 1
    The targetCoordinates are part of the MapCameraState. _hereMapController.camera.state. targetCoordinates should do the trick. This should work perfectly fine with the Explore Edition. To draw a widget statically over another widget (like the map view) you can use a stack view, yes. It's basically independent of any HERE SDK component. – Nusatad May 31 '21 at 13:18