计时器和地图Android

时间:2010-10-31 18:33:11

标签: android google-maps-markers itemizedoverlay

我在地图上显示ItemizedOverlay时遇到了一些麻烦。 在这里,我提取了一个我需要做的简化示例。 这是我的Map类:

import java.util.Timer;
import java.util.TimerTask;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;

public class Map extends MapActivity {
protected static final String TAG = "Map";
private int test = 0;
private TestItemized items = null;
private Timer timer = new Timer();
private MapView map = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    this.map = (MapView) this.findViewById(R.id.myMapView1);
    this.map.setBuiltInZoomControls(true);
    this.map.setSatellite(false);
    this.map.setStreetView(true);
    this.map.setClickable(true);
    this.timer.scheduleAtFixedRate(new TimerTask(){

        @Override
        public void run() {
            Log.d(TAG, "refeshing: "+ String.valueOf(test));
            Drawable drawable = Map.this.getResources().getDrawable(R.drawable.androidmarker);
            test++;
            if (Map.this.items instanceof TestItemized && Map.this.map.getOverlays().contains(Map.this.items))
                Map.this.map.getOverlays().remove(Map.this.items);
            //some code might go here to retrieve new coordinates but this isn't a problem
            Map.this.items = new TestItemized(drawable, Map.this);
            double lat = 46.491734;
            Log.d(TAG,"Latitude : "+ String.valueOf(lat));
            double lng = 11.320365;
            Log.d(TAG,"Longitude : "+ String.valueOf(lng));

            OverlayItem item = new OverlayItem(new GeoPoint((int)(lat * 1E6), (int)(lng * 1E6)), String.valueOf(Map.this.test), "blah!");
            Map.this.items.addOverlay(item);
            Map.this.map.getOverlays().add(Map.this.items);
            Log.d(TAG,"refreshing "+String.valueOf(test)+" is over");
            }}, 1000,60000);        

}

@Override
protected boolean isRouteDisplayed() {
    return false;
}
}

我按照谷歌提供的指南扩展了ItemizedOverlay类

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.widget.Toast;

import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.OverlayItem;


@SuppressWarnings("rawtypes")
public class TestItemized extends ItemizedOverlay {
private Context mCtx;
private List<OverlayItem> mItems = new ArrayList<OverlayItem>();

public TestItemized(Drawable arg0) {
    super(arg0);
}

/**
 * @param defaultMarker
 * @param mCtx
 */
public TestItemized(Drawable defaultMarker, Context mCtx) {
    super(defaultMarker);
    this.mCtx = mCtx;
}

public void addOverlay(OverlayItem overlay) {
    this.mItems.add(overlay);
    populate();
}


@Override
protected OverlayItem createItem(int i) {
    return this.mItems.get(i);
}

@Override
public int size() {
    return mItems.size();
}

/* (non-Javadoc)
 * @see com.google.android.maps.ItemizedOverlay#onTap(int)
 */
@Override
protected boolean onTap(int index) {
    OverlayItem item = this.mItems.get(index);
    Toast.makeText(this.mCtx, item.getTitle() +" : " +item.getSnippet(), Toast.LENGTH_LONG).show();
    return true;

}


} 

从代码中可以清楚地看到,我需要在固定时间更新一个可能有新位置的地图用户界面这样做我正在使用计时器,但我想我可能会陷入某种线程问题我不会知道如何处理,因为在运行测试时,我可以在日志中看到发生了什么但在地图上没有绘制标记。有谁知道如何解决这类问题?

1 个答案:

答案 0 :(得分:3)

在run()方法结束时,在完成MapView的更新后,你应该调用

Map.this.map.postInvalidate();