0

我为我的地图视图做了一个覆盖。我已经绘制了标记以显示发生地震的位置,但我正在尝试绘制文本以在标记旁边显示地震的震级。问题是文本出现了,但是一旦触摸地图(我认为那是执行覆盖的 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

        }
    }
}
4

1 回答 1

0

我认为您的问题是以下行-

 mags.remove(0);

第一次绘制后,您从列表中删除所有量级,因此下一次draw调用mags.isEmpty()将是真实的。

查看您的逻辑,您可能需要重做循环以遍历您的两个并行数组quakeLocationsmags.

类似于更改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)
} 
于 2012-12-22T18:12:24.897 回答