【问题标题】:Load Map inside of a fragment in android在android中的片段内加载地图
【发布时间】:2014-08-26 09:10:04
【问题描述】:

我已经创建了导航抽屉,并且每个列表项 onclick 都会加载一个片段。有将加载地图的主页片段。我已经在扩展片段活动的活动中加载了地图,但没有在片段内部加载。我收到错误但没有得到它所以需要建议。

//HomeFragment.java

public class HomeFragment extends Fragment implements OnInfoWindowClickListener {

GoogleMap myMap;
private Marker marker;
private Marker marker2;
private Marker marker3;

// GPSTracker class
GpsTracker gps;

// textview
TextView DestinationDistance;

double latitude;
double longitude;

double lat2 = 23.7818442;
double lng2 = 90.3245161;

double lat3 = 23.7818442;
double lng3 = 90.4245161;

private Polyline line;

private ViewGroup userLayout;

// user info views

private TextView txtDistance;
private TextView txtETA;

private int count = 0;
private ArrayList<Polyline>pl;

public HomeFragment(){}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View v = inflater.inflate(R.layout.fragment_home, container, false);

    userLayout = (ViewGroup) v.findViewById(R.id.userLayout);
    txtDistance = (TextView) v.findViewById(R.id.txtV2);
    txtETA = (TextView) v.findViewById(R.id.txtV3);

    myMap = ((SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map)).getMap();

    myMap.setOnInfoWindowClickListener(this);

    /*Here will be other code to execute asynctask, 
   location detect, add marker etc */

    return v;
}

@Override
public void onInfoWindowClick(Marker arg0) {
    // TODO Auto-generated method stub

}

}

*此代码出现错误

  'The method getSupportFragmentManager() is undefined for the type HomeFragment'

//fragment_home.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1.0"
    android:orientation="vertical" >

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

<LinearLayout
    android:id="@+id/userLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="2.0"
    android:orientation="horizontal"
    android:visibility="gone" >

    <LinearLayout
        android:id="@+id/user_infoLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1.0"
        android:background="#fff" >

        <TextView
            android:id="@+id/txtV1"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1.0"
            android:background="#000"
            android:text="Profile Picture" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/position_infoLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1.0"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/txtV2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1.0"
            android:text="" />

        <TextView
            android:id="@+id/txtV3"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1.0"
            android:text="" />

        <Button
            android:id="@+id/btnMsg"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1.0"
            android:text="Send Message" />
    </LinearLayout>
</LinearLayout>

【问题讨论】:

    标签: android android-fragments google-maps-api-2


    【解决方案1】:

    你试过这个吗:-

    <fragment
        android:id="@+id/map"
        class="com.google.android.gms.maps.MapFragment"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_margin="5dp" />
    

    并使用:

    googleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
    

    获取片段

    【讨论】:

      【解决方案2】:

      试试下面的代码,我已经用这种方式做了,它工作正常。

          lnrMap.setVisibility(View.VISIBLE);
                      mapFragment = new SupportMapFragment();
                      addFragment(lnrMap.getId(), mapFragment);
                      Timer t = new Timer();
                      t.schedule(new TimerTask() {
      
                          @Override
                          public void run() {
                              getActivity().runOnUiThread(new Runnable() {
      
                                  @Override
                                  public void run() {
                                      try {
      
                                          googleMap = mapFragment.getExtendedMap();
      
                                          mapFragment.getView().setClickable(false);
                                          if (googleMap != null) {
                                              googleMap.getUiSettings().setZoomControlsEnabled(false);
                                              googleMap.getUiSettings().setCompassEnabled(false);
                                              googleMap.getUiSettings().setZoomGesturesEnabled(false);
                                              googleMap.getUiSettings().setScrollGesturesEnabled(false);
                                              googleMap.getUiSettings().setRotateGesturesEnabled(false);
                                              googleMap.getUiSettings().setTiltGesturesEnabled(false);
                                              googleMap.animateCamera(CameraUpdateFactory.zoomTo(6));
                                              t.cancel();
                                          }
      
                                      } catch (Exception e) {
                                      }
      
                                  }
                              });
      
                          }
                      }, 0, 500);
      
      //Just declare one LinearLayout in your xml file with the name of lnrMap, and then above //code will add map in that Linearlayout programatically.
      
          //Here is sample xml block for lnrMap LinearLayout.
      
          <LinearLayout
                  android:id="@+id/lnrMap"
                  android:layout_width="match_parent"
                  android:layout_height="300dp"
                  android:orientation="vertical"
                  android:visibility="gone" >
      
          //addFragment method:
      
              public void addFragment(int layoutId, Fragment fragment) {
                      FragmentManager fm = getChildFragmentManager();
                      FragmentTransaction ft = fm.beginTransaction();
                      ft.replace(layoutId, fragment);
                      ft.commit();
                  }
      

      【讨论】:

        【解决方案3】:

        首先检查您的 Android 最低 SDK 版本。如果是below &lt; 11,则使用SupportMapFragment,如果是above &gt;11,则使用MapFragment

        其次,检查您是否已将 Fragment 导入为 android.support.app.v4

        【讨论】:

          【解决方案4】:
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多