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

时间:2017-02-15 18:14:00

标签: java android google-maps google-maps-api-3

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

这就是我的意思:

Sizes i actually mean

我已经尝试过:

    // 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;

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

1 个答案:

答案 0 :(得分:3)

基于If I call getMeasuredWidth() or getWidth() for layout in onResume they return 0,你可以这样工作:

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

        String widthAndHeight = width + " " + height;
    }
});