与MapView对话的GoogleMapFragment

时间:2017-05-31 16:13:35

标签: android google-maps

我在AlertDialog中有一个GoogleMap。最近,我的MapView变为null,因此我发现Google使用异步方式获取地图。我更新了我的代码,但它没有用。 MapView始终为null。

final Dialog dialog = new Dialog(mainActivity);     
MapView mMapView = (MapView) dialog.findViewById(R.id.mapView);

MapsInitializer.initialize(mainActivity);

mMapView = (MapView) dialog.findViewById(R.id.mapView);
if (**mMapView** != null) {
    mMapView.onCreate(dialog.onSaveInstanceState());
    mMapView.onResume();// needed to get the map to display immediately
    OnMapReadyCallback onMapReadyCallback = new OnMapReadyCallback() {
        @Override
        public void onMapReady(final GoogleMap googleMap) {
            if (googleMap != null) {

                dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
                dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
                dialog.setContentView(R.layout.dialogmap);
                dialog.show(); googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
                if (myLocation != null) {

                    double latitude = myLocation.getLatitude();
                    double longitude = myLocation.getLongitude();
                    CameraPosition cameraPosition = new CameraPosition.Builder()
                            .target(new LatLng(latitude, longitude)).zoom(16.0F).build();
                    googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));

                    googleMap.setOnMarkerDragListener(new GoogleMap.OnMarkerDragListener() {
                        @Override
                        public void onMarkerDragStart(Marker arg0) {
                            // TODO Auto-generated method stub
                            Log.e("System out", "onMarkerDragStart..." + arg0.getPosition().latitude + "..." + arg0.getPosition().longitude);
                        }

                        @SuppressWarnings("unchecked")
                        @Override
                        public void onMarkerDragEnd(final Marker arg0) {
                            // TODO Auto-generated method stub
                            Log.e("System out", "onMarkerDragEnd..." + arg0.getPosition().latitude + "..." + arg0.getPosition().longitude);

                googleMap.animateCamera(CameraUpdateFactory.newLatLng(arg0.getPosition()));
                            if (dialog.findViewById(R.id.bar_save) != null) {
                                                                    dialog.findViewById(R.id.bar_save).findViewById(R.id.annuler).setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View view) {
                                                                                        localisation.setText(arg0.getPosition().latitude + " " + arg0.getPosition().longitude);
                                    }
                                });
                                dialog.findViewById(R.id.bar_save).findViewById(R.id.enregistrer).setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View view) {                                        

                                        dialog.dismiss();

                                    }
                                });
                            }
                        }

                        @Override
                        public void onMarkerDrag(Marker arg0) {
                            // TODO Auto-generated method stub
                            Log.i("System out", "onMarkerDrag...");
                        }
                    });
                     MarkerOptions marker0 = new MarkerOptions().position(new LatLng(latitude, longitude)).icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED)).title("Infraction");
                    marker0.draggable(true);
                    googleMap.addMarker(marker0);

                } 
            }
        }
    };
    mMapView.getMapAsync(onMapReadyCallback);

    // latitude and longitude

}else
    Toast.makeText(mainActivity,"MapView is null",Toast.LENGTH_SHORT).show();

}

即使我添加一个新方法来处理地图,MapView仍然是null。这是我在对话框中膨胀的XML视图。

<RelativeLayout
    android:id="@+id/mapHolder_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/legend" >

    <com.google.android.gms.maps.MapView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/mapView"
        android:background="@color/TealLight"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</RelativeLayout>

2 个答案:

答案 0 :(得分:0)

final删除Dialog或在全局级别声明。

答案 1 :(得分:0)

我认为你最好将DialogFragment(而不是Dialog)子类化,并使用它的生命周期回调来实现MapView,就像Google在普通片段中所做的那样。