[Mapsforge] Listener for zoom controls or any zoom changes

493 views
Skip to first unread message

George Bezerra

unread,
Mar 20, 2017, 8:57:15 AM3/20/17
to mapsforge-dev
Hello, I am developing an Android app with Mapsforge. Currently it starts by default in a "navigation" mode, where the map pans and zooms automatically according to the driver's position and some other things. When the user interacts with the map, it goes to a "manual" mode where no automatic zooming and panning happens, until the user clicks on a button to go back to navigation mode.

The problem I have is: I can't set up a listener for ZoomControls to know when the user changed zoom via zoom controls. I have a GestureDetector that already detects drag movements and double-tap movements to call a handler, but I found no way to set up a listener to the zoom controls, since I'd like to call the same handler whenever the user changed zoom via zoom controls as well.

I've tried setting onClick and OnTouch listeners like this with no results:

mapView.getMapZoomControls().setOnClickListener(new View.OnClickListener() {
   
@Override
   
public void onClick(View v) {
       
Log.d(TAG, "onClick");
   
}
});
mapView
.getMapZoomControls().setOnTouchListener(new View.OnTouchListener() {
   
@Override
   
public boolean onTouch(View v, MotionEvent event) {
       
Log.d(TAG, "onTouch");
       
return true;
   
}
});


What would solve my problem:
  1. A way to set a listener to zoom controls; or
  2. A way to set a listener for ANY zoom changes, and then I check internally if the zoom was generated programatically by my navigation logic or not (I've tried that as well with no success).
I don't know if that's basic or not, but I'm stuck with this and decided to seek help.

Thank you in advance!

Emux

unread,
Mar 20, 2017, 9:20:38 AM3/20/17
to mapsfo...@googlegroups.com
Usually the most convenient solution is to use your own zoom controls (as buttons above MapView), so you can handle them accordingly (see RotateMapViewer for an implementation).

To listen for map changes (pan, zoom) you can use an Observer to MapViewPosition, like:
mapView.getModel().mapViewPosition.addObserver(Observer);

--
Emux

George Bezerra

unread,
Mar 20, 2017, 9:56:46 AM3/20/17
to mapsforge-dev
The Observer solution worked for me, I haven't noticed that method in MapViewPosition before. Problem solved, thank you!
Reply all
Reply to author
Forward
0 new messages