본문 바로가기
컴퓨터관련

안드로이드 마커 그리기

by 기록이답이다 2014. 1. 7.
반응형

 List<ImageView> ivList = new ArrayList<ImageView>();
 private void showItemizedOverlay(List<CommunityItem> commList) {
  List<Overlay> overlays = mapView.getOverlays();
  overlays.clear();
  for(int o=0; o<ivList.size(); o++) {
   mapView.removeView(ivList.get(o));
  }
  
  for(CommunityItem item : commList) {
   if(item == null) continue;
   if(item.lat == null || item.lng == null || item.lat.length() == 0 || item.lng.length() == 0) continue;
   Double commLat = Double.valueOf(item.lat) * 1E6;
   Double commLng = Double.valueOf(item.lng) * 1E6;
   
   GeoPoint geoPoint = new GeoPoint(commLat.intValue(), commLng.intValue());
   
   Projection projection = mapView.getProjection();
   Point point = new Point();
   projection.toPixels(geoPoint, point);

   Random rand = new Random(System.currentTimeMillis());
   int randNum = rand.nextInt(3);
   
   Bitmap marker = BitmapFactory.decodeResource(getResources(), R.drawable.icon_comm_interest);
   if(randNum == 1) marker = BitmapFactory.decodeResource(getResources(), R.drawable.icon_comm_music);
   if(randNum == 2) marker = BitmapFactory.decodeResource(getResources(), R.drawable.icon_comm_movie);
   GeoPoint textGeoPoint = projection.fromPixels(point.x + (marker.getWidth()/2) , point.y + (marker.getHeight()/2));
   
   MapView.LayoutParams lp = new MapView.LayoutParams(marker.getWidth(), marker.getHeight(),textGeoPoint, MapView.LayoutParams.CENTER);
   ImageView iv = communityAction.getImageView(item); // marker 클릭시 액션 실행하기 위해서...
   mapView.addView(iv, lp);
   ivList.add(iv);

      String selectCommunityId = intent.getStringExtra("selectCommunityId");
      if(selectCommunityId == null) selectCommunityId = "";
      
   boolean sweep = false;
   if(loginUserSweep == false) {
    if(selectCommunityId.equals(item.id)) sweep = true;
    else sweep = false;
   }   
   myOverlay = new CommunityOverlay(LoginUser.item.userId, item, marker, geoPoint, iv, sweep);
   overlays.add(myOverlay);
  } // end for
 } 

 

--------------------------------------

CommunityOverlay.java

 

package com.tab.community.map;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.RectF;
import android.graphics.Paint.Style;
import android.os.SystemClock;
import android.widget.ImageView;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.Projection;
import com.tab.model.CommunityItem;

public class CommunityOverlay extends Overlay {
 private Bitmap marker;
 private String userId;
 private ImageView iv;
 private CommunityItem item;
 private GeoPoint geoPoint;
 private long mSweepTime;
 private Paint mSweepPaint;
 private boolean mSweepBefore;
 private long mBlipTime;
 private boolean sweep;
 public CommunityOverlay(String userId, CommunityItem item, Bitmap marker, GeoPoint geoPoint, ImageView iv, boolean sweep) {
  this.marker = marker;
  this.item = item;
  this.userId = userId;
  this.iv = iv;
  this.geoPoint = geoPoint;
  this.sweep = sweep;
  
  mSweepPaint = new Paint();
  mSweepPaint.setColor(0x55009900);
  mSweepPaint.setAntiAlias(true);
  mSweepPaint.setStyle(Style.STROKE);
  mSweepPaint.setStrokeWidth(5);

  mSweepTime = SystemClock.uptimeMillis();  
 }

 private Point getTextPoint(Projection projection, Point point, int markerHeight) {
  GeoPoint textGeoPoint = projection.fromPixels(point.x - (marker.getWidth()/2), point.y + (markerHeight/2));
  Point textPoint = new Point();
  projection.toPixels(textGeoPoint, textPoint);
  
  return textPoint;
 } 
 
 private void drawMarker(Canvas canvas, Point point, Paint textPaint) {
  canvas.drawBitmap(marker, point.x, point.y, textPaint);  
 }
 
 private void initTextPaint(Paint textPaint) {
  if(item.dcJoin == true) textPaint.setARGB(250, 210, 210, 0);
  else textPaint.setARGB(250, 255, 255, 255);
  textPaint.setAntiAlias(true);
  textPaint.setFakeBoldText(true);
 }

 private void drawText(Canvas canvas, int markerHeight, Point textPoint, Point rectPoint, Paint textPaint, Paint backPaint) {
  int widthTmp = 10;
  int bottomTmp = 3;
  int left = rectPoint.x - marker.getWidth() / 2;
  int top = rectPoint.y ;
  int right = rectPoint.x + getTextWidth(textPaint, item.name) + (widthTmp * 2) + widthTmp ;
  int bottom = rectPoint.y + 20;
  RectF backRect = new RectF(left, top, right, bottom);
  canvas.drawRoundRect(backRect, 5, 5, backPaint);
  canvas.drawText(item.name, (textPoint.x -(marker.getWidth()/2)) + widthTmp, (textPoint.y + markerHeight) - bottomTmp, textPaint);
 }
 
 public void draw(Canvas canvas, MapView mapView, boolean shadow) {
  Projection projection = mapView.getProjection();
  if(shadow == false) {
   Point point = new Point();
   projection.toPixels(geoPoint, point);

   int markerHeight = marker.getHeight();
   
   Point textPoint = getTextPoint(projection, point, markerHeight);
   Point rectPoint = new Point(textPoint.x - (marker.getWidth()/2), textPoint.y + (markerHeight/2));
   
   Paint textPaint = new Paint();
   initTextPaint(textPaint);
   
   Paint backPaint = new Paint();
   backPaint.setARGB(175, 50, 50, 50);
   backPaint.setAntiAlias(true);
   
   // left, top, right, bottom
   drawMarker(canvas, point, textPaint);
   drawText(canvas, markerHeight, textPoint, rectPoint, textPaint,backPaint);
   
   if(iv != null) {
    GeoPoint viewGeoPoint = projection.fromPixels(point.x + (marker.getWidth()/2) , point.y + (markerHeight/2));
    MapView.LayoutParams lp = new MapView.LayoutParams(marker.getWidth(), marker.getHeight(),viewGeoPoint, MapView.LayoutParams.CENTER);
    iv.setLayoutParams(lp);
   }
   
   if(sweep) {
//    drawOval(canvas, point);
          int centerX = point.x + (marker.getWidth()/2);
          int centerY = point.y + (marker.getHeight()/2);
          int radius = centerX - 8;
          float mDistanceRatio = 100;
          int blipRadius = (int) (mDistanceRatio * radius);
         
    final long now = SystemClock.uptimeMillis();
          if (mSweepTime > 0) {
              // Draw the sweep. Radius is determined by how long ago it started
              long sweepDifference = now - mSweepTime;
              if (sweepDifference < 1000L) {
                  int sweepRadius = (int) (((radius + 6) * sweepDifference) >> 9);
                  canvas.drawCircle(centerX, centerY, sweepRadius, mSweepPaint);
                 
                  // Note when the sweep has passed the blip
                  boolean before = sweepRadius < blipRadius;
                  if (!before && mSweepBefore) {
                      mSweepBefore = false;
                      mBlipTime = now;
                  }
              } else {
                  mSweepTime = now + 1000;
                  mSweepBefore = true;
              }
              mapView.postInvalidate();
          }    
   }   
  }
 }

 private int getTextWidth(Paint textPaint, String text) {
  int result = 0;
  float[] textArr = new float[text.getBytes().length];
  textPaint.getTextWidths(text, textArr);
  for(int i=0; i<textArr.length; i++) {
   result += textArr[i];
  }
  return result;
 }
 
 protected boolean onTap(int index) {
  return true;
 }  
} // end class

 결과는 아래와 같다.

설명은 나중에...

 

 

 

 

 

반응형