Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8134629
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T10:06:10+00:00 2026-06-06T10:06:10+00:00

I am using the OSM for a mapping application where I rotate the map

  • 0

I am using the OSM for a mapping application where I
rotate the map in direction of travel as explained here Android Rotating MapView . This works well.

However, I haven’t yet managed to adjust the dispatchTouchEvent code
to counter the map rotation effect for the user touches (right now
when the map is rotated 90 degrees a user’s horizontal sweep will move
the map vertically etc). The sample code only offers the teaser:

   public boolean dispatchTouchEvent(MotionEvent ev) {
        // TODO: rotate events too
        return super.dispatchTouchEvent(ev);
    }

Any guidance would be appreciated.

And while I am at it – Is it still possible to position the zoom
controls separately, so that they do NOT rotate when the map rotates?
I read that the getZoomControls() is deprecated. (Why ?)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T10:06:12+00:00Added an answer on June 6, 2026 at 10:06 am

    I know it’s too late but it may help someone…

    import org.osmdroid.views.overlay.MyLocationOverlay;

    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.content.IntentFilter;
    import android.graphics.Canvas;
    import android.graphics.Matrix ;
    import android.hardware.Sensor;
    import android.os.BatteryManager;
    import android.util.AttributeSet;
    import android.view.MotionEvent;
    import android.view.View;
    import android.widget.RelativeLayout;
    /**
    * Rotate the Map in accordance to the movement of user and always point to North
    *
    */
    public class RotatingRelativeLayout extends RelativeLayout 
    {
        private Matrix mMatrix = new Matrix();
        private float[] mTemp = new float[2]; 
    private Context  context;
    private static final float SQ2 = 1.414213562373095f;
    public RotatingRelativeLayout(final Context pContext,
        final AttributeSet pAttrs)
    {
    super(pContext, pAttrs);
    
    this.context=pContext;
    
    }
    
    @Override
    protected void dispatchDraw(Canvas canvas) {
    
    long  rotateTime = MyLocationOverlay.getTimeOfMovement();
    float overlayBearing = MyLocationOverlay.getBearing();//this method returns current      bearing from OSM 
    long  currentTime = System.currentTimeMillis();
    long  diffTime = currentTime-rotateTime;
    /*
     * Here we rotate map in accordance with Compass to point always North
         *  
     */
    
     if(diffTime >= (40*1000 )){
    
        //isBearing=false;
        overlayBearing=0;
        canvas.rotate(overlayBearing, getWidth() * 0.5f, getHeight() * 0.5f);
    
    }
    else 
    /*
     * Rotate Map According to the user movement
     */
    canvas.rotate(-overlayBearing, getWidth() * 0.5f, getHeight() * 0.5f);
    canvas.getMatrix().invert(mMatrix);
    final float w = this.getWidth();
        final float h = this.getHeight();
    
        final float scaleFactor = (float)(Math.sqrt(h * h + w * w) / Math.min(w, h));
        canvas.scale(scaleFactor, scaleFactor, getWidth() * 0.5f, getHeight() * 0.5f);
        super.dispatchDraw(canvas);
    canvas.save(Canvas.MATRIX_SAVE_FLAG);
    canvas.restore();
    
    }
    
    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
    
    final int width = getWidth();
    final int height = getHeight();
    final int count = getChildCount();
    for (int i = 0; i < count; i++) {
    final View view = getChildAt(i);
    final int childWidth = view.getMeasuredWidth();
    final int childHeight = view.getMeasuredHeight();
    final int childLeft = (width - childWidth) / 2;
    final int childTop = (height - childHeight) / 2;
    
    view.layout(childLeft, childTop, childLeft + childWidth, childTop + childHeight);
    
    }
    
    }
    
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    
    int w = getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec);
    int h = getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec);
    int sizeSpec;
    
    if (w > h) {
    
        sizeSpec = MeasureSpec.makeMeasureSpec((int) (w*SQ2), MeasureSpec.EXACTLY);
    
    } 
    else {
    
        sizeSpec = MeasureSpec.makeMeasureSpec((int) (h*SQ2), MeasureSpec.EXACTLY);
    
    }
    
    final int count = getChildCount();
    
    for (int i = 0; i < count; i++) {
     getChildAt(i).measure(sizeSpec, sizeSpec);
    
    }
    
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    
    }
    
    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
    
    final float[] temp = mTemp;
    temp[0] = event.getX();
    temp[1] = event.getY();
    mMatrix.mapPoints(temp);
    event.setLocation(temp[0], temp[1]);
    
    return super.dispatchTouchEvent(event);
    
    }
    
    
        }
    

    now just use this Relative Layout in your xml like this:-

    <YourPackageName.RotatingRelativeLayout     android:layout_width="fill_parent"  android:layout_height="fill_parent"  android:id="@+id/rotating_layout" android:layout_marginBottom="40dip"/>    
    

    And add Map View Programatially like this:-

       // Find target container
        final RelativeLayout rl=(RelativeLayout)mParent.findViewById(R.id.rotating_layout);
    
        // Create rotator
        mRotator=new RotatingRelativeLayout(mParent, null);
    
        // Add map to the rotating layout
        // Add rotator to the screen
        rl.addView(mMap,new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,  LayoutParams.FILL_PARENT));
    

    here mParent is Context. And one more thing if you encounter Image Pixelation prob you just have to use it

        //Paint distortion handling..
    p.setFilterBitmap(true);
    

    Hope i explained it as good as i could…. feel free to ask if you find problem understanding it.

    Thanks.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using Android TelephonyManager an application can obtain the state of data activity over the
I'm trying to geocode using openstreetmap geocoding service from my asp.net application. OSM geocoding
i am develpoing a map application with osm. i want to create some marker
I'd previously managed to incorporate OpenStreetMaps into my application using osmdroid-android-3.0.1.jar with the great
I have exported a map to a .osm file (this is an xml). I
I'm using http://www.openstreetmap.org/ export utility to generate a .osm (xml map file) of an
I have an application with a map-client using OpenLayers . I want to use
I'm developing an application for measuring and storing running/cycling tracks using OSM/Google Maps integration.
I have a requirement for building an Android mapping application onto which I will
Within my Android application I display some data within a map. To do that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.