Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MapController.setCenter offset #22

Closed
devemux86 opened this issue Apr 17, 2014 · 19 comments
Closed

MapController.setCenter offset #22

devemux86 opened this issue Apr 17, 2014 · 19 comments

Comments

@devemux86
Copy link

devemux86 commented Apr 17, 2014

When we call the new MapController.setCenter implemented at revision 423680b,
the map center is not at the GeoPoint we pass, but at an offset of half the screen down right.
Can you confirm that?

@kurtzmarc
Copy link
Contributor

I tried triggering it in OpenStreetMapViewer via a menu item and both setCenter() and animateTo() worked properly. However if it is called from onActivityCreated() there is an issue. I think it's because the MapView's width and height is zero because it hasn't had onLayout called yet. Is that where you are calling setCenter()? Or can you confirm that when you call it mMapView.getWidth() is zero?

@devemux86
Copy link
Author

devemux86 commented Apr 17, 2014

You are right, now there is need to check the map view size.
If map view has been layout then setCenter works correctly.

@devemux86 devemux86 reopened this Apr 17, 2014
@devemux86
Copy link
Author

devemux86 commented Apr 18, 2014

Sorry for the close / reopen, you can close the issue.

@kurtzmarc
Copy link
Contributor

I think I want to keep this open a little longer until I investigate the getWidth() issues and see if there is a nice solution for that.

@lcacheux
Copy link

I had this issue in my application which was previously using OSMDroid 4.1 and called setCenter in Fragment.onCreateView. To make it work properly with 4.2, I used something like this in onCreateView :

ViewTreeObserver vto = view.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
        mMapView.getController().setCenter(...);
    }
});

This way, setCenter is called only once the layout has been drawn and width/height has been calculated.

@ritschwumm
Copy link

i found another workaround which might be a little bit less invasive:

just delay the call to setCenter by one tick in the main loop so android gets a chance to do it's layouting before the actual call happens.

new Handler(Looper.getMainLooper()).post(
    new Runnable() {
        public void run() {
            mapController.setCenter(...);
        }
    }
);

thinking about a proper solution, would it make sense to make center the one property of a MapView which is used to calculate everything else from?

@devemux86
Copy link
Author

devemux86 commented May 15, 2014

I use externally the way with the OnGlobalLayoutListener.

But I like also the way Mapbox handle this issue internally.
At MapView.onSizeChanged they call MapController.mapViewLayedOut
where it executes any actions that have been recorded before map view is layed out.

@ziem
Copy link
Contributor

ziem commented Jun 2, 2014

Any progress?

@kjeremy
Copy link
Contributor

kjeremy commented Jun 2, 2014

I'm seeing this too.

@kurtzmarc
Copy link
Contributor

Please try out this patch and let me know if it works:
https://gist.github.com/kurtzmarc/f99b0bbbff46791d2168

@Sash0k
Copy link

Sash0k commented Jun 6, 2014

Please try out this patch and let me know if it works

Unfortunately, it works incorrect.

    GeoPoint defaultPoint = new GeoPoint(Const.DEF_LATITUDE, Const.DEF_LONGITURE);
    mMapView.getController().animateTo(defaultPoint);

In my project this code set defaultPoint in center screen before osmdroid 4.2. Now it set point at pixel (0,0) without patch and at pixel (screen.w, screen.h) with one.

@kurtzmarc
Copy link
Contributor

Sorry - I missed that method. I am going to commit a different approach that will keep track of the calls you make and "replay" them in order once the MapView has a layout.

kurtzmarc added a commit that referenced this issue Jun 6, 2014
…lay them until the MapView has a layout. Updates #22.
@Sash0k
Copy link

Sash0k commented Jun 7, 2014

Now it fixed. Thank you!

@kurtzmarc
Copy link
Contributor

This appears to be working properly.

kurtzmarc added a commit that referenced this issue Jun 23, 2014
…layout of the maps. Allow listeners to subscribe to first layout event. Change MapController to use this for determining first layout. Updates #22.
@courdi95
Copy link

Hello

I still have this problem (centering ot working) with 4.2 version. ....

Has someone a trick to manage that problem ?

@devemux86
Copy link
Author

devemux86 commented Jun 30, 2014

It's fixed in the source at repository, not released yet as new version.
If you play with the 4.2 take a look at the OnGlobalLayoutListener solution.

@courdi95
Copy link

courdi95 commented Jul 9, 2014

I have implemented this solution with addOnGlobalLayoutListener. The trouble now is that the map is centering itself and I can not move it (it comes back) ! How can i remove the listener after the first centering ?

@devemux86
Copy link
Author

devemux86 commented Jul 9, 2014

You can try this:

mapView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN)
            mapView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
        else
            mapView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
        ...
    }
});

@courdi95
Copy link

courdi95 commented Jul 9, 2014

I finally found how tout manage that.

After centering i added this code in onGlobalLayout :
///////////////
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {

  •       mMapView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
    
  •   } else {
    
  •       mMapView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
    
  •   }
    
    ///////////////

This will remove the listener , :-)

mar-v-in pushed a commit to microg/android_external_osmdroid that referenced this issue Aug 22, 2014
mar-v-in pushed a commit to microg/android_external_osmdroid that referenced this issue Aug 22, 2014
mar-v-in pushed a commit to microg/android_external_osmdroid that referenced this issue Aug 22, 2014
…layout of the maps. Allow listeners to subscribe to first layout event. Change MapController to use this for determining first layout. Updates osmdroid#22.
garvankeeley added a commit to garvankeeley/MozStumbler that referenced this issue Sep 5, 2014
Setting initial pan and zoom requires a delay
garvankeeley added a commit to garvankeeley/MozStumbler that referenced this issue Sep 5, 2014
Setting initial pan and zoom requires a delay
jlhostyn added a commit to CMPUT301W15T03/Team3ScandalouStopwatch that referenced this issue Apr 1, 2015
…ks when GPS is disabled as well. There is a bug where the map center should default to the a certain location but it is always centered to the upper left. See this: osmdroid/osmdroid#22 (comment) for details. Furthermore, after the new home location is set it is not being updated in the user list until the app is closed and opened again. This is the issue we had before for GPS. I think I'll rename the activity to SetHomeLocationActivity and make separate ones for destinations and expenses.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

8 participants