Показать Google Map в диалоговом окне материалов Android - PullRequest
0 голосов
/ 15 мая 2018

Я хочу показать карту Google в пользовательском диалоге .. Но я не могу получить доступ к 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);

1 Ответ

0 голосов
/ 30 мая 2018

Вы должны вызывать 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);

и создайте свой AddressViewDialog из MainActivity следующим образом:

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

(MainActivity должен расширятьсяAppCompatActivity).

...