This post covers the way to add some custom Map Overlays of custom shape, lets say circle with some custom color in it.
package com.your.package;
import java.util.ArrayList;
import android.R.color;
import android.app.AlertDialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;
import com.google.android.maps.Projection;
public class LocationOverlay extends ItemizedOverlay<OverlayItem> {
private Context mContext;
private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
private int outerCircleColorCode = 0xFF7600;
/** Paint for drawing the overlay circle. */
private Paint mCirclePaint;
/** Represents screen co-ordinates. */
private final Point mScreenCoords = new Point();
/** Halo color. */
private int mHaloColor;
/** Halo border color. */
private int mHaloBorderColor;
private GeoPoint childLocation;
public LocationOverlay(Drawable arg0, Context context, GeoPoint childLocation) {
super(boundCenterBottom(arg0));
mContext = context;
this.childLocation = childLocation;
}
public void addOverlay(OverlayItem overlay) {
mOverlays.add(overlay);
populate();
}
@Override
protected OverlayItem createItem(int arg0) {
return mOverlays.get(arg0);
}
@Override
public int size() {
return mOverlays.size();
}
@Override
protected boolean onTap(int index) {
OverlayItem item = mOverlays.get(index);
AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
dialog.setTitle(item.getTitle());
dialog.setMessage(item.getSnippet());
dialog.show();
return true;
}
@Override
public void draw(Canvas canvas, MapView mapView, boolean arg2) {
final Projection projection = mapView.getProjection();
projection.toPixels(childLocation, mScreenCoords);
double radiusInMeter = 100;
final float radius = projection.metersToEquatorPixels((float) radiusInMeter);
//Inner white circle
mCirclePaint = new Paint();
mCirclePaint.setAntiAlias(true);
// Draw semi-transparent circle.
mCirclePaint.setAlpha(50);
mCirclePaint.setColor(Color.WHITE);
mCirclePaint.setStyle(Style.STROKE);
mCirclePaint.setStrokeWidth(12);
canvas.drawCircle(mScreenCoords.x, mScreenCoords.y, radius-1, mCirclePaint);
//Outer while circle
mCirclePaint = new Paint();
mCirclePaint.setAntiAlias(true);
// Draw semi-transparent circle.
mCirclePaint.setAlpha(10);
mCirclePaint.setColor(Color.WHITE);
mCirclePaint.setStyle(Style.STROKE);
mCirclePaint.setStrokeWidth(12);
canvas.drawCircle(mScreenCoords.x, mScreenCoords.y, radius+1, mCirclePaint);
mCirclePaint.setColor(outerCircleColorCode); //Orange Color
mCirclePaint.setStyle(Style.STROKE);
mCirclePaint.setAlpha(255);
mCirclePaint.setStrokeWidth(8);
canvas.drawCircle(mScreenCoords.x, mScreenCoords.y, radius, mCirclePaint);
}
}
package com.your.package;
import java.util.ArrayList;
import android.R.color;
import android.app.AlertDialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;
import com.google.android.maps.Projection;
public class LocationOverlay extends ItemizedOverlay<OverlayItem> {
private Context mContext;
private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
private int outerCircleColorCode = 0xFF7600;
/** Paint for drawing the overlay circle. */
private Paint mCirclePaint;
/** Represents screen co-ordinates. */
private final Point mScreenCoords = new Point();
/** Halo color. */
private int mHaloColor;
/** Halo border color. */
private int mHaloBorderColor;
private GeoPoint childLocation;
public LocationOverlay(Drawable arg0, Context context, GeoPoint childLocation) {
super(boundCenterBottom(arg0));
mContext = context;
this.childLocation = childLocation;
}
public void addOverlay(OverlayItem overlay) {
mOverlays.add(overlay);
populate();
}
@Override
protected OverlayItem createItem(int arg0) {
return mOverlays.get(arg0);
}
@Override
public int size() {
return mOverlays.size();
}
@Override
protected boolean onTap(int index) {
OverlayItem item = mOverlays.get(index);
AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
dialog.setTitle(item.getTitle());
dialog.setMessage(item.getSnippet());
dialog.show();
return true;
}
@Override
public void draw(Canvas canvas, MapView mapView, boolean arg2) {
final Projection projection = mapView.getProjection();
projection.toPixels(childLocation, mScreenCoords);
double radiusInMeter = 100;
final float radius = projection.metersToEquatorPixels((float) radiusInMeter);
//Inner white circle
mCirclePaint = new Paint();
mCirclePaint.setAntiAlias(true);
// Draw semi-transparent circle.
mCirclePaint.setAlpha(50);
mCirclePaint.setColor(Color.WHITE);
mCirclePaint.setStyle(Style.STROKE);
mCirclePaint.setStrokeWidth(12);
canvas.drawCircle(mScreenCoords.x, mScreenCoords.y, radius-1, mCirclePaint);
//Outer while circle
mCirclePaint = new Paint();
mCirclePaint.setAntiAlias(true);
// Draw semi-transparent circle.
mCirclePaint.setAlpha(10);
mCirclePaint.setColor(Color.WHITE);
mCirclePaint.setStyle(Style.STROKE);
mCirclePaint.setStrokeWidth(12);
canvas.drawCircle(mScreenCoords.x, mScreenCoords.y, radius+1, mCirclePaint);
mCirclePaint.setColor(outerCircleColorCode); //Orange Color
mCirclePaint.setStyle(Style.STROKE);
mCirclePaint.setAlpha(255);
mCirclePaint.setStrokeWidth(8);
canvas.drawCircle(mScreenCoords.x, mScreenCoords.y, radius, mCirclePaint);
}
}
No comments:
Post a Comment