corban
corban

Reputation: 628

GoogleMap does not load inside ViewPager

I got a weird problem with a GoogleMap in Fragment.

I implemented some sort of a menu with items displayed via ViewPager. Some items can show a short GoogleMap as preview. The Items are Fragments wich are loaded by ViewPager. To show previous and next items the ViewPager is also wrapped inside a FrameLayout Container (like ViewPager with previous and next page boundaries).

The problem is that the map does not load. Last year I released a version with Eclipse and there was no problem. Now I use a different mac and imported the project in Android Studio. All things worked. I got an error in logcat, but map shows up in other activity with MapFragment:

E/dalvikvm﹕ Could not find class 'android.app.AppOpsManager', referenced from method com.google.android.gms.common.GooglePlayServicesUtil.zza
E/dalvikvm﹕ Could not find class 'android.app.AppOpsManager', referenced from method com.google.android.gms.common.hg.a

Method 1: MapView in custom Fragment (android MapView in Fragment) The MapView was shown but you can only see the grid of the map. When you scroll in the ViewPager a black rectangle appears on the place where the map has been.

<LinearLayout
    android:id="@+id/itemInfo"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/itemSubTitle"
    android:layout_centerHorizontal="true"
    android:orientation="vertical"
    >    


    <TextView
    android:id="@+id/itemDistanceInfo"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="0dp"
    android:paddingTop="0dp"
    android:textSize="6pt"
    android:textColor="@color/white"
    android:text="ca. 89 km"
    android:visibility="gone"
        />

        <RelativeLayout
    android:id="@+id/itemInfoPrice"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:background="@color/white“
    android:layout_marginTop="8dp"
    android:padding="5dp"
    android:visibility="gone"
    >

    <include layout="@layout/price_menu_fragment"/>

    </RelativeLayout>


<FrameLayout
    android:id="@+id/itemInfoPicture"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:background="@color/white“
    android:layout_marginTop="8dp"
    android:padding="5dp"
    android:visibility="visible"
    >

    <ImageView
        android:id="@+id/itemImage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:src="@drawable/menue_info"
        android:visibility="invisible"
         />

    <com.google.android.gms.maps.MapView
        android:id="@+id/mapview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

</FrameLayout>
</LinearLayout>

enter image description here

Method 2: SupportMapFragment in custom Fragment Here there is only a black view but with Google logo shown in the left corner. Scrolling in the ViewPager does not show a black rectangle like in method 1.

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

I hope that someone can help me out.

Upvotes: 1

Views: 639

Answers (1)

Laxminarayan Nayak
Laxminarayan Nayak

Reputation: 215

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" />

Upvotes: 2

Related Questions