2017-04-04 44 views
0

我有一個SupportMapFragmentManagers getMapAsync()不會觸發onMapReady(GoogleMap的地圖)

public abstract class MyMapFragment implements OnMapReadyCallback 
{ 
    // 
    public GoogleMap googleMap; 
    SupportMapFragment mapFragment; 

    @IdRes 
    public abstract int getSupportMapFragId(); 

    @Override 
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { 
    super.onViewCreated(view, savedInstanceState); 

    // http://stackoverflow.com/a/36592000/5102206 
    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){ 
     // Do something for lollipop and above versions 
     mapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(getSupportMapFragId()); 
    } else { 
     // do something for phones running an SDK before lollipop 
     mapFragment = (SupportMapFragment) getFragmentManager().findFragmentById(getSupportMapFragId()); 
    } 
    mapFragment.getMapAsync(this); 

    } 

    //.. 

    @Override 
    public void onMapReady(GoogleMap map) { 
    this.googleMap = map; 
    } 
} 

根據我的斷點onViewCreated()被調用,但onMapReady()不叫(上this.googleMap = map斷點觸發)

在Android 5,6和7,它迄今爲止工作正常,我可以看到地圖.. 在Android 4.X(API 16 - API 19)設備上我的應用程序啓動,但它似乎凍結在那裏...我看到一個白色的空白屏幕。

在Android 4.X OS設備上: 1.使用getFragmentManager(),map條件對象在else條件後爲null。 2.使用getChildFragmentMenager(),mapfragment似乎是有效且非空的,但是onMapReady未觸發。

我在這裏錯過了什麼?

+0

爲什麼不使用擴展'SupportMapFragment'或直接使用它? Android Jelly(即API 16),後來覆蓋支持95.2%Google Play的設備。 –

+0

@JPVentura我暫時這樣做了,但沒有改變。我有同樣的結果。 –

+0

1.爲什麼在'MyMapFragment'中嵌套'SupportMapFragment'? 2.爲什麼你明確檢查沒有人使用的API級別? –

回答

1

在主線程上發生了RxJava的阻塞線程問題。所以這不是Google地圖問題。

0

我不太明白你爲什麼要嵌套片段,特別是因爲它可能會導致performance issues

如果你看一看Google Samples,則Google Maps例子使用了ActivitySupportMapFragment

public class MapsActivityCurrentPlace extends AppCompatActivity 
      implements OnMapReadyCallback, ConnectionCallbacks, 
      OnConnectionFailedListener { 

    @Override 
    public void onMapReady(GoogleMap map) { 
     mMap = map; 

     // Use a custom info window adapter to handle multiple lines of text in the 
     // info window contents. 
     mMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() { 

      @Override 
      // Return null here, so that getInfoContents() is called next. 
      public View getInfoWindow(Marker arg0) { 
       return null; 
      } 

      @Override 
      public View getInfoContents(Marker marker) { 
       // Inflate the layouts for the info window, title and snippet. 
       View infoWindow = getLayoutInflater().inflate(R.layout.custom_info_contents, 
         (FrameLayout)findViewById(R.id.map), false); 

       TextView title = ((TextView) infoWindow.findViewById(R.id.title)); 
       title.setText(marker.getTitle()); 

       TextView snippet = ((TextView) infoWindow.findViewById(R.id.snippet)); 
       snippet.setText(marker.getSnippet()); 

       return infoWindow; 
      } 
     }); 

     // Turn on the My Location layer and the related control on the map. 
     updateLocationUI(); 

     // Get the current location of the device and set the position of the map. 
     getDeviceLocation(); 
    } 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     if (savedInstanceState != null) { 
      mLastKnownLocation = savedInstanceState.getParcelable(KEY_LOCATION); 
      mCameraPosition = savedInstanceState.getParcelable(KEY_CAMERA_POSITION); 
     } 

     setContentView(R.layout.activity_maps); 

     mGoogleApiClient = new GoogleApiClient.Builder(this) 
      .enableAutoManage(this /* FragmentActivity */, 
        this /* OnConnectionFailedListener */) 
      .addConnectionCallbacks(this) 
      .addApi(LocationServices.API) 
      .addApi(Places.GEO_DATA_API) 
      .addApi(Places.PLACE_DETECTION_API) 
      .build(); 

     mGoogleApiClient.connect(); 
    } 

    @Override 
    public void onConnected(Bundle connectionHint) { 
     SupportMapFragment mapFragment = 
      (SupportMapFragment) getSupportFragmentManager() 
       .findFragmentById(R.id.map); 
     mapFragment.getMapAsync(this); 
    } 

    @Override 
    public void onConnectionFailed(@NonNull ConnectionResult result) { 
     Log.d(TAG, result.getErrorMessage()); 
    } 

    @Override 
    public void onConnectionSuspended(int cause) { 
     Log.d(TAG, "Play services connection suspended"); 
    } 

} 
0

注:不能充氣佈局成片段時佈局包括。當添加到片段嵌套的片段,僅支持動態

如果要充氣,片段的地圖,你既可以做它在XML或做它在Java代碼中是這樣的:

@Override 
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { 
    super.onViewCreated(view, savedInstanceState); 
    FragmentManager fm = getChildFragmentManager(); 
    SupportMapFragment mapFragment = (SupportMapFragment) fm.findFragmentByTag("mapFragment"); 
    if (mapFragment == null) { 
     mapFragment = new SupportMapFragment(); 
     FragmentTransaction ft = fm.beginTransaction(); 
     ft.add(R.id.mapFragmentContainer, mapFragment, "mapFragment"); 
     ft.commit(); 
     fm.executePendingTransactions(); 
    } 
    mapFragment.getMapAsync(callback); 
} 

還有簡單的容器

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/mapFragmentContainer" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 
</LinearLayout> 

此外,您不需要在類定義中實現onMapReadyCallback。取而代之的回調您創建一個新的OnMapReadyCallback()那裏:

MapView.getMapAsync(new OnMapReadyCallback() { 
     @Override 
     public void onMapReady(GoogleMap mMap) { 
      googleMap = mMap; 
       } 
      }); 

你還需要這些

MapView mMapView; 
private GoogleMap googleMap; 

我希望這有助於莫名其妙!