1

I am displaying map in fragment inside view pager,with detail fragment,for map I have created fragment dynamically. I am using single activity so each screen is represented by a Fragment. NewsDetailsFragment includes a ViewPager (map).

When I navigate to the NewsDetailsFragment initially, the map is loaded, now if I load a new instance of the NewsDetailsFragment like on scrolling pager I am displaying my next record. The ViewPager in the 2nd NewsDetailsFragment wont display the map. In LogCat,Log shows that everything works fine, map is displaying and markers get set. But on place of map showing black screen.

If there is 1 loaded map ViewPager, any more NewsDetailsFragment wont show their maps, only if I press Back till I have no NewsDetailsFragment (hence no ViewPagers running), only then if I open a new instance of NewsDetailsFragment it will load the map.

Please sort out my problem.

  • This is a known issue, take a look on this link. http://stackoverflow.com/questions/14419521/moving-mapfragment-surfaceview-causes-black-background-flickering – osayilgan Dec 10 '13 at 09:08
  • I had the same issue, take a look on this link. This solved the problem. https://github.com/jfeinstein10/SlidingMenu/issues/168#issuecomment-11531681 – osayilgan Dec 10 '13 at 09:28
  • Suppose, I have 4 fragments.On 2nd and 3rd fragment I am displaying map and on remaining two, displaying no map.Senerio is: When I will scroll from 1st fragment to 2nd, at that time on place of map, displaying black screen. Then again, I scroll from 2nd to 1st and 1st to 2nd, map is showing. – Surabhi Jain Dec 10 '13 at 09:34
  • Hi osayilgan, I have seen solutions in the links which you have mentioned. But not useful for me, showing same problem :( – Surabhi Jain Dec 11 '13 at 05:42
  • Is any setting in FragmentTransaction or in view pager? – Surabhi Jain Dec 11 '13 at 05:44
  • I am trying similar function. Do you have any answer? Can you share your answer? thank you. – dhiku Feb 24 '14 at 06:27
  • Upto My RND, It's not possible :(. In Fragment, map take time to load. – Surabhi Jain Mar 04 '14 at 12:53

1 Answers1

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);
}

where fragment is instance of your viewPagerFragment

in xml

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