我要建立一个地图活动,最重要的是,我需要显示一些位置点。这些位置是从另一个类一个哈希表中检索。此哈希表的内容总是变化。所以,我希望看到在地图上的活动而移动的位置点作为哈希表被修改。在code如下所示:
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;
}
}
有什么办法刷新覆盖对象,如果我触摸屏幕。我正在考虑把一些功能MarkerOverlay的覆盖的onTap方法。但我认为是没有逻辑,如果我尝试让叠加尝试清除本身?任何建议的想法?
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?
mapView.postInvalidate()将更新地图视图和重绘覆盖。我使用这个带有一个Runnable刷新我的叠加每秒:
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);
}
};