我为我的mapview做了一个叠加层。我已经绘制了标记来显示地震发生的地方,但我正在尝试绘制文本以显示标记旁边的地震的大小。问题是文本出现但是一旦触摸了地图(我认为这是执行覆盖的onDraw时),文本就会消失。这是我的代码。任何人将不胜感激。这是我的代码:
import java.util.ArrayList;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.Projection;
public class EarthquakeOverlay extends Overlay {
Context c;
int counter = 0;
Cursor earthquakes;
ArrayList<GeoPoint> quakeLocations;
ArrayList<String> mags;
int rad = 5;
String g;
public EarthquakeOverlay(Cursor cursor, Context con) {
super();
earthquakes = cursor;
c = con;
mags = new ArrayList<String>();
quakeLocations = new ArrayList<GeoPoint>();
refreshQuakeLocations();
}
public void swapCursor(Cursor cursor) {
earthquakes = cursor;
refreshQuakeLocations();
}
private void refreshQuakeLocations() {
quakeLocations.clear();
if (earthquakes != null && earthquakes.moveToFirst())
do {
int magIndex = earthquakes
.getColumnIndexOrThrow(EarthquakeProvider.KEY_MAGNITUDE);
String mag = earthquakes.getString(magIndex);
mags.add(mag);
int latIndex = earthquakes
.getColumnIndexOrThrow(EarthquakeProvider.KEY_LOCATION_LAT);
int lngIndex = earthquakes
.getColumnIndexOrThrow(EarthquakeProvider.KEY_LOCATION_LNG);
Double lat = earthquakes.getFloat(latIndex) * 1E6;
Double lng = earthquakes.getFloat(lngIndex) * 1E6;
GeoPoint geoPoint = new GeoPoint(lat.intValue(), lng.intValue());
quakeLocations.add(geoPoint);
} while (earthquakes.moveToNext());
}
public void draw(Canvas canvas, MapView mapView, boolean shadow) {
Projection projection = mapView.getProjection();
Bitmap flag = BitmapFactory.decodeResource(c.getResources(),
R.drawable.marker);
Paint bitmapPaint = new Paint();
bitmapPaint.setFilterBitmap(false);
bitmapPaint.setAntiAlias(true);
Point globalPoint = new Point();
// Create and setup your paint brush
Paint paint = new Paint();
paint.setColor(Color.BLUE);
paint.setAntiAlias(true);
paint.setFakeBoldText(true);
if (shadow == false) {
for (GeoPoint point : quakeLocations) {
Point myPoint = new Point();
projection.toPixels(point, myPoint);
globalPoint = myPoint;
if (c != null) {
canvas.drawBitmap(flag, myPoint.x - rad, myPoint.y - rad,
bitmapPaint);
if(mags.isEmpty()){
// do nothing
}else{
canvas.drawText(mags.get(0), myPoint.x - rad, myPoint.y - rad,
paint);
mags.remove(0);
}
}// end of if stetment
}// end of quakeLocations for statment
}
}
}
答案 0 :(得分:0)
我认为你的问题是以下几行 -
mags.remove(0);
第一次绘制后,您将从列表中删除所有大小,因此下次draw
被称为mags.isEmpty()
时将是真实的。
查看您的逻辑,您可能需要重做循环以遍历两个并行数组quakeLocations
和mags
。
将for (GeoPoint point : quakeLocations)
更改为
final int numQuakes = 0;
for(int i=0; i < numQuakes; i++) {
// in here use quakeLocations.get(i) and mags.get(i)
}