2

I want to get a MapFragment displayed inside an AlertDialog.

I ended up asking this after reading the following questions [observations between brackets are my results, someone may have got them working but I'm not one to dismiss my amazing talent for screwing things up]:

Trying either of those, or a jury-rigged marriage of both, results in either IllegalArgument or NullPointer exceptions. Sometimes I got a whine about being unable to find the view associated with the ID in the extended MapFragment.

The relevant code I've got so far:

public class PropDetailActivity extends Activity
{
    static final int PICK_REQUEST = 70003;

    private static final int TABLE_PROP_TYPES = 13,
        ADD_ID = Menu.FIRST + 1,
        DELETE_ID = Menu.FIRST + 3;                 

    Button btnLocation;

    private LocationManager daLocationManager;
    private String daProvider;
    private double daCurrentLat, daCurrentLong;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {   
        super.onCreate(savedInstanceState);
        setContentView(R.layout.prop_detail);

        btnLocation = (Button) findViewById(R.id.btnLocation);

        btnLocation.setOnClickListener(new OnClickListener()
        {           
            @Override
            public void onClick(View v)
            {
                openGps();
            }
        });

        doMapGubbinz();
    }

    private void openGps()
    {
        LayoutInflater inflater = LayoutInflater.from(this);

        LatLng here = new LatLng(daCurrentLat, daCurrentLong);

        MapFragment fragment = PreparedMapFragment.newInstance(here);

        FragmentTransaction transaction = getFragmentManager().beginTransaction();

        transaction.add(R.id.mapView, fragment)
            .commit();

        //daMap = fragment.getMap();

        View gps = inflater.inflate(R.layout.location_gps, null);

        new AlertDialog.Builder(this)
            .setTitle("Property location")
            .setView(gps)
            .show();

//      Marker currentLoc = daMap.addMarker(new MarkerOptions().position(here).title("Ubicación actual"));
//      
//      daMap.moveCamera(CameraUpdateFactory.newLatLngZoom(here, 15));
//      
//      daMap.animateCamera(CameraUpdateFactory.zoomTo(15), 2000, null);
    }

    public void doMapGubbinz()
    {
        daLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

        Criteria criteria = new Criteria();

        daProvider = daLocationManager.getBestProvider(criteria, false);

        daLocationManager.requestLocationUpdates(daProvider, 400, 1, this);

        Location location = daLocationManager.getLastKnownLocation(daProvider);     

        if (location != null) 
        {
            System.out.println("Provider " + daProvider + " has been selected.");
            onLocationChanged(location);
        } 
        daCurrentLat = location.getLatitude();
        daCurrentLong = location.getLongitude();
    }

    static public class PreparedMapFragment extends MapFragment
    {
        static MapFragment frag;

        public PreparedMapFragment() 
        {
            super();
        }

        public static MapFragment newInstance(LatLng pos)
        {
            frag = new MapFragment();

            UiSettings settings = frag.getMap().getUiSettings();

            settings.setAllGesturesEnabled(false);
            settings.setMyLocationButtonEnabled(false);

            frag.getMap().moveCamera(CameraUpdateFactory.newLatLngZoom(pos, 16));

            frag.getMap().addMarker(new MarkerOptions().position(pos).title("Ubicación actual"));

            frag.getMap().animateCamera(CameraUpdateFactory.zoomTo(15), 2000, null);

            return frag;
        }

//      @Override
//      public View onCreateView(LayoutInflater arg0, ViewGroup arg1, Bundle arg2)
//      {
//          View v = super.onCreateView(arg0, arg1, arg2);
//          initMap();
//          return v;
//      }
//
//      private void initMap()
//      {
//          UiSettings settings = getMap().getUiSettings();
//          
//          settings.setAllGesturesEnabled(false);
//          settings.setMyLocationButtonEnabled(false);
//          
//          getMap().moveCamera(CameraUpdateFactory.newLatLngZoom(daCurrentPos, 16));
//          
//          getMap().addMarker(new MarkerOptions().position(daCurrentPos).title("Ubicación actual"));
//
//          getMap().animateCamera(CameraUpdateFactory.zoomTo(15), 2000, null);
//      }
    }
}
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Emiliano De Santis
  • 197
  • 1
  • 3
  • 15

1 Answers1

0

You can show a map fragment in a dialog by this

public class DialogMapFragment extends DialogFragment {

    private SupportMapFragment fragment;

    public DialogMapFragment() {
        fragment = new SupportMapFragment();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.mapdialog, container, false);
        getDialog().setTitle("");
        FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
        transaction.add(R.id.mapView, fragment).commit();

        return view;
    }



    public SupportMapFragment getFragment() {
        return fragment;
    }
}



<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="0dp" >

    <FrameLayout
        android:id="@+id/mapView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </FrameLayout>

</RelativeLayout>`

And show dialog by new DialogMapFragment().show(getFragmentManager(),"tag"); You can further customize the Dialog Fragment with any buttons you need

kmangr
  • 121
  • 1
  • 4