0

Tried almost all the ways to put maps FragmentActivity inside dialog cannot able to succeed that. here is my code:

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

private static final int LOCATION_REQUEST_CODE = 111;
private GoogleMap mMap;

LatLng latLng;
String userName;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);
    if (getIntent().getExtras() != null) {
        latLng = (LatLng) getIntent().getExtras().get(MainActivity.MAP_COORDINATES);
        userName = (String) getIntent().getExtras().get(MainActivity.USER_NAME);
    }
    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
}



@Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (ActivityCompat.checkSelfPermission(this,
                    Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
                    && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            requestPermissions(new String[]{
                    Manifest.permission.ACCESS_FINE_LOCATION,
                    Manifest.permission.ACCESS_COARSE_LOCATION
            }, LOCATION_REQUEST_CODE);
        }
        return;
    }
    mMap.setMyLocationEnabled(true);
    mMap.getUiSettings().setZoomControlsEnabled(true);
    mMap.addMarker(new MarkerOptions().position(latLng).title(userName));
    mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, 14.0f));
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    if (requestCode == LOCATION_REQUEST_CODE){
        if (grantResults[0] == PackageManager.PERMISSION_GRANTED){
            onMapReady(mMap);
        }
    }
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}

}

i want to put above FragmentActivity inside Dialog (it is possible to pass fragment but i don't find a way to pass FragmentActivity tell me if any)

Here is the code i tried in different way but no use it is throws null pointer exception

private Dialog showMaps(Context context, final LatLng latLng, final String username){

    Dialog dialog = new Dialog(context);
    dialog.setContentView(R.layout.activity_maps);

    MapView mapView = dialog.findViewById(R.id.map_view);
    MapsInitializer.initialize(this);
    mapView.onCreate(dialog.onSaveInstanceState());
    mapView.onResume();
    mapView.getMapAsync(new OnMapReadyCallback() {
        @Override
        public void onMapReady(GoogleMap googleMap) {
            googleMap.addMarker(new MarkerOptions().position(latLng).title(username));
            googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, 14.0f));
        }
    });

    Window window = getWindow();
    assert window != null;
    window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
    return dialog;
}

1 Answers1

0

Suggestion: Instead going for map fragments in dialog, just use FrameLayout as parent and add map fragment as a last child of the layout and make its visibility to gone(flexibility here is you can specify the size of the map fragment, but coming to dialog it is too complex)..So whenever the user clicks on some button, you can make it visible programatically like...

 mapFragment.setVisibility(View.VISIBLE);

and

mapFragment.setVisibility(View.GONE);