Giter Site home page Giter Site logo

Comments (19)

kurtzmarc avatar kurtzmarc commented on July 24, 2024

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?

from osmdroid.

devemux86 avatar devemux86 commented on July 24, 2024

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

from osmdroid.

devemux86 avatar devemux86 commented on July 24, 2024

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

from osmdroid.

kurtzmarc avatar kurtzmarc commented on July 24, 2024

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.

from osmdroid.

lcacheux avatar lcacheux commented on July 24, 2024

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.

from osmdroid.

ritschwumm avatar ritschwumm commented on July 24, 2024

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?

from osmdroid.

devemux86 avatar devemux86 commented on July 24, 2024

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.

from osmdroid.

ziem avatar ziem commented on July 24, 2024

Any progress?

from osmdroid.

kjeremy avatar kjeremy commented on July 24, 2024

I'm seeing this too.

from osmdroid.

kurtzmarc avatar kurtzmarc commented on July 24, 2024

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

from osmdroid.

Sash0k avatar Sash0k commented on July 24, 2024

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.

from osmdroid.

kurtzmarc avatar kurtzmarc commented on July 24, 2024

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.

from osmdroid.

Sash0k avatar Sash0k commented on July 24, 2024

Now it fixed. Thank you!

from osmdroid.

kurtzmarc avatar kurtzmarc commented on July 24, 2024

This appears to be working properly.

from osmdroid.

courdi95 avatar courdi95 commented on July 24, 2024

Hello

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

Has someone a trick to manage that problem ?

from osmdroid.

devemux86 avatar devemux86 commented on July 24, 2024

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.

from osmdroid.

courdi95 avatar courdi95 commented on July 24, 2024

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 ?

from osmdroid.

devemux86 avatar devemux86 commented on July 24, 2024

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);
        ...
    }
});

from osmdroid.

courdi95 avatar courdi95 commented on July 24, 2024

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 , :-)

from osmdroid.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.