0

So im currently making an GoogleMaps based game. Because i wanted to make an camera, i need the size of the whole google map in pixel.

Heres what i mean :

Sizes i actually mean

I already tried :

    // pre generated by android studios
    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);

    mapFragment.getMapAsync(this);


    int width = mapFragment.getView().getMeasuredWidth();

    int height = mapFragment.getView().getMeasuredHeight();


    String widthAndHeight = width+" "+height;

But it only returns null. Whats wrong with the snippet? Any other ways to calculate the full size of the google map ? Or does this just returns the size of the part of the map, which is currently displayed ?

genaray
  • 1,080
  • 1
  • 9
  • 30

1 Answers1

5

Based on If I call getMeasuredWidth() or getWidth() for layout in onResume they return 0 you can get it working like this:

mapView = mapFragment.getView();
mapView.post(new Runnable() {
    @Override
    public void run() {
        int width = mapView.getMeasuredWidth();
        int height = mapView.getMeasuredHeight();

        String widthAndHeight = width + " " + height;
    }
});
Community
  • 1
  • 1
antonio
  • 18,044
  • 4
  • 45
  • 61