Reputation: 983
I have to build a map activity and on top of that, I need to display some location points. These locations are retrieving from a hashtable in another class. The content of this hashtable always changes. So I expect to see those location spots on the Map Activity move as the hashtable is modified. The code is shown below:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mapview);
MapView mapView = (MapView) this.findViewById(R.id.map);
mapView.setStreetView(true);
mc = mapView.getController();
mc.animateTo(...);
mc.setZoom(12);
mapView.invalidate();
List<Overlay> listOfOverlays = mapView.getOverlays();
listOfOverlays.clear();
MarkerOverlay mark = new MarkerOverlay(p);
listOfOverlays.add(mark);
for(Entry<Long,Pair> p: LQTManager.getInstance().ResultTable.entrySet())
{
GeoPoint point = new GeoPoint(
(int) (p.getValue().x * 1E6),
(int) (p.getValue().y * 1E6));//some code is not shown
MarkerOverlay markerOverlay = new MarkerOverlay(point);
listOfOverlays.add(markerOverlay);
}
mapView.postInvalidate();
}
class MarkerOverlay extends com.google.android.maps.Overlay
{
public GeoPoint pt;
public MarkerOverlay(GeoPoint pt){
this.pt=pt;
}
@Override
public boolean draw(Canvas canvas, MapView mapView,
boolean shadow, long when)
{
super.draw(canvas, mapView, shadow);
Point screenPts = new Point();
mapView.getProjection().toPixels(pt, screenPts);
Bitmap bmp = BitmapFactory.decodeResource(
getResources(), R.drawable.pushpin);
canvas.drawBitmap(bmp, screenPts.x-16, screenPts.y-32, null);
return true;
}
}
Is there any way to refresh overlay objects if I touch the screen. I am thinking about put some functions in onTap override method of MarkerOverlay. But I think is is not logically if I try to make the Overlay try to clear itself? Any suggested idea?
Upvotes: 2
Views: 10947
Reputation: 616
mapView.postInvalidate() will update the map view and redraw the overlays. I'm using this with a Runnable to refresh my overlays every second:
private Handler handler = new Handler();
private Runnable refreshTask = new Runnable()
{
public void run()
{
handler.removeCallbacks(this);
mapView.postInvalidate();
handler.postDelayed(this, 1000);
}
};
Upvotes: 7
Reputation: 4637
That might not be perfect but it worked for me, you may move the map a bit forth and back, which updates all overlays afterwards.
mapView.getController().scrollBy(1, 1);
mapView.getController().scrollBy(-1, -1);
Upvotes: 1
Reputation: 6188
When you add or remove an overlayItem you need to call populate() in your MarkerOverlay class.
Upvotes: 0