Notice
Recent Posts
Recent Comments
올해는 머신러닝이다.
[펌]안드로이드 구글 맵 반경그리기 본문
츌처 : http://stackoverflow.com/questions/13991301/android-maps-api-v2-draw-circle
// 1. some variables:
private static final double EARTH_RADIUS = 6378100.0;
private int offset;
// 2. convert meters to pixels between 2 points in current zoom:
private int convertMetersToPixels(double lat, double lng, double radiusInMeters) {
double lat1 = radiusInMeters / EARTH_RADIUS;
double lng1 = radiusInMeters / (EARTH_RADIUS * Math.cos((Math.PI * lat / 180)));
double lat2 = lat + lat1 * 180 / Math.PI;
double lng2 = lng + lng1 * 180 / Math.PI;
Point p1 = YourActivity.getMap().getProjection().toScreenLocation(new LatLng(lat, lng));
Point p2 = YourActivity.getMap().getProjection().toScreenLocation(new LatLng(lat2, lng2));
return Math.abs(p1.x - p2.x);
}
// 3. bitmap creation:
private Bitmap getBitmap() {
// fill color
Paint paint1 = new Paint(Paint.ANTI_ALIAS_FLAG);
paint1.setColor(0x110000FF);
paint1.setStyle(Style.FILL);
// stroke color
Paint paint2 = new Paint(Paint.ANTI_ALIAS_FLAG);
paint2.setColor(0xFF0000FF);
paint2.setStyle(Style.STROKE);
// icon
Bitmap icon = BitmapFactory.decodeResource(YourActivity.getResources(), R.drawable.blue);
// circle radius - 200 meters
int radius = offset = convertMetersToPixels(lat, lng, 200);
// if zoom too small
if (radius < icon.getWidth() / 2) {
radius = icon.getWidth() / 2;
}
// create empty bitmap
Bitmap b = Bitmap.createBitmap(radius * 2, radius * 2, Config.ARGB_8888);
Canvas c = new Canvas(b);
// draw blue area if area > icon size
if (radius != icon.getWidth() / 2) {
c.drawCircle(radius, radius, radius, paint1);
c.drawCircle(radius, radius, radius, paint2);
}
// draw icon
c.drawBitmap(icon, radius - icon.getWidth() / 2, radius - icon.getHeight() / 2, new Paint());
return b;
}
// 4. calculate image offset:
private LatLng getCoords(double lat, double lng) {
LatLng latLng = new LatLng(lat, lng);
Projection proj = YourActivity.getMap().getProjection();
Point p = proj.toScreenLocation(latLng);
p.set(p.x, p.y + offset);
return proj.fromScreenLocation(p);
}
// 5. draw:
MarkerOptions options = new MarkerOptions();
options.position(getCoords(lat, lng));
options.icon(BitmapDescriptorFactory.fromBitmap(getBitmap()));
marker = YourActivity.getMap().addMarker(options);
and result:
'Android > Tip&Tech' 카테고리의 다른 글
List of Android Top 1000 Libraries (0) | 2016.06.08 |
---|---|
줌인되는 커스텀 리스트 뷰 소스 (0) | 2015.09.04 |
Android ListView CheckBox 스크롤후 없어지는 문제 (0) | 2015.01.24 |
Android 갤러리에서 filepath(Action_PICK) (0) | 2015.01.22 |
Android StrictMode Mode 설정/해제하기 (0) | 2015.01.19 |