我在Fragment中遇到了一个奇怪的问题。

我实现了某种菜单,其中包含通过ViewPager显示的项目。有些项目可以显示简短的GoogleMap作为预览。 “项目是片段”由ViewPager加载。为了显示上一个和下一个项目,ViewPager也被包装在FrameLayout容器中(例如ViewPager with previous and next page boundaries)。

问题在于该地图无法加载。去年,我使用Eclipse发布了一个版本,没有问题。现在,我使用另一台Mac,并将该项目导入Android Studio。一切正常。我在logcat中遇到错误,但是map在其他使用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


方法1:
自定义片段(android MapView in Fragment)中的MapView
显示了MapView,但您只能看到地图的网格。在ViewPager中滚动时,地图所在的位置会出现一个黑色矩形。

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




方法2:
自定义Fragment中的SupportMapFragment
这里只有黑色视图,但在左上角显示有Google徽标。在ViewPager中滚动不会像方法1中那样显示黑色矩形。

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


我希望有人能帮助我。

最佳答案

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的实例

在xml中

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

关于android - GoogleMap不会在ViewPager中加载,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30122131/

10-13 03:34