2015-10-05 25 views
0

我一直在努力,現在的幾天,我只是不能讓它正常工作,是否有可能延長從Fragment一個googleMap而不是FragmentActivityGoogleMap的一個片段,而不是作爲一個FragmentActivity

我用同樣的例子代碼android studio創建爲map fragment例子,只是一直在努力使之成爲一個Fragment而不是FragmentActivity

可能嗎?

我使用的目標sdk21和minsdk 21

+1

看看這個:http://stackoverflow.com/questions/19353255/how-to-put-google-maps-v2-on-a-fragment-using- viewpager –

回答

3

使用下面的代碼在你的活動/片段,你想用地圖

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

然後在你的碎片的onCreateView您可以使用以下代碼

private void setUpMapIfNeeded() { 
     // Do a null check to confirm that we have not already instantiated the 
     // map. 
     if (mMapFragment == null) { 
      // Try to obtain the map from the SupportMapFragment. 
      mMapFragment = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)); 
      // Check if we were successful in obtaining the map. 
      if (mMapFragment != null) { 
       setUpMap(); 
      } 
     } 
    } 

設置您在以下提到的方法

映射的東西10
private void setUpMap() { 

     mMapFragment.getMapAsync(new OnMapReadyCallback() { 
      @Override 
      public void onMapReady(GoogleMap map) { 
      ... 
      } 
     } 
    } 

希望它可以幫助...

+0

您可能會接受這個問題 – Rinav

+0

感謝Rinav !,這確實幫我找到了我曾經遇到過的問題,所以我找到了工作。 :) – user1501127

+0

@ user1501127很高興看到您的問題解決:) – Rinav

相關問題