android - 是否可以在对话框 fragment 内显示 map View ?

标签 android google-maps android-dialogfragment

我有一个对话框 fragment ,我想在其中显示我的 map 并标记我随身携带的纬度和日志。 但我的 getMap() 为 NULL。这就是我所做的

public class EventDetailsDialogFragment extends DialogFragment{
    TextView title, desc, sdate, edate, room, loctn;
    FeedObjModel selectedFeedObject;
    Date netDate;
    SimpleDateFormat sdf;
    GoogleMap theMap;
    MarkerOptions markerOptions;
    FragmentActivity myContext;

    @Override
    public void onAttach(Activity activity) {
        // TODO Auto-generated method stub
        super.onAttach(activity);
    }

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    View view = inflater.inflate(R.layout.event_details_dialog_layout, container);

    title = (TextView) view.findViewById(R.id.title_event_details_TV);
    desc = (TextView) view.findViewById(R.id.description_event_details_TV);
    sdate = (TextView) view.findViewById(R.id.start_date_event_details_TV);
    edate = (TextView) view.findViewById(R.id.end_date_event_details_TV);
    loctn = (TextView) view.findViewById(R.id.locEdTV);


    long timestamp1 = Long.parseLong(selectedFeedObject.eventstartDate);
    long timestamp2 = Long.parseLong(selectedFeedObject.eventendDate);
    try{ 
        sdf = new SimpleDateFormat("MMM dd, yyyy");
        netDate = (new Date(timestamp1*1000));
        sdate.setText(sdf.format(netDate));  
        netDate = (new Date(timestamp2*1000));
        edate.setText(sdf.format(netDate));  
    } catch(Exception ex){

        }

    getDialog().setTitle(""+selectedFeedObject.subject);
    title.setText(selectedFeedObject.subject);
    desc.setText(selectedFeedObject.eventDescription);
    loctn.setText(selectedFeedObject.eventLocation);

/*  theMap = ((SupportMapFragment) getActivity().getSupportFragmentManager()
            .findFragmentById(R.id.mapED))
            .getMap();*/

    SupportMapFragment fragment = new SupportMapFragment();
    FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
    transaction.add(R.id.mapED, fragment).commit(); 
    theMap = fragment.getMap();

    Log.v("theMap1", ""+theMap);

        Double lng = Double.parseDouble(selectedFeedObject.eventlongitude);
        Double lat = Double.parseDouble(selectedFeedObject.eventlatitude);
        LatLng latLng = new LatLng(lng, lat);
        MarkerOptions markerOptions = new MarkerOptions();
        markerOptions.position(latLng);
        markerOptions.title(selectedFeedObject.eventLocation);

    Log.v("markerOptions", ""+markerOptions);

        theMap.addMarker(markerOptions);
        theMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
        CameraUpdate center = CameraUpdateFactory
                .newLatLng(new LatLng(lat, lng));
        CameraUpdate zoom = CameraUpdateFactory.zoomTo(15);
        theMap.moveCamera(center);
        theMap.animateCamera(zoom);
    return view;

}


public void setEventDetails(FeedObjModel _selectedFeedObject) {
    // TODO Auto-generated method stub
    selectedFeedObject = _selectedFeedObject;
}
}

我将“themap”设置为 NULL

.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="5dp"
    android:paddingLeft="10dp"
    android:paddingRight="5dp"
    android:paddingTop="5dp" >

    <TextView
        android:id="@+id/title_event_details_TV"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Description" 
        android:textSize="16sp"
        android:textStyle="bold"/>

    <TextView
        android:id="@+id/description_event_details_TV"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Start Date and Time"
        android:textSize="16sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/start_date_event_details_TV"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="End Date and Time"
        android:textSize="16sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/end_date_event_details_TV"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Location" 
        android:textSize="16sp"
        android:textStyle="bold"/>

    <TextView
        android:id="@+id/locEdTV"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />

    <FrameLayout
        android:id="@+id/mapED"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:name="com.google.android.gms.maps.SupportMapFragment"
         />

</LinearLayout>

最佳答案

我在onResume()中编写了以下代码,现在它正在工作......我不知道这是正确的方法。

    @Override
public void onResume() {

    // TODO Auto-generated method stub
    super.onResume();
    theMap = mapView.getMap();
    Log.v("theMap1", ""+theMap);
    Log.v("lng", ""+lng);
    Log.v("lat", ""+lat);
    latLng = new LatLng(lng, lat);
    MarkerOptions markerOptions = new MarkerOptions();
    markerOptions.position(latLng);
    markerOptions.title(locString);

    Log.v("markerOptions", ""+markerOptions);
    theMap.addMarker(markerOptions);
    theMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
     CameraUpdate center=
                CameraUpdateFactory.newLatLngZoom(new LatLng(lat,
                        lng),16);
    CameraUpdate zoom = CameraUpdateFactory.zoomTo(15);
    theMap.moveCamera(center);
    theMap.animateCamera(zoom);

}

在oncreateview中

mapView = CustomMapFragmentForEventDetails.newInstance();
    FragmentTransaction fragmentTransaction = getChildFragmentManager().beginTransaction();
    fragmentTransaction.add(R.id.mapED, mapView);
    fragmentTransaction.commit();

        lng = Double.parseDouble(selectedFeedObject.eventlongitude);
        lat = Double.parseDouble(selectedFeedObject.eventlatitude);

以及 CustomMapFragmentForEventDetails

public class CustomMapFragmentForEventDetails extends SupportMapFragment {
    SupportMapFragment mSupportMapFragment;
    GoogleMap googleMap;
    OnMapReadyListener mOnMapReadyListener;
    @Override
     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
     {
      super.onCreateView(inflater, container, savedInstanceState);
      Log.v("Inside CustomMapFrag", "Success");
      View root = inflater.inflate(R.layout.event_details_dialog_layout, null, false); 
      initilizeMap();
      return root;
     }

    @Override
    public void onAttach(Activity activity) {
        // TODO Auto-generated method stub
        super.onAttach(activity);
        mOnMapReadyListener = (OnMapReadyListener) activity;
    }

    private void initilizeMap()
     {
      mSupportMapFragment = (SupportMapFragment) getFragmentManager().findFragmentById(R.id.mapED);
      if (mSupportMapFragment == null) {
       FragmentManager fragmentManager = getFragmentManager();
       FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
       mSupportMapFragment = SupportMapFragment.newInstance();
       fragmentTransaction.replace(R.id.mapED, mSupportMapFragment).commit();
         }
      if (mSupportMapFragment != null)
      {
          googleMap = mSupportMapFragment.getMap();
       if (googleMap != null)

       {
           Log.v("MAP GOOGLE in CustomMApfragment", ":: "+googleMap);
           mOnMapReadyListener.onMapReady(googleMap);
       }

      }
     }
    public static interface OnMapReadyListener {

        void onMapReady(GoogleMap googleMap);
    }
}

关于android - 是否可以在对话框 fragment 内显示 map View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25157002/

相关文章:

java - DialogFragment 在 Android 4.4.2 中顶部有蓝线

android - 如何更改 DialogFragment 大小

android - 如何显示全角 DialogFragment?

android - 使用上下文菜单在自定义 ListView 中突出显示所选项目?

android - 在 Android 中解析 Google Places JSON 数据?

android - 尝试在谷歌地图 V2 android 应用程序中获取空数组的长度

android - 使用 Google map 选项初始化 map fragment

android - 谷歌地图 setClustering(ClusteringSettings) 方法未解析

android - 如何在 CollapsingToolbarLayout 折叠时启用后退按钮

android - 一个可以将项目 rebase 到 Google AOSP 的 Gerrit 实例?