IT_Programming/Android_Java

갤럭시 시리즈 MyLocationOverlay가 오류가 날 때 !

JJun ™ 2011. 11. 10. 14:30

 

MyLocationOverlay를 사용할 때 java.lang.ClassCastException: android.graphics.drawable.BitmapDrawable 오류가 생기면서

어플이 죽는 현상이 있다. 구글링 결과 구글 맵에 표시되는 구글 로고 이미지 파일이 누락되어 생기는 문제라고 한다. 
나 같은 경우에는 Fragment 로  MapView 를 구현할 때 갤럭시 탭 10.1 단말에서 
이와 같은 문제가 발생했었다.

  

 

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.Paint.Style;
import android.graphics.drawable.Drawable;
import android.location.Location;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;
import com.google.android.maps.Projection;

public class FixedMyLocationOverlay extends MyLocationOverlay
{
      private boolean bugged = false;

      private Paint accuracyPaint;
      private Point center;
      private Point left;
      private Drawable drawable;
      private int width;
      private int height;

 

      public FixedMyLocationOverlay(Context context, MapView mapView)
      {
           super(context, mapView);
      }

 

 /**
  * @Override
  * MyLocationOverlay 기능 구현 및 단말 버그 처리 추가 (현재 위치 및 오차 반경 그리기)
  */

     protected void drawMyLocation(Canvas canvas, MapView mapView, Location lastFix,

                                                             GeoPoint myLoc, long when)
     {
          if (!bugged)
          {
               try { super.drawMyLocation(canvas, mapView, lastFix, myLoc, when); } 
               catch (Exception e) { bugged = true; }
          }
  
          if (bugged) // 버그 발생시 .. 
          {
               if (drawable == null)
               {
                      accuracyPaint = new Paint();
                      accuracyPaint.setAntiAlias(true);
                      accuracyPaint.setStrokeWidth(2.0f);
                      

                      // 버그 발생시 대체할 이미지 (mylocation.png)
                      drawable = mapView.getContext().getResources().getDrawable

                                               (R.drawable.mylocation);
                      width = drawable.getIntrinsicWidth();
                      height = drawable.getIntrinsicHeight();
                      center = new Point();
                      left = new Point();
              }
    
              // 오차 반경 그리기
             double latitude = lastFix.getLatitude();
             double longitude = lastFix.getLongitude();
             float accuracy = lastFix.getAccuracy();
             float[] result = new float[1];
             Location.distanceBetween(latitude, longitude, latitude, longitude + 1, result);
   
             float longitudeLineDistance = result[0];
             GeoPoint leftGeo = new GeoPoint((int)(latitude*1e6), (int)((longitude-

                                                                accuracy/longitudeLineDistance)*1e6));
             Projection projection = mapView.getProjection();
             projection.toPixels(leftGeo, left);
             projection.toPixels(myLoc, center);
   
             int radius = center.x - left.x;
             accuracyPaint.setColor(0xff6666ff);
             accuracyPaint.setStyle(Style.STROKE);
             canvas.drawCircle(center.x, center.y, radius, accuracyPaint);

             accuracyPaint.setColor(0x186666ff);
             accuracyPaint.setStyle(Style.FILL);
             canvas.drawCircle(center.x, center.y, radius, accuracyPaint);
      
             drawable.setBounds(center.x - width / 2, center.y - height / 2, center.x + width / 2,

                                               center.y + height / 2);
             drawable.draw(canvas);
         }
     }

}
 

 

 

FixedMyLocationOverlay.java

 

mylocation.png

 


mylocation.png
0.0MB
FixedMyLocationOverlay.java
0.0MB