2

我想在 AlertDialog 中显示 MapFragment。

在阅读了以下问题后,我最终提出了这个问题[括号中的观察结果是我的结果,有人可能已经让它们工作了,但我不会因为我把事情搞砸而忽视我惊人的天赋]:

尝试其中任何一个,或两者的陪审团操纵的婚姻,都会导致 IllegalArgument 或 NullPointer 异常。有时我抱怨无法在扩展的 MapFragment 中找到与 ID 关联的视图。

到目前为止我得到的相关代码:

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);
//      }
    }
}
4

1 回答 1

0

您可以通过此在对话框中显示地图片段

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>`

并通过 new DialogMapFragment().show(getFragmentManager(),"tag"); 显示对话框 您可以使用所需的任何按钮进一步自定义对话框片段

于 2013-11-01T12:41:20.360 回答