在Fragment中的MapFragment,有替代方案吗?

27

我需要你的帮助... 我已经工作了3天,我的应用程序使用碎片(fragments)。 其中一个碎片必须显示来自Google Maps V2 api for Android的地图。

目前,我正在使用MapFragment,但并不奇怪,一个碎片中包含另一个碎片不是个好主意。虽然它可以工作,地图也能够显示和编辑,但是当我切换到其他碎片并返回它时,就会出现以下错误:

Caused by: java.lang.IllegalArgumentException: Binary XML file line #59: Duplicate id 0x7f070041, tag null, or parent id 0x7f070040 with another fragment for com.google.android.gms.maps.MapFragment

at android.app.Activity.onCreateView(Activity.java:4252)

at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:673)

这是当我去另一个碎片然后返回包含地图的碎片时的原因。我已经花了3天时间来尝试解决这个问题,但是没有什么好结果。

简单来说,我有一个Activity调用一个布局文件中包含MapFragment的碎片。如果你需要更多信息,请随时问我:)

谢谢

编辑: 以下是在主Activity中更改Fragment的代码:

private void swtichFragment(Fragment fragment, Bundle bundle)
{
fragment.setBundle(this, bundle);
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.rightFragmentPlaceHolder, fragment);
fragmentTransaction.commit();
mRightFragment = fragment;
}

展示你的片段切换代码... - Emil Adz
已添加切换片段的功能 :) - Kyu_
你是以静态方式将地图作为参考,还是将其传递给其他可能仍然存在的类? - Simon Zettervall
8个回答

43

使用SupportMapFragment来解决这个错误:

在片段布局中

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

在你的Fragment的onCreateView方法中

SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.googleMap);

        if (mapFragment != null) {
            mapFragment.getMapAsync(this);
        }

由于你的布局在Fragment的布局中,因此SupportMapFragment是你的片段的子布局。因此请使用getChildFragmentManager(),这是Fragment的FragmentManager


6
这绝对是最佳方法。这改变了 Google 示例中的一行代码并使其正常运作。干得好,Adnan。 - PGMacDesign
1
谢谢你解释getChildFragmentManager()。这对我来说很有帮助。 - Philipp Schumann

29
请在Fragment布局中使用MapView而不是MapFragment。请记得调用MapView的生命周期方法:
  • onCreate(Bundle)
  • onResume()
  • onPause()
  • onDestroy()
  • onSaveInstanceState(Bundle)
  • onLowMemory()
如此处所述:这里
顺便说一句,您不应该使用MapFragment,只能使用SupportMapFragment和支持库
编辑:
如果切换到支持库,则可以使用此处评论#1中的代码: http://code.google.com/p/gmaps-api-issues/issues/detail?id=5064#c1

3
为什么我们不应该使用MapFragment? - James Mundy
3
可能的原因之一是,支持库包含的“Fragment”比早期版本的Android 4.x中的本机“Fragment”更不容易出错,因此即使您只针对4.x进行定位,您也可以使用较少错误的代码,而APK的大小仅增加了几百kB。另外一件事是,如果客户最终决定您仍然需要支持2.3.3,那么您需要处理的工作量就少了。 - MaciejGórski
谢谢,非常感谢!很有道理。 - James Mundy

13

如此描述

要在片段(NestedFragment)中显示MapFragment: 此时我认为您已经:

  1. 在清单文件上添加了必要的权限
  2. 将Google Play服务作为库项目添加
  3. 在清单文件中添加了API密钥。
  4. 4.

where.xml

 <LinearLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:map="http://schemas.android.com/apk/res-auto"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:orientation="vertical" >
         <FrameLayout
           android:layout_width="match_parent"
           android:layout_height="0dp"
           android:layout_weight="1.03"
           android:name="com.google.android.gms.maps.SupportMapFragment"
           android:id="@+id/mapwhere" />


          <TextView
           android:layout_width="match_parent"
           android:layout_height="wrap_content"/>

        </LinearLayout>

类:

 public class WhereFragment extends SupportMapFragment {

    @Override
     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
     {
      super.onCreateView(inflater, container, savedInstanceState);
      View root = inflater.inflate(R.layout.where, null, false); 
      initilizeMap();
      return root;
     }

    private void initilizeMap()
     {
      mSupportMapFragment = (SupportMapFragment) getFragmentManager().findFragmentById(R.id.mapwhere);
      if (mSupportMapFragment == null) {
       FragmentManager fragmentManager = getFragmentManager();
       FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
       mSupportMapFragment = SupportMapFragment.newInstance();
       fragmentTransaction.replace(R.id.mapwhere, mSupportMapFragment).commit();
         }
      if (mSupportMapFragment != null)
      {
       googleMap = mSupportMapFragment.getMap();
       if (googleMap != null)
        googleMap.setOnMapClickListener(new OnMapClickListener()
        {
         @Override
         public void onMapClick(LatLng point)
         {
          //TODO: your onclick stuffs
         }
        });
      }
     }
    }

文档:

嵌套片段:现在您可以在片段中嵌入其他片段。这对于需要将动态和可重复使用的UI组件放置到自身也是动态和可重复使用的UI组件中的各种情况非常有用。例如,如果您使用ViewPager创建可以左右滑动并占据大部分屏幕空间的片段,则现在可以将片段插入每个片段页面中。

要嵌套一个片段,只需在要添加片段的片段上调用getChildFragmentManager()。这会返回一个FragmentManager,可以像从顶级活动一样使用它来创建片段事务。例如,以下是一些代码,它会在现有片段类内添加一个片段:

Fragment videoFragment = new VideoPlayerFragment(); FragmentTransaction transaction = getChildFragmentManager().beginTransaction(); transaction.add(R.id.video_fragment, videoFragment).commit(); 从嵌套片段内部,您可以通过调用getParentFragment()获取对父片段的引用。

Android Support库现在也支持嵌套片段,因此您可以在Android1.6及更高版本上实现嵌套片段设计。

注意:当布局包含<fragment>时,您无法将布局填充到片段中。仅在动态添加到片段时支持嵌套片段。

来源:http://developer.android.com/about/versions/android-4.2.html#NestedFragments

这也会修复以下问题:

 11-06 11:36:01.509: E/AndroidRuntime(6309): FATAL EXCEPTION: main
    11-06 11:36:01.509: E/AndroidRuntime(6309): android.view.InflateException: Binary XML file line #9: Error inflating class fragment
    11-06 11:36:01.509: E/AndroidRuntime(6309):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:710)
    11-06 11:36:01.509: E/AndroidRuntime(6309):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:752)
    11-06 11:36:01.509: E/AndroidRuntime(6309):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:760)
    11-06 11:36:01.509: E/AndroidRuntime(6309):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:760)
    11-06 11:36:01.509: E/AndroidRuntime(6309):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:760)
    11-06 11:36:01.509: E/AndroidRuntime(6309):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:760)
    11-06 11:36:01.509: E/AndroidRuntime(6309):  at android.view.LayoutInflater.inflate(LayoutInflater.java:495)
    11-06 11:36:01.509: E/AndroidRuntime(6309):  at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
    11-06 11:36:01.509: E/AndroidRuntime(6309):  at com.abc.android.ui.WhereFragment.onCreateView(WhereFragment.java:60)
    11-06 11:36:01.509: E/AndroidRuntime(6309):  at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
    11-06 11:36:01.509: E/AndroidRuntime(6309):  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
    11-06 11:36:01.509: E/AndroidRuntime(6309):  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
    11-06 11:36:01.509: E/AndroidRuntime(6309):  at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
    11-06 11:36:01.509: E/AndroidRuntime(6309):  at a ...

1
嗨,这里我得到了一个空的Google地图实例。你能帮我解决一下吗? - ekjyot
在选项卡或导航抽屉中与其他片段一起保持地图片段的活动状态非常好,谢谢。 - Pelanes

5

在你的课程中

      SupportMapFragment mSupportMapFragment;
      private GoogleMap googleMap;
      int ZOOM_LEVEL=15;

      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
       View mTrackView = inflater
            .inflate(R.layout.mylayout, container, false);
        mSupportMapFragment = SupportMapFragment.newInstance();
        FragmentTransaction fragmentTransaction = getChildFragmentManager().beginTransaction();
        fragmentTransaction.add(R.id.mapwhere, mSupportMapFragment);
        fragmentTransaction.commit();

        return mTrackView;
    }

      @Override
      public void onStart() {
        // TODO Auto-generated method stub
          super.onStart();

        if(mSupportMapFragment!=null){

            googleMap = mSupportMapFragment.getMap();
            if(googleMap!=null){
            googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
            googleMap.getUiSettings().setMyLocationButtonEnabled(false);
            googleMap.setMyLocationEnabled(false);


            CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(
                    new LatLng(12.12122,
                        17.22323), ZOOM_LEVEL);
            googleMap.animateCamera(cameraUpdate);
              }
            }
      }

mylayout.xml

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:map="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:orientation="vertical" >
     <FrameLayout
       android:layout_width="match_parent"
       android:layout_height="0dp"
       android:layout_weight="1.03"

       android:id="@+id/mapwhere" />


      <TextView
       android:layout_width="match_parent"
       android:layout_height="wrap_content"/>

    </LinearLayout>

1
在你的布局文件中。
<fragment
                android:id="@+id/map"
                android:name="com.google.android.gms.maps.SupportMapFragment"
                android:layout_width="match_parent"
                android:layout_height="match_parent"/>

在你的onCreate() 片段中,使用childFragmentManager引用你的地图片段在你的片段布局文件中。
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
        val mapFragment: SupportMapFragment = childFragmentManager.findFragmentById(R.id.map) as SupportMapFragment
        // Set callback on the fragment
        mapFragment.getMapAsync(this)

0

经过许多错误,我终于做到了,这是我的MapView Fragment类:

import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnMapClickListener;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.serveroverload.yago.R;

/**
 * @author 663918
 *
 */
public class HomeFragment extends Fragment implements LocationListener {
    // Class to do operations on the Map
    GoogleMap googleMap;
    private LocationManager locationManager;

    public static Fragment newInstance() {
        return new HomeFragment();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.home_fragment, container, false);
        Bundle bdl = getArguments();

        // setuping locatiomanager to perfrom location related operations
        locationManager = (LocationManager) getActivity().getSystemService(
                Context.LOCATION_SERVICE);

        // Requesting locationmanager for location updates
        locationManager.requestLocationUpdates(
                LocationManager.NETWORK_PROVIDER, 1, 1, this);

        // To get map from MapFragment from layout
        googleMap = ((MapFragment) getActivity().getFragmentManager()
                .findFragmentById(R.id.map)).getMap();

        // To change the map type to Satellite
        // googleMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);

        // To show our current location in the map with dot
        // googleMap.setMyLocationEnabled(true);

        // To listen action whenever we click on the map
        googleMap.setOnMapClickListener(new OnMapClickListener() {

            @Override
            public void onMapClick(LatLng latLng) {
                /*
                 * LatLng:Class will give us selected position lattigude and
                 * longitude values
                 */
                Toast.makeText(getActivity(), latLng.toString(),
                        Toast.LENGTH_LONG).show();
            }
        });

        changeMapMode(3);

        // googleMap.setSatellite(true);
        googleMap.setTrafficEnabled(true);
        googleMap.setBuildingsEnabled(true);
        googleMap.setMyLocationEnabled(true);

        return v;
    }

    private void doZoom() {
        if (googleMap != null) {
            googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(
                    new LatLng(18.520430, 73.856744), 17));
        }
    }

    private void changeMapMode(int mapMode) {

        if (googleMap != null) {
            switch (mapMode) {
            case 0:
                googleMap.setMapType(GoogleMap.MAP_TYPE_NONE);
                break;

            case 1:
                googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
                break;

            case 2:
                googleMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
                break;

            case 3:
                googleMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
                break;

            case 4:
                googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
                break;

            default:
                break;
            }
        }
    }

    private void createMarker(double latitude, double longitude) {
        // double latitude = 17.385044;
        // double longitude = 78.486671;

        // lets place some 10 random markers
        for (int i = 0; i < 10; i++) {
            // random latitude and logitude
            double[] randomLocation = createRandLocation(latitude, longitude);

            // Adding a marker
            MarkerOptions marker = new MarkerOptions().position(
                    new LatLng(randomLocation[0], randomLocation[1])).title(
                    "Hello Maps " + i);

            Log.e("Random", "> " + randomLocation[0] + ", " + randomLocation[1]);

            // changing marker color
            if (i == 0)
                marker.icon(BitmapDescriptorFactory
                        .defaultMarker(BitmapDescriptorFactory.HUE_AZURE));
            if (i == 1)
                marker.icon(BitmapDescriptorFactory
                        .defaultMarker(BitmapDescriptorFactory.HUE_BLUE));
            if (i == 2)
                marker.icon(BitmapDescriptorFactory
                        .defaultMarker(BitmapDescriptorFactory.HUE_CYAN));
            if (i == 3)
                marker.icon(BitmapDescriptorFactory
                        .defaultMarker(BitmapDescriptorFactory.HUE_GREEN));
            if (i == 4)
                marker.icon(BitmapDescriptorFactory
                        .defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA));
            if (i == 5)
                marker.icon(BitmapDescriptorFactory
                        .defaultMarker(BitmapDescriptorFactory.HUE_ORANGE));
            if (i == 6)
                marker.icon(BitmapDescriptorFactory
                        .defaultMarker(BitmapDescriptorFactory.HUE_RED));
            if (i == 7)
                marker.icon(BitmapDescriptorFactory
                        .defaultMarker(BitmapDescriptorFactory.HUE_ROSE));
            if (i == 8)
                marker.icon(BitmapDescriptorFactory
                        .defaultMarker(BitmapDescriptorFactory.HUE_VIOLET));
            if (i == 9)
                marker.icon(BitmapDescriptorFactory
                        .defaultMarker(BitmapDescriptorFactory.HUE_YELLOW));

            googleMap.addMarker(marker);

            // Move the camera to last position with a zoom level
            if (i == 9) {
                CameraPosition cameraPosition = new CameraPosition.Builder()
                        .target(new LatLng(randomLocation[0], randomLocation[1]))
                        .zoom(15).build();

                googleMap.animateCamera(CameraUpdateFactory
                        .newCameraPosition(cameraPosition));
            }
        }

    }

    /*
     * creating random postion around a location for testing purpose only
     */
    private double[] createRandLocation(double latitude, double longitude) {

        return new double[] { latitude + ((Math.random() - 0.5) / 500),
                longitude + ((Math.random() - 0.5) / 500),
                150 + ((Math.random() - 0.5) * 10) };
    }

    @Override
    public void onLocationChanged(Location location) {

        if (null != googleMap) {
            // To get lattitude value from location object
            double latti = location.getLatitude();
            // To get longitude value from location object
            double longi = location.getLongitude();

            // To hold lattitude and longitude values
            LatLng position = new LatLng(latti, longi);

            createMarker(latti, longi);

            // Creating object to pass our current location to the map
            MarkerOptions markerOptions = new MarkerOptions();
            // To store current location in the markeroptions object
            markerOptions.position(position);

            // Zooming to our current location with zoom level 17.0f
            googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(position,
                    17f));

            // adding markeroptions class object to the map to show our current
            // location in the map with help of default marker
            googleMap.addMarker(markerOptions);
        }

    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onProviderEnabled(String provider) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onProviderDisabled(String provider) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onDestroyView() {
        // TODO Auto-generated method stub
        super.onDestroyView();

        locationManager.removeUpdates(this);

        android.app.Fragment fragment = getActivity().getFragmentManager()
                .findFragmentById(R.id.map);
        if (null != fragment) {
            android.app.FragmentTransaction ft = getActivity()
                    .getFragmentManager().beginTransaction();
            ft.remove(fragment);
            ft.commit();
        }
    }


}

我的 Xml 文件长这样:

<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/map"
    android:name="com.google.android.gms.maps.MapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

结果看起来像这样:

enter image description here

最重要的是不要混合使用app.Fragment和v4.Fragment,否则应用程序会崩溃。

正如您所看到的,我已经使用app.Fragment来附加和删除我的MapView Fragment。

希望能对某些人有所帮助。


0

0
            <fragment
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:name="com.google.android.gms.maps.SupportMapFragment"
            android:id="@+id/location_map"
            android:layout_above="@id/atmLocation_recyclerView"
            />


View root= inflater.inflate(R.layout.fragment_a_t_m_locations, container, false);
    SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.location_map);
    mapFragment.getMapAsync(googleMap -> {
        mMap=googleMap;
        if(mMap!=null){
            mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
        }
    });

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接