Pinch to zoom with Osmdroid
Asked Answered
O

4

13

I've written a mapping application which has two activities, one activity displays a Google Maps view, the other displays the equivalent view using osmdroid 3.0.5 jar.

Until now I've been testing on emulators only and the functionality is completely identical in terms of area shown and overlay data. Now I have run the app on a real Gingerbread device, I notice that the Google Maps activity seems to support pinch to zoom, whilst the Osmdroid one doesn't.

I haven't written any code specific to pinch to zoom for the Google side or for Osmdroid. Both activities implement OnTouchListener. Both activities just have a stub for OnTouch like:

@Override
public boolean onTouch(View v, MotionEvent e) {
    // chain it for now
    return false;
}

My mapview in the Osmdroid activity is of the class: org.osmdroid.views.MapView

Does anybody know how to make pinch to zoom work with osmdroid or know of a sample application using osmdroid that I could study and adapt into my app?

Obryan answered 28/9, 2011 at 12:36 Comment(0)
E
5

Osmdroid has the functionality to zoom. You will need to setup a gesturelistener to check for the pinch action, at which point you should call the zoom function in osmdroid. I believe in osmdroid 3.0.5 it is something like

mOsm.getController.setZoomLevel(someNumber) (mOsm is an instance of the map view).

I have the zoom function working for the opposite pinch (you fingers start close together and then expand). I suggest using a MotionEvent (like you are currently doing) and doing something like this:

boolean finished = false;

@Override
public boolean onTouch(View v, MotionEvent e) {

    switch (e.getAction()) {

        case MotionEvent.ACTION_DOWN:
            finished = false;
            break;

        case MotionEvent.ACTION_MOVE:
            finished = false;
            break;

        case MotionEvent.ACTION_UP:
            //action is finishing at this point, so now I can do my refreshActionOnMap();
            if (!finished) 
                refreshActionOnMap();
            break;
    }
    return true;
}

The code I have added deals with the pinch - the finished boolean is something I implemented in my program to figure out when to refresh the map. That should help you out more.

Here is a further explanation of this.

If you are looking for something different, then try reading here. Android has been supporting the pinch action since June 2010 apparently.

Elliot answered 29/9, 2011 at 3:24 Comment(3)
Thanks for the information. I think the links will prove useful. I guess that Google Maps' activity (MapsActivity) must implement the pinch behind the scenes. Of course their code is closed source, so it looks like I'll have to to a bit of work instead of just copying their implementation. +1 for the links.Obryan
I experimented with it in my program, and the code above works for me. If that's more helpful then ++!Elliot
It would have worked straight away if I wasn't such an IDIOT! I missed out the line mMapVw.setMultiTouchControls(true); I'm certainly not going to answer my own question with that. So to stop others wasting their time and in gratitude for you getting me thinking, I'll accept your answer.Obryan
G
40
MapView.setBuiltInZoomControls(true);
MapView.setMultiTouchControls(true);
Gerry answered 20/1, 2012 at 13:35 Comment(3)
I have found that it looks like it is zooming fine when I am moving my two fingers, but as soon as I lift them the map jumps to a different position. @Warpzit Is this what you meant by "implemented poorly"?Finsteraarhorn
Works great on my Defy Plus. I have yet to try this on other devices.Charlyncharm
samsung s2 kitkat working perfectly. tks. UPVOTED!Serriform
E
5

Osmdroid has the functionality to zoom. You will need to setup a gesturelistener to check for the pinch action, at which point you should call the zoom function in osmdroid. I believe in osmdroid 3.0.5 it is something like

mOsm.getController.setZoomLevel(someNumber) (mOsm is an instance of the map view).

I have the zoom function working for the opposite pinch (you fingers start close together and then expand). I suggest using a MotionEvent (like you are currently doing) and doing something like this:

boolean finished = false;

@Override
public boolean onTouch(View v, MotionEvent e) {

    switch (e.getAction()) {

        case MotionEvent.ACTION_DOWN:
            finished = false;
            break;

        case MotionEvent.ACTION_MOVE:
            finished = false;
            break;

        case MotionEvent.ACTION_UP:
            //action is finishing at this point, so now I can do my refreshActionOnMap();
            if (!finished) 
                refreshActionOnMap();
            break;
    }
    return true;
}

The code I have added deals with the pinch - the finished boolean is something I implemented in my program to figure out when to refresh the map. That should help you out more.

Here is a further explanation of this.

If you are looking for something different, then try reading here. Android has been supporting the pinch action since June 2010 apparently.

Elliot answered 29/9, 2011 at 3:24 Comment(3)
Thanks for the information. I think the links will prove useful. I guess that Google Maps' activity (MapsActivity) must implement the pinch behind the scenes. Of course their code is closed source, so it looks like I'll have to to a bit of work instead of just copying their implementation. +1 for the links.Obryan
I experimented with it in my program, and the code above works for me. If that's more helpful then ++!Elliot
It would have worked straight away if I wasn't such an IDIOT! I missed out the line mMapVw.setMultiTouchControls(true); I'm certainly not going to answer my own question with that. So to stop others wasting their time and in gratitude for you getting me thinking, I'll accept your answer.Obryan
D
2

Is it possible not to zoom to the map center but to the middle of users two finger.

Dirtcheap answered 1/3, 2013 at 13:59 Comment(2)
have you find a good solution? if yes, please answer my question https://mcmap.net/q/902991/-osmdroid-and-pinchWoo
unfortunatel no but I asked the question here as well maybe one day it will be solved :) #15160044Dirtcheap
G
1

As shameus.burp said, the codeline-

mapView.setMultiTouchControls(true); 

Is working for me in order to pinch for zoom (check at 2021).

Notice that the function that shows zoom-controller (basically it is buttons of (-) and (+)) -

MapView.setBuiltInZoomControls(true);

is deprecated by osmdroid.

So instead, you can use the code line -

mapView.getZoomController().setVisibility(CustomZoomButtonsController.Visibility.ALWAYS);

By default (without adding the last code line) osmdroid is set the zoom-controller to show and fade. Equivalent to

CustomZoomButtonsController.Visibility.SHOW_AND_FADEOUT 

option.

Grammar answered 14/7, 2021 at 8:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.