2015-10-09 130 views
5

我正在開發一個android應用程序。在這個應用程序中,我在片段中顯示MapView。如果我的設備是旋轉的,我嘗試使用以下方法之一,將攝像機:IllegalStateException:填充後視圖大小太小

mapa.animateCamera(cameraUpdate);

mapa.moveCamera(cameraUpdate);

然後,我的應用程序崩潰,並顯示以下錯誤:

java.lang.IllegalStateException: View size is too small after padding 
at com.google.a.a.ab.b(Unknown Source) 
at com.google.maps.api.android.lib6.gmm6.c.a.a(Unknown Source) 
at com.google.maps.api.android.lib6.gmm6.c.a.a(Unknown Source) 
at com.google.maps.api.android.lib6.d.dw.a(Unknown Source) 
at com.google.maps.api.android.lib6.gmm6.c.a.a(Unknown Source) 
at com.google.maps.api.android.lib6.d.et.a(Unknown Source) 
at com.google.android.gms.maps.internal.j.onTransact(SourceFile:83) 
at android.os.Binder.transact(Binder.java:380) 
at com.google.android.gms.maps.internal.IGoogleMapDelegate$zza$zza.moveCamera(Unknown Source) 
at com.google.android.gms.maps.GoogleMap.moveCamera(Unknown Source) 
at com.accionplus.dbdplus.utils.MapUtils.centrarCamara(MapUtils.java:88) 
at com.accionplus.dbdplus.utils.MapUtils.centrarCamara(MapUtils.java:52) 
at com.accionplus.dbdplus.controladores.pdv.RutaProgramadaFragment$4.onMyLocationButtonClick(RutaProgramadaFragment.java:274) 
at com.google.android.gms.maps.GoogleMap$3.onMyLocationButtonClick(Unknown Source) 
at com.google.android.gms.maps.internal.zzo$zza.onTransact(Unknown Source) 
at android.os.Binder.transact(Binder.java:380) 
at com.google.android.gms.maps.internal.be.a(SourceFile:81) 
at com.google.maps.api.android.lib6.d.ak.onClick(Unknown Source) 
at android.view.View.performClick(View.java:5156) 
at android.view.View$PerformClick.run(View.java:20755) 
at android.os.Handler.handleCallback(Handler.java:739) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:145) 
at android.app.ActivityThread.main(ActivityThread.java:5832) 
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194) 

我使用了Google,但沒有成功。我剛剛發現這是谷歌的一個報告問題,但我不知道是否有任何解決方法來「解決」這個問題。任何人都可以幫助我?

編輯:我加入更多的代碼

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       xmlns:tools="http://schemas.android.com/tools" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       tools:context="com.accionplus.dbdplus.controladores.pdv.RutaProgramadaActivity"> 

    <LinearLayout 
     android:id="@+id/card_container" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="#15000000" 
     android:orientation="vertical"/> 

    <LinearLayout 
     android:id="@+id/filtro_ll" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/card_container" 
     android:layout_margin="10dp" 
     android:gravity="left|top" 
     android:orientation="horizontal" 
     android:weightSum="1"> 

     <LinearLayout 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="0.7" 
      android:gravity="left|top" 
      android:orientation="vertical"> 

      <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:gravity="left" 
       android:orientation="horizontal" 
       android:weightSum="1"> 

       <ImageButton 
        android:id="@+id/seleccionar_fecha_btn" 
        android:layout_width="wrap_content" 
        android:layout_height="35dp" 
        android:background="@drawable/button" 
        android:src="@drawable/ic_date" 
        android:text="@string/buscar" 
        android:textColor="@color/color_text_primary"/> 

       <TextView 
        android:id="@+id/fecha_consulta_tv" 
        android:layout_width="match_parent" 
        android:layout_height="35dp" 
        android:layout_alignParentTop="true" 
        android:layout_marginLeft="5dp" 
        android:background="@drawable/rectangle" 
        android:gravity="center"/> 
      </LinearLayout> 

      <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginTop="10dp" 
       android:background="@drawable/rectangle"> 

       <Spinner 
        android:id="@+id/tipo_ruta_sp" 
        android:layout_width="fill_parent" 
        android:layout_height="35dp" 
        android:layout_gravity="center" 
        /> 
      </LinearLayout> 

     </LinearLayout> 
    </LinearLayout> 

    <com.google.android.gms.maps.MapView 
     android:id="@+id/map" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_below="@+id/filtro_ll"/> 

    <ProgressBar 
     android:id="@+id/progress_bar" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true" 
     android:foregroundGravity="center|top" 
     android:visibility="gone"/> 

</RelativeLayout> 

這裏時MyLocationButton點擊'S:

mMap.setOnMyLocationButtonClickListener(new GoogleMap.OnMyLocationButtonClickListener() { 
      @Override 
      public boolean onMyLocationButtonClick() { 
       MapUtils.centrarCamara(getActivity(), mMap, mPuntosFinales); 
       return true; 
      } 
     }); 

這裏是MapUtils.centrarCamara()方法:

public static void centrarCamara(Context contexto, GoogleMap mapa, LatLng... posiciones) { 
     centrarCamara(contexto, mapa, Boolean.TRUE, posiciones); 
    } 

    public static void centrarCamara(Context contexto, GoogleMap mapa, boolean animado, 
            LatLng... posiciones) { 
     if (mapa == null) { 
      return; 
     } 

     LatLngBounds.Builder builder = new LatLngBounds.Builder(); 
     for (LatLng posicion : posiciones) { 
      builder.include(posicion); 
     } 

     LatLngBounds bounds = builder.build(); 

     CameraUpdate cu = CameraUpdateFactory 
       .newLatLngBounds(bounds, ViewUtils.dpAPixeles(MARKERS_DP_OFFSET, contexto)); 

     if (animado) { 
      mapa.animateCamera(cu); 
     } else { 
      mapa.moveCamera(cu); 
     } 
    } 

ViewUtils.dpAPixeles:

public static int dpAPixeles(int dp, Context contexto) { 
    Resources r = contexto.getResources(); 
    int px = (int) TypedValue 
      .applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, r.getDisplayMetrics()); 

    return px; 
} 

當調用mapa.moveCamera()mapa.animateCamera()時會發生異常;

+0

你可以發佈你用來創建'cameraUpdate'對象的代碼嗎? – antonio

+0

謝謝你的回答,@antonio。我已發佈更多代碼 – Antonio

回答

4

the documentation

Do not change the camera with this camera update until the map has undergone layout (in order for this method to correctly determine the appropriate bounding box and zoom level, the map must have a size). Otherwise an IllegalStateException will be thrown. It is NOT sufficient for the map to be available (i.e. getMap() returns a non-null object); the view containing the map must have also undergone layout such that its dimensions have been determined. If you cannot be sure that this has occured, use newLatLngBounds(LatLngBounds, int, int, int) instead and provide the dimensions of the map manually.

因此,您的問題可能是地圖上沒有的尺寸,當你在呼喚mapa.animateCamera(cameraUpdate)mapa.moveCamera(cameraUpdate)

無論如何,你說,當你旋轉你的設備異常被拋出,我會說,真正的問題是,調用newLatLngBoundsViewUtils.dpAPixeles(MARKERS_DP_OFFSET, contexto))當您正在使用的填充是造成問題(也許填充是大於視圖的高度/ 2?)。與Google地圖相關的註冊問題(4773)可能會幫助您解決問題。如上所述,一種可能的解決方法是根據方向設置cameraUpdate:

if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) { 
    cu = CameraUpdateFactory.newLatLngBounds(bounds, ViewUtils.dpAPixeles(MARKERS_DP_OFFSET, contexto)); 
} else { 
    // Use another method to get your camera update, for example CameraUpdateFactory.newLatLng(latLng); 
} 
+0

感謝您的回答,@antonio。我還沒有找到其他方法叫'CameraUpdate.newLatLng(latLng);'來更新相機。你能舉一個例子來更新相機嗎? – Antonio

+0

@Antonio糟糕...你是對的,我的錯誤,我的意思是CameraUpdateFactory.newLatLng(latLng)(https://developers.google.com/android/reference/com/google/android/gms/maps/CameraUpdateFactory#newLatLng (com.google.android.gms.maps.model.LatLng)) – antonio

+0

謝謝@antonio。您的答案完美無缺 – Antonio

0

設置填充300到150.它爲我工作。

map.animateCamera(
       CameraUpdateFactory.newLatLngBounds(boundsLatLongs, 150), 
       1000, null); 
+1

如果地圖放置在小尺寸的容器中,它將無法正常工作。 – Kaizie

2

我已經嘗試了很多的解決方案,但沒有人真的解決我的問題這是填充超過地圖的實際大小。

我的解決方案是,但一個小填充,然後縮小,以提供相同的結果。

這段代碼來自我的應用程序,它適用於我。

public static void fitThePoints(GoogleMap map, ArrayList<LatLng> points) { 
    //Calculate the markers to get their position 
    LatLngBounds.Builder b = new LatLngBounds.Builder(); 
    for (LatLng point : points) b.include(point); 

    //Change the padding as per needed 
    map.moveCamera(CameraUpdateFactory.newLatLngBounds(b.build(), ConvertUtils.dp2px(30))); 
    map.moveCamera(CameraUpdateFactory.zoomTo(map.getCameraPosition().zoom - 0.5f)); 
} 
0

曾與react-native-maps

解決方案中的錯誤是給地圖視圖分量的最小寬度和高度,因爲它是被佈置其父視圖以前。