How to use google map V2 inside fragment?

后端 未结 9 842
攒了一身酷
攒了一身酷 2020-12-02 13:43

I have a fragment which is a part of Viewpager, and I want to use Google Map V2 inside that fragment. This is what I have tried so far,

In my fragment,



        
相关标签:
9条回答
  • 2020-12-02 14:08

    This is how I did it

    in layout:

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

    in code:

    public class GPS extends FragmentActivity {
    
    @Override
    protected void onResume() {
        super.onResume();
        setUpMapIfNeeded();
    }
    
    private void setUpMapIfNeeded() {
        // Do a null check to confirm that we have not already instantiated the map.
        if (supportMap == null) {
            // Try to obtain the map from the SupportMapFragment.
            supportMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
                    .getMap();
            // Check if we were successful in obtaining the map.
            if (supportMap != null) {
                MarkerOptions mo = new MarkerOptions().position( new LatLng( latitude, longitude ) );
                supportMap.addMarker( mo );
            }
        }
    }
    }
    

    Took me a lot of fiddling, you need just the right combination of things, make sure your class extends FragmentActivity

    0 讨论(0)
  • 2020-12-02 14:15

    my approach is:

    Bundle bundle = new Bundle();
    bundle.putInt(MY_ID, id);
    
    FragmentManager fm  = getFragmentManager();
    Fragment fragment = fm.findFragmentById(R.id.customer_details_fragment);
    
    fragment = new SalesFragment();
    fm.beginTransaction()
      .add(R.id.customer_details_fragment, fragment)
      .commit();
    
    fragment.setArguments(bundle);  
    
    0 讨论(0)
  • 2020-12-02 14:19

    In link , answer for you:

    "The problem is that what you are trying to do shouldn't be done. You shouldn't be inflating fragments inside other fragments. From Android's documentation:

    Note: You cannot inflate a layout into a fragment when that layout includes a . Nested fragments are only supported when added to a fragment dynamically.

    While you may be able to accomplish the task with the hacks presented here, I highly suggest you don't do it. Its impossible to be sure that these hacks will handle what each new Android OS does when you try to inflate a layout for a fragment containing another fragment.

    The only Android-supported way to add a fragment to another fragment is via a transaction from the child fragment manager."

    For this problem:

    Duplicate ID, tag null, or parent id with another fragment for com.google.android.gms.maps.MapFragment

    For me, the best solution are of @DeepakPanwar

    0 讨论(0)
提交回复
热议问题