ViewPager不会在多个片段中显示地图v2

时间:2013-12-10 09:03:49

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

我在视图分页器里面的片段中显示地图,带有详细信息片段,对于地图我已经动态创建了片段。我正在使用单个活动,因此每个屏幕都由片段表示。 NewsDetailsFragment包含一个ViewPager(地图)。

当我最初导航到NewsDetailsFragment时,地图被加载,现在如果我加载一个新的NewsDetailsFragment实例,就像在滚动页面上我正在显示我的下一条记录。第二个NewsDetailsFragment中的ViewPager不会显示地图。在LogCat中,Log显示一切正常,地图显示和标记设置。但是在地图上显示黑屏。

如果有一个加载的地图ViewPager,只有当我按下Back直到我没有NewsDetailsFragment(因此没有运行ViewPagers)时,任何更多的NewsDetailsFragment都不会显示他们的地图,只有当我打开一个新的NewsDetailsFragment实例时它才会加载地图。

请解决我的问题。

1 个答案:

答案 0 :(得分:0)

import com.google.android.gms.maps.SupportMapFragment;

SupportMapFragment mMapFragment = SupportMapFragment.newInstance();     
FragmentManager fm = fragment.getChildFragmentManager();
fm.beginTransaction().add(R.id.map, mMapFragment).commit();
mMapFragment.getMapAsync(this);

@Override
public void onMapReady(GoogleMap googleMap) {
mGoogleMap = googleMap;

if (ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
    // TODO: Consider calling
    //    ActivityCompat#requestPermissions
    // here to request the missing permissions, and then overriding
    //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
    //                                          int[] grantResults)
    // to handle the case where the user grants the permission. See the documentation
    // for ActivityCompat#requestPermissions for more details.
    return;
}
mGoogleMap.setMyLocationEnabled(true);

buildGoogleApiClient();

mGoogleApiClient.connect();
}

protected synchronized void buildGoogleApiClient() {

mGoogleApiClient = new GoogleApiClient.Builder(getContext())
        .addConnectionCallbacks(this)
        .addOnConnectionFailedListener(this)
        .addApi(LocationServices.API)
        .build();
}

@Override
public void onConnected(Bundle bundle) {

if (ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
    // TODO: Consider calling
    //    ActivityCompat#requestPermissions
    // here to request the missing permissions, and then overriding
    //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
    //                                          int[] grantResults)
    // to handle the case where the user grants the permission. See the documentation
    // for ActivityCompat#requestPermissions for more details.
    return;
}
Location mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
        mGoogleApiClient);
if (mLastLocation != null) {
    //place marker at current position
    mGoogleMap.clear();
    //setLocation(mLastLocation.getLatitude(), mLastLocation.getLongitude());
}


mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(50000); //50 seconds
mLocationRequest.setFastestInterval(30000); //30 seconds
mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
//mLocationRequest.setSmallestDisplacement(0.1F); //1/10 meter

 LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
}

@Override
public void onConnectionSuspended(int i) {
Toast.makeText(customAdapter.getContext(), "onConnectionSuspended", Toast.LENGTH_SHORT).show();
}

@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
Toast.makeText(customAdapter.getContext(), "onConnectionFailed", Toast.LENGTH_SHORT).show();

}

@Override
public void onLocationChanged(Location location) {
sourceLat = location.getLatitude();
sourceLng = location.getLongitude();
getRestaurantDetails();
latLng = new LatLng(location.getLatitude(), location.getLongitude());
//latLng = new LatLng(lat, lng);
//Log.wtf("Lat lng", lat + " " + lng);
mGoogleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, 18));
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
}

其中fragment是viewPagerFragment的实例

in xml

<LinearLayout
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="250dp" />