2015-10-09 19 views
5

Tôi đang phát triển một ứng dụng Android. Trong ứng dụng này, tôi đang hiển thị một MapView trong một đoạn. Nếu điện thoại của tôi là xoay và tôi cố gắng để di chuyển camera bằng một trong những phương pháp sau đây:IllegalStateException: Kích thước xem quá nhỏ sau khi đệm

mapa.animateCamera(cameraUpdate);

hoặc

mapa.moveCamera(cameraUpdate);

Sau đó treo ứng dụng của tôi và hiển thị lỗi sau:

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) 

Tôi đã googled nó nhưng không thành công. Tôi chỉ đã thấy rằng nó đã được báo cáo vấn đề cho google nhưng tôi không biết nếu có bất kỳ workaround để "giải quyết" vấn đề này. Bất kỳ ai có thể giúp tôi?

EDIT: Tôi thêm nhiều mã

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

Dưới đây là khi MyLocationButton được nhấp:

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

Dưới đây là MapUtils.centrarCamara() phương pháp:

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

Trường hợp ngoại lệ xảy ra khi mapa.moveCamera() hoặc mapa.animateCamera() được gọi;

+0

Bạn có thể đăng mã mà bạn đang sử dụng để tạo đối tượng 'cameraUpdate' của mình không? – antonio

+0

Cảm ơn bạn đã trả lời, @antonio. Tôi đã đăng thêm mã – Antonio

Trả lời

4

Từ 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.

Như vậy, vấn đề của bạn có thể là bản đồ không có một kích thước khi bạn đang gọi mapa.animateCamera(cameraUpdate) hoặc mapa.moveCamera(cameraUpdate).

Dù sao, khi bạn nói rằng ngoại lệ được ném khi bạn xoay thiết bị, tôi sẽ nói rằng vấn đề thực sự là padding mà bạn đang sử dụng khi gọi newLatLngBounds (ViewUtils.dpAPixeles(MARKERS_DP_OFFSET, contexto)) đang gây ra sự cố (có thể đệm là lớn hơn chiều cao của Chế độ xem/2?). Đã xảy ra sự cố đã đăng ký (4773) liên quan đến Google Maps có thể giúp bạn giải quyết vấn đề của mình. Như đã nêu ở đó, một giải pháp thay thế có thể là đặt chế độ Cập nhật máy ảnh tùy theo hướng:

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

Cảm ơn bạn đã trả lời, @antonio. Tôi đã không tìm thấy phương pháp khác được gọi là 'CameraUpdate.newLatLng (latLng);' để cập nhật camera. Bạn có thể đăng một ví dụ để cập nhật máy ảnh không? – Antonio

+0

@Antonio Rất tiếc ... bạn đúng, sai lầm của tôi, ý tôi là 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

Cảm ơn bạn, @antonio. Câu trả lời của bạn hoạt động hoàn hảo – Antonio

0

Đặt đệm 300 đến 150. nó làm việc cho tôi.

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

Nó sẽ không hoạt động nếu bản đồ được đặt trong một thùng chứa có kích thước nhỏ. – Kaizie

2

Tôi đã thử rất nhiều giải pháp nhưng không có giải pháp nào thực sự giải quyết được vấn đề của tôi mà phần đệm vượt quá kích thước thực tế của bản đồ.

giải pháp của tôi là nhưng một phần đệm nhỏ và sau đó thu nhỏ để đưa ra kết quả tương tự.

mã này là từ ứng dụng của tôi và nó hoạt động tốt cho tôi.

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

Đã lỗi với react-native-maps

Giải pháp là để cung cấp cho các thành phần xem bản đồ có chiều rộng tối thiểu và chiều cao, vì nó đã được đặt ra trước khi xem mẹ của nó là gì.

Các vấn đề liên quan