Placing shape into MapView in Android
Is there a way to place a shape (drawable or shape of any kind) that occupies a specific area in MapView (lat/lon area) not pixel area . I need that for GeoPoint Clustering purposes
If that is not possible any guidance to do it with projection coordinates would be greatly appreciated. But using the MapView canvas to do this doesn't seem performance-wise since i recycle my Overlay Items all the time and i wish i could take advantage of that too.
Look atItemizedOverlay
I'm culling from my code, so this probably wont' compile out of the box, but should give you enough to figure it out from here...
extended class:
public class MyOverlay extends ItemizedOverlay<OverlayItem>
{
private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
private Context mContext;
private OverlayItem item;
public MyOverlay(Drawable defaultMarker, Context context)
{
super(boundCenterBottom(defaultMarker));
mContext = context;
}
public void addOverlay(OverlayItem overlay)
{
mOverlays.add(overlay);
}
public void doPopulate()
{
populate();
}
@Override
protected OverlayItem createItem(int i)
{
return mOverlays.get(i);
}
@Override
public int size()
{
return mOverlays.size();
}
}
and then in your activity....
public void addLocations(GeoPoint _center)
{
final GeoPoint center = _center;
mapOverlays = mapView.getOverlays();
Drawable drawable = MyActivity.this.getResources().getDrawable(R.drawable.map_annotation_pin);
itemizedoverlay = new ScoopOverlay(drawable,mContext);
//add as many points as you wish...
itemizedoverlay.addOverlay(
new OverlayItem(new GeoPoint(/*lon lat data here*/));
);
showResults.sendEmptyMessage(0);
}
private Handler showResults = new Handler()
{
@Override
public void handleMessage(Message msg)
{
itemizedoverlay.doPopulate();
mapOverlays.add(itemizedoverlay);
mapView.invalidate();
}
};
所以解决方案是绘制到ItemizedOverlay来保存项目。
@Override
public void draw(Canvas canvas, MapView mapView, boolean shadow) {
super.draw(canvas, mapView, shadow);
// cycle through all overlays
for (int index = 0; index < mOverlays.size(); index++) {
OverlayItemExtended item = mOverlays.get(index);
// Converts lat/lng-Point to coordinates on the screen
GeoPoint point = item.getPoint();
Point ptScreenCoord = new Point();
mapView.getProjection().toPixels(point, ptScreenCoord);
Paint boxPaint = new Paint();
boxPaint.setColor(android.graphics.Color.WHITE);
boxPaint.setStyle(Paint.Style.FILL);
boxPaint.setAlpha(140);
canvas.drawCircle(ptScreenCoord.X, ptScreenCoord.y,
20, boxPaint);
}
}
链接地址: http://www.djcxy.com/p/68568.html
上一篇: 如何在android地图中使用两个覆盖图