1

我在Fragment中遇到了一個GoogleMap的奇怪問題。GoogleMap未在ViewPager中加載

我實現了某種菜單項目,通過ViewPager顯示項目。有些項目可以顯示簡短的GoogleMap作爲預覽。這些項目是由ViewPager加載的碎片。爲了顯示前一個和下一個項目,ViewPager也被包裝在一個FrameLayout容器中(如ViewPager with previous and next page boundaries)。

問題是地圖不加載。去年我用Eclipse發佈了一個版本,沒有問題。現在我使用不同的mac並在Android Studio中導入項目。一切工作。我在logcat中的錯誤,但地圖上顯示出來的其他行爲的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: MapView的自定義分段(android MapView in Fragment) 的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> 

enter image description here

方法2: SupportMapFragment在定製的片斷 這裏只有一個黑色的圖,但在左上角顯示谷歌的標誌。在ViewPager中滾動並不像方法1那樣顯示黑色矩形。

我希望有人能幫助我。

+0

你使用簽署,如果你用Eclipse構建您的應用程序作爲一個不同的密鑰庫?你需要在谷歌開發者控制檯中重新配置你的Google API嗎? – Christopher

+0

是一個新的密鑰庫已經建立。但我在控制檯中重新配置了Google Key。該地圖顯示在另外兩個活動中。也許我試着再次安裝eclipse,但我做了一些其他更改。 – corban

回答

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

其中片段您viewPagerFragment實例

在XML

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