flutter_map icon indicating copy to clipboard operation
flutter_map copied to clipboard

onMoveStart onMoveEnd implementation

Open yostrokon opened this issue 8 years ago • 8 comments

Hello again, There are two map event leaflet js on the map - onMoveStart and onMoveEnd. Because i'm using big DB - about two dozen M markers i use them in them web client to get an initial bounds and zoom before drag and at the end to call backend and get lists with the markers that have to stay, be removed from the memory or add to the new screen. Is there a chance to have these events "from the box" with callback at the map that of course to be corresponded with the mapcontroller for zoom and move or should be better to implement workaround monkey job wrap with gesturedetector using onDrag events?

have some relation to https://github.com/apptreesoftware/flutter_map/issues/138 but move and zoom are required too.

yostrokon avatar Oct 26 '18 16:10 yostrokon

+1, I'd like to see this feature

vinicentus avatar Nov 05 '18 13:11 vinicentus

I need something like that too. I want to fetch some data from the server according to the map position every time the map stop moving.

GENL avatar Feb 09 '19 21:02 GENL

I know the issue has been open for a while, yet I allow myself to post my onMoveEnd workaround here, hoping to save time for the ones having a similar problem in the future :)

So in my case I'm using Timer to detect when the position hasn't changed for a while.

First, I create a variable to store my Timer:

Timer _mapMovementTimer;

Then, within the onPositionChanged callback:

onPositionChanged: (MapPosition position, bool hasGesture, bool fromUser) {
  if(_mapMovementTimer != null)
    _mapMovementTimer.cancel();

  _mapMovementTimer = Timer(Duration(milliseconds: 750), () {
    // Here, the map didn't moved for 750ms
    BlocProvider.of<PointsBloc>(context).requestPointsInZone(position.bounds);
  });
},

The counter is canceled and restarted each time the user pan or zoom the map. If onPositionChanged callback is not called for 750ms (if the map didn't moved), then I request my points in the given zone. You can change the Duration of the Timer to have a quicker/slower reaction.

SebastienAwali avatar Jul 01 '19 13:07 SebastienAwali

+1 Just stumbled upon this and it's exactly what I need. Any chance a onPositionChangeStopped callback could be added in the next public release?

dimitristaufer avatar Oct 25 '19 07:10 dimitristaufer

I did not understand exactly what John has suggested, but maybe you guys can help: https://github.com/johnpryan/flutter_map/issues/252#issuecomment-493557510

aytunch avatar Oct 25 '19 14:10 aytunch

I know the issue has been open for a while, yet I allow myself to post my onMoveEnd workaround here, hoping to save time for the ones having a similar problem in the future :)

So in my case I'm using Timer to detect when the position hasn't changed for a while.

First, I create a variable to store my Timer:

Timer _mapMovementTimer;

Then, within the onPositionChanged callback:

onPositionChanged: (MapPosition position, bool hasGesture, bool fromUser) {
  if(_mapMovementTimer != null)
    _mapMovementTimer.cancel();

  _mapMovementTimer = Timer(Duration(milliseconds: 750), () {
    // Here, the map didn't moved for 750ms
    BlocProvider.of<PointsBloc>(context).requestPointsInZone(position.bounds);
  });
},

The counter is canceled and restarted each time the user pan or zoom the map. If onPositionChanged callback is not called for 750ms (if the map didn't moved), then I request my points in the given zone. You can change the Duration of the Timer to have a quicker/slower reaction.

The problem with this solution comes when you want to center the map again and the user is still touching the screen but not moving the map.

nicowernli avatar Nov 15 '19 16:11 nicowernli

I ended up using the following workaround, maybe it works for you as well:

var mapPointers = 0;
var mapPosition;
var mapWidget = Listener(
  onPointerDown: (details) {
    if (mapPointers == 0) // onMoveStart
    mapPointers++;
  },
  onPointerUp: (details) {
    mapPointers--;
    if (mapPointers == 0) // onMoveEnd
  },
  behavior: HitTestBehavior.deferToChild,
  child: FlutterMap(
    options: new MapOptions(
      onPositionChanged: (position, gesture) {
        mapPosition = position;
      }
    )            
  )
);

bashennekam avatar Apr 29 '20 19:04 bashennekam

Check out: #719

There are a lot of issues which ask for onMoveStart / onMoveEnd callbacks, just listen to mapEventStream and filter these events (MapEventMoveStart / MapEventMoveEnd), however take care because Map may use fling animation if the velocity is high enough so the final location will be different from MapEventMoveEnd's targetLocation, so there are two options:

  • disable fling animation
  • filter MapEventFlingEnd and MapEventFlingNotStarted

maRci002 avatar Aug 06 '20 20:08 maRci002