Android / Google Maps API - 计算完整地图的宽度和高度(以像素为单位)

gen*_*ray 0 java android google-maps google-maps-api-3

所以我目前正在制作一个基于 GoogleMaps 的游戏。因为我想制作一个相机,所以我需要整个谷歌地图的像素大小。

这就是我的意思:

我实际上指的是尺寸

我已经尝试过了:

    // 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;
Run Code Online (Sandbox Code Playgroud)

但它只返回 null。片段有什么问题吗?还有其他方法可以计算谷歌地图的完整尺寸吗?或者这只是返回当前显示的地图部分的大小?

ant*_*nio 5

基于如果我在 onResume 中调用 getMeasuredWidth() 或 getWidth() 进行布局,它们返回 0你可以让它像这样工作:

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

        String widthAndHeight = width + " " + height;
    }
});
Run Code Online (Sandbox Code Playgroud)