【问题标题】:Show Google Map in Material Dialog Android在材料对话框 Android 中显示 Google 地图
【发布时间】:2018-05-15 03:09:47
【问题描述】:

我想在自定义对话中显示谷歌地图。但我无法从对话类访问 SupportFragmentManager...

这是我的代码....

public class AddressViewDialog extends MaterialDialog.Builder implements OnMapReadyCallback {

private MaterialDialog reviewDialog;
private double latitude = 23.0;
private double longitude = 90.0;
private SupportMapFragment mMapFragment;
private GoogleMap mMap;

public AddressViewDialog(Context context) {
    super(context);

    this.context = context;
    initializeView();
}

public void initializeView() {

    reviewDialog = new MaterialDialog.Builder(this.getContext())
            .title(R.string.request_to_introduce)
            .customView(R.layout.dialog_pending_introducer_review, true)
            .show();

    View v = reviewDialog.getCustomView();

    mMapFragment = (SupportMapFragment) this.getChildFragmentManager()
            .findFragmentById(R.id.fragment);
    mMapFragment.getView().setVisibility(View.GONE);

    if (latitude != 0.0 && longitude != 0.0) {
        mMapFragment.getView().setVisibility(View.VISIBLE);
        mMapFragment.getMapAsync(this);
    } else
        mMapFragment.getView().setVisibility(View.GONE);
}

@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;
    LatLng currentLoc = new LatLng(latitude, longitude);
    mMap.addMarker(new MarkerOptions().position(currentLoc).title("My Location"));
    mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(currentLoc, 14.0f));
}

}

这是我的 xml..

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal"
    android:orientation="vertical"
    android:layout_margin="@dimen/activity_horizontal_margin">

    <fragment
        android:id="@+id/fragment"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="250dp" />
</LinearLayout>

这行显示错误: mMapFragment = (SupportMapFragment) this.getChildFragmentManager() .findFragmentById(R.id.fragment);

我也试过: mMapFragment = (SupportMapFragment) this.getSupportFragmentManager() .findFragmentById(R.id.fragment);

【问题讨论】:

  • 使用DialogFragment,它的对话框布局与fragment-like 实现

标签: android google-maps-android-api-2


【解决方案1】:

你应该调用getSupportFragmentManager()而不是MaterialDialog.Builder对象(你的AddressViewDialog扩展MaterialDialog.Builder),而是AppCompatActivity对象。所以使用

mMapFragment = (SupportMapFragment) ((AppCompatActivity) this.context).getSupportFragmentManager()
            .findFragmentById(R.id.fragment);

而不是

mMapFragment = (SupportMapFragment) this.getChildFragmentManager()
        .findFragmentById(R.id.fragment);

并以这种方式从MainActivity 创建您的AddressViewDialog

new AddressViewDialog(MainActivity.this).show();

MainActivity 应该扩展 AppCompatActivity)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-09
    • 2015-12-03
    • 1970-01-01
    • 2022-06-30
    • 1970-01-01
    相关资源
    最近更新 更多