不调用GoogleMap.setOnMapLoadedCallback [英] GoogleMap.setOnMapLoadedCallback is not called

查看:130
本文介绍了不调用GoogleMap.setOnMapLoadedCallback的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用googleMap和地理编码功能来获取位置.地方找到我正确的纬度,经度,格式化地址.会调用onMapReady函数,但根本不会调用onMapLoaded函数.

清单:

    <meta-data
        android:name="com.google.android.geo.API_KEY"
        android:value="..." />

我的Google gms版本:

implementation 'com.google.android.gms:play-services-auth:15.0.1'
implementation 'com.google.android.gms:play-services-gcm:15.0.1'
implementation 'com.google.android.gms:play-services-maps:15.0.1'
implementation 'com.google.android.gms:play-services-places:15.0.1'

我的customMapView布局:

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">

    <com.google.android.gms.maps.MapView
       android:id="@+id/map_view"
       android:layout_width="match_parent"
       android:layout_height="match_parent" />

    <TextView
       android:id="@+id/address_view"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:layout_weight="0"
       android:ellipsize="end"
       android:maxLines="1"
       android:padding="5dp"
       android:singleLine="true"
       android:visibility="visible"
       tools:text="21 Jump St, Atlanta GA 30311" />
</merge>

CustomMapView.java:

    public class CustomMapView extends LinearLayout {

    private MapView mapView;
    private TextView textView;

    public CustomMapView(Context context) {
        this(context, null);
    }

    public CustomMapView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initialize(context);
    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    public CustomMapView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initialize(context);
    }

    private void initialize(Context context) {
        setOrientation(LinearLayout.VERTICAL);
        LayoutInflater.from(context).inflate(R.layout.map_view, this, true);

        this.mapView = ViewUtil.findById(this, R.id.map_view);
        this.textView = ViewUtil.findById(this, R.id.address_view);
    }

    public void display(Place place) {

        this.mapView.onCreate(null);
        this.mapView.onResume();

        this.mapView.setVisibility(View.VISIBLE);
        this.imageView.setVisibility(View.GONE);

        this.mapView.getMapAsync(new OnMapReadyCallback() {
            @Override
            public void onMapReady(final @Nonnull GoogleMap googleMap) {

                googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(place.getLatLong(), 13));
                googleMap.addMarker(new MarkerOptions().position(place.getLatLong()));
                googleMap.setBuildingsEnabled(true);
                googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
                googleMap.getUiSettings().setAllGesturesEnabled(false);
                googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(place.getLatLong(), googleMap.getMaxZoomLevel() - 4));

                googleMap.setOnMapLoadedCallback(new GoogleMap.OnMapLoadedCallback() {
                    @Override
                    public void onMapLoaded() {

                        googleMap.snapshot(new GoogleMap.SnapshotReadyCallback() {
                            @Override
                            public void onSnapshotReady(Bitmap bitmap) {

                               // it is not called
                            }
                        });
                    }
                });
            }
        });

        this.textView.setText(place.getDescription());

    }

}

解决方案

尝试以下代码,您尚未调用MapView的正确生命周期方法. 文档

您自定义View:

public class CustomMapView extends LinearLayout {

private MapView mapView;
private TextView textView;

public CustomMapView(Context context) {
    this(context, null);
}

public CustomMapView(Context context, AttributeSet attrs) {
    super(context, attrs);
    initialize(context);
}

public void onCreate(Bundle var1) {
    mapView.onCreate(var1);
}

public void onResume() {
    mapView.onResume();
}

public void onPause() {
    mapView.onPause();
}

public void onStart() {
    mapView.onStart();
}

public void onStop() {
    mapView.onStop();
}

public void onDestroy() {
    mapView.onDestroy();
}

public void onLowMemory() {
    mapView.onLowMemory();
}

public void onSaveInstanceState(Bundle var1) {
    mapView.onSaveInstanceState(var1);
}

private void initialize(Context context) {
    setOrientation(LinearLayout.VERTICAL);
    LayoutInflater.from(context).inflate(R.layout.map_view, this, true);

    this.mapView = ViewUtil.findById(this, R.id.map_view);
    this.textView = ViewUtil.findById(this, R.id.address_view);
}

public void display(Place place) {
    this.mapView.setVisibility(View.VISIBLE);
    this.imageView.setVisibility(View.GONE);

    this.mapView.getMapAsync(new OnMapReadyCallback() {
        @Override
        public void onMapReady(final @Nonnull GoogleMap googleMap) {

            googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(place.getLatLong(), 13));
            googleMap.addMarker(new MarkerOptions().position(place.getLatLong()));
            googleMap.setBuildingsEnabled(true);
            googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
            googleMap.getUiSettings().setAllGesturesEnabled(false);
            googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(place.getLatLong(), googleMap.getMaxZoomLevel() - 4));

            googleMap.setOnMapLoadedCallback(new GoogleMap.OnMapLoadedCallback() {
                @Override
                public void onMapLoaded() {

                    googleMap.snapshot(new GoogleMap.SnapshotReadyCallback() {
                        @Override
                        public void onSnapshotReady(Bitmap bitmap) {

                            // it is not called
                        }
                    });
                }
            });
        }
    });

    this.textView.setText(place.getDescription());
}

您的活动

public class MainActivity extends Activity {
    private CustomMapView customView;

    @Override
    public void onCreate(Bundle var1) {
        super.onCreate(var1);

        customView = ViewUtil.findById(this, R.id.custom_view_id);
        customView.onCreate(var1);
    }

    @Override
    public void onResume() {
        super.onResume();
        customView.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
        customView.onPause();
    }

    @Override
    public void onStart() {
        super.onStart();
        customView.onStart();
    }

    @Override
    public void onStop() {
        super.onStop();
        customView.onStop();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        customView.onDestroy();
    }

    @Override
    public void onLowMemory() {
        super.onLowMemory();
        customView.onLowMemory();
    }

    @Override
    public void onSaveInstanceState(Bundle var1) {
        super.onSaveInstanceState(var1);
        customView.onSaveInstanceState(var1);
    }
}

I want to get location with using googleMap and geocode features. Place find my right latitude,longitude, formatted address. onMapReady function is called but onMapLoaded function is not called at all.

Manifest:

    <meta-data
        android:name="com.google.android.geo.API_KEY"
        android:value="..." />

My google gms versions:

implementation 'com.google.android.gms:play-services-auth:15.0.1'
implementation 'com.google.android.gms:play-services-gcm:15.0.1'
implementation 'com.google.android.gms:play-services-maps:15.0.1'
implementation 'com.google.android.gms:play-services-places:15.0.1'

My customMapView layout:

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">

    <com.google.android.gms.maps.MapView
       android:id="@+id/map_view"
       android:layout_width="match_parent"
       android:layout_height="match_parent" />

    <TextView
       android:id="@+id/address_view"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:layout_weight="0"
       android:ellipsize="end"
       android:maxLines="1"
       android:padding="5dp"
       android:singleLine="true"
       android:visibility="visible"
       tools:text="21 Jump St, Atlanta GA 30311" />
</merge>

CustomMapView.java:

    public class CustomMapView extends LinearLayout {

    private MapView mapView;
    private TextView textView;

    public CustomMapView(Context context) {
        this(context, null);
    }

    public CustomMapView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initialize(context);
    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    public CustomMapView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initialize(context);
    }

    private void initialize(Context context) {
        setOrientation(LinearLayout.VERTICAL);
        LayoutInflater.from(context).inflate(R.layout.map_view, this, true);

        this.mapView = ViewUtil.findById(this, R.id.map_view);
        this.textView = ViewUtil.findById(this, R.id.address_view);
    }

    public void display(Place place) {

        this.mapView.onCreate(null);
        this.mapView.onResume();

        this.mapView.setVisibility(View.VISIBLE);
        this.imageView.setVisibility(View.GONE);

        this.mapView.getMapAsync(new OnMapReadyCallback() {
            @Override
            public void onMapReady(final @Nonnull GoogleMap googleMap) {

                googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(place.getLatLong(), 13));
                googleMap.addMarker(new MarkerOptions().position(place.getLatLong()));
                googleMap.setBuildingsEnabled(true);
                googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
                googleMap.getUiSettings().setAllGesturesEnabled(false);
                googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(place.getLatLong(), googleMap.getMaxZoomLevel() - 4));

                googleMap.setOnMapLoadedCallback(new GoogleMap.OnMapLoadedCallback() {
                    @Override
                    public void onMapLoaded() {

                        googleMap.snapshot(new GoogleMap.SnapshotReadyCallback() {
                            @Override
                            public void onSnapshotReady(Bitmap bitmap) {

                               // it is not called
                            }
                        });
                    }
                });
            }
        });

        this.textView.setText(place.getDescription());

    }

}

解决方案

Try this code, You haven't called right lifecycle methods of the MapView. It's already explained in the docs

You custom View:

public class CustomMapView extends LinearLayout {

private MapView mapView;
private TextView textView;

public CustomMapView(Context context) {
    this(context, null);
}

public CustomMapView(Context context, AttributeSet attrs) {
    super(context, attrs);
    initialize(context);
}

public void onCreate(Bundle var1) {
    mapView.onCreate(var1);
}

public void onResume() {
    mapView.onResume();
}

public void onPause() {
    mapView.onPause();
}

public void onStart() {
    mapView.onStart();
}

public void onStop() {
    mapView.onStop();
}

public void onDestroy() {
    mapView.onDestroy();
}

public void onLowMemory() {
    mapView.onLowMemory();
}

public void onSaveInstanceState(Bundle var1) {
    mapView.onSaveInstanceState(var1);
}

private void initialize(Context context) {
    setOrientation(LinearLayout.VERTICAL);
    LayoutInflater.from(context).inflate(R.layout.map_view, this, true);

    this.mapView = ViewUtil.findById(this, R.id.map_view);
    this.textView = ViewUtil.findById(this, R.id.address_view);
}

public void display(Place place) {
    this.mapView.setVisibility(View.VISIBLE);
    this.imageView.setVisibility(View.GONE);

    this.mapView.getMapAsync(new OnMapReadyCallback() {
        @Override
        public void onMapReady(final @Nonnull GoogleMap googleMap) {

            googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(place.getLatLong(), 13));
            googleMap.addMarker(new MarkerOptions().position(place.getLatLong()));
            googleMap.setBuildingsEnabled(true);
            googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
            googleMap.getUiSettings().setAllGesturesEnabled(false);
            googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(place.getLatLong(), googleMap.getMaxZoomLevel() - 4));

            googleMap.setOnMapLoadedCallback(new GoogleMap.OnMapLoadedCallback() {
                @Override
                public void onMapLoaded() {

                    googleMap.snapshot(new GoogleMap.SnapshotReadyCallback() {
                        @Override
                        public void onSnapshotReady(Bitmap bitmap) {

                            // it is not called
                        }
                    });
                }
            });
        }
    });

    this.textView.setText(place.getDescription());
}

Your Activity

public class MainActivity extends Activity {
    private CustomMapView customView;

    @Override
    public void onCreate(Bundle var1) {
        super.onCreate(var1);

        customView = ViewUtil.findById(this, R.id.custom_view_id);
        customView.onCreate(var1);
    }

    @Override
    public void onResume() {
        super.onResume();
        customView.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
        customView.onPause();
    }

    @Override
    public void onStart() {
        super.onStart();
        customView.onStart();
    }

    @Override
    public void onStop() {
        super.onStop();
        customView.onStop();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        customView.onDestroy();
    }

    @Override
    public void onLowMemory() {
        super.onLowMemory();
        customView.onLowMemory();
    }

    @Override
    public void onSaveInstanceState(Bundle var1) {
        super.onSaveInstanceState(var1);
        customView.onSaveInstanceState(var1);
    }
}

这篇关于不调用GoogleMap.setOnMapLoadedCallback的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆