Я делал нечто подобное некоторое время назад. Однако возникла проблема создания нового потока для каждого изображения, что в итоге привело к сбою. Это было необходимо (вроде как) для меня, но вы могли бы сделать это без этого, если вы не получаете изображения из Интернета. Вот код -
/**
*
* @author Kyle
*/
public class HelloItemizedOverlay extends ItemizedOverlay {
private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
private Context mContext;
public HelloItemizedOverlay(Drawable defaultMarker, Context context) {
super(boundCenterBottom(defaultMarker));
populate();
mContext = context;
}
@Override
public boolean onTouchEvent(MotionEvent event, final MapView mapView) {
//---when user lifts his finger---
if (event.getAction() == MotionEvent.ACTION_UP) {
new Thread(new Runnable() {
public void run() {
ArrayList db = MainActivity.getDB(mContext);
int i=0;
for (Object o : db) {
if(i>40)break;
final ArrayList list = ((ArrayList) o);
final int lon = (int) ((Double.parseDouble("" + list.get(1)) * 1000000.0) + .5);
final int lat = (int) ((Double.parseDouble("" + list.get(2)) * 1000000.0) + .5);
final GeoPoint point = new GeoPoint(lat, lon);
if (isCurrentLocationVisible(mapView, point)) {
Log.i("***", "POINT IS VISIBLE");
if (mOverlays.size() > 40) {
i++;
Log.i("***", i+"");
removeOverlay(0);
}
if (lat != 0) {
Thread t=new Thread(new Runnable() {
public void run() {
final OverlayItem overlayitem = new OverlayItem(point, "" + lat + " " + lon, list.get(0).toString().replace("jpg", "mjpg").replace("image", "video"));
final String url = list.get(0).toString().replace("mjpg", "jpg").replace("video", "image");
Drawable d = MainActivity.getImage(url);
if (d != null) {
d.setBounds(0, 0, 48, 32);
overlayitem.setMarker(d);
addOverlay(overlayitem);
mapView.postInvalidate();
}
}
});
t.setPriority(Thread.MIN_PRIORITY);
t.start();
}
} else {
Log.i("***", "POINT IS OUTSIDE VISIBLE RECT");
}
}
}
}).start();
}
return false;
}
public void addOverlay(OverlayItem overlay) {
mOverlays.add(overlay);
populate();
}
public void removeOverlay(int index) {
try {
mOverlays.remove(index);
} catch (Exception e) {
Log.e("***", e.toString());
}
populate();
}
@Override
protected OverlayItem createItem(int i) {
return mOverlays.get(i);
}
@Override
public int size() {
return mOverlays.size();
}
@Override
protected boolean onTap(int index) {
OverlayItem item = mOverlays.get(index);
return true;
}
}
и основной вид деятельности -
public class MainActivity extends MapActivity {
public static final String mapsApi = "xxxxxxxxxxxxxxxxxx";
public static final String PREFS_NAME = "CamWorldPrefsFile";
ListView myListView = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
final MapView mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
mapView.setSatellite(false);
List<Overlay> mapOverlays = mapView.getOverlays();
Drawable drawable = this.getResources().getDrawable(R.drawable.cam);
// d.setBounds(0, 0, d.getIntrinsicWidth()-50, d.getIntrinsicHeight());
final HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(drawable, this);
final DataBaseHelper myDbHelper = new DataBaseHelper(this.getApplicationContext());
try {
myDbHelper.createDataBase();
} catch (IOException ioe) {
throw new Error("Unable to create database");
}
new Thread(new Runnable() {
public void run() {
try {
//myDbHelper.close();
myDbHelper.openDataBase();
String query = "SELECT * from database;";
ArrayList stringList = myDbHelper.selectRecordsFromDBList(query, null);
myDbHelper.close();
for (int i = 0; i < stringList.size() - 1; i++) {
final ArrayList list = (ArrayList) stringList.get(i);
try {
if (Double.parseDouble("" + list.get(1)) != 0) {
Log.i("***", list.get(0).toString().replace("mjpg", "jpg").replace("video", "image"));
// Log.i("***", list.get(2) + " : ");
// Log.i("***", list.get(3) + " : ");
final int lon = (int) ((Double.parseDouble("" + list.get(1)) * 1000000.0) + .5);
final int lat = (int) ((Double.parseDouble("" + list.get(2)) * 1000000.0) + .5);
final GeoPoint point = new GeoPoint(lat, lon);
if (lat != 0) {
Thread t = new Thread(new Runnable() {
public void run() {
final OverlayItem overlayitem = new OverlayItem(point, "" + lat + " " + lon, list.get(0).toString().replace("jpg", "mjpg").replace("image", "video"));
final String url = list.get(0).toString().replace("mjpg", "jpg").replace("video", "image");
Drawable d = MainActivity.getImage(url);
if (d != null) {
d.setBounds(0, 0, 48, 32);
overlayitem.setMarker(d);
itemizedoverlay.addOverlay(overlayitem);
}
}
});
t.setPriority(Thread.MIN_PRIORITY);
t.start();
}
// ("http://151cam.uoregon.edu/axis-cgi/jpg/image.cgi?resolution=160x120")
}
} catch (Exception e) {
Log.i("***", e.getMessage());
}
}
// mapView.invalidate();
} catch (SQLException sqle) {
throw sqle;
}
}
}).start();
mapOverlays.add(itemizedoverlay);