문제

I created a class MiOverlay that extends from Overlay.

And it doesn't recognize the getResources method.. What do I have to do. Here the full code of my class

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Point;
import android.location.Location;

import com.google.android.maps.MapView;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.Overlay;


public class MiOverlay extends Overlay {
    GeoPoint point;

    public MiOverlay(GeoPoint point)
    {
        super();
        this.point = point;
    }

    @Override
    public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when){
        super.draw(canvas, mapView, shadow);
        Point scrnPoint = new Point();
        mapView.getProjection().toPixels(this.point, scrnPoint);
        Bitmap marker = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
        canvas.drawBitmap(marker, scrnPoint.x - marker.getWidth() / 2, scrnPoint.y - marker.getHeight() /2 , null);
        return true;
    }
도움이 되었습니까?

해결책

I disagree with Luis Lavieri's answer. Your easiest solution would be to use the MapView's context:

Bitmap marker = BitmapFactory.decodeResource(mapView.getContext().getResources(), R.drawable.ic_launcher);

Easy and no potential memory leaks.

다른 팁

You are in an Non-Activity Class, so you must refer to what is discussed in this question. However, it is not recommended to pass the Context due to a possible memory leaking. Try to implement your resources within your Activity using getResources() or if you are using Fragments, use getActivity().getResources()...

Anyway, your easiest option would be:

private Context context;
public MiOverlay(GeoPoint point, Context _context)
{
    super();
    this.point = point;
    context = _context;
}

@Override
public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when){
    super.draw(canvas, mapView, shadow);
    Point scrnPoint = new Point();
    mapView.getProjection().toPixels(this.point, scrnPoint);
    Bitmap marker = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_launcher);
    canvas.drawBitmap(marker, scrnPoint.x - marker.getWidth() / 2, scrnPoint.y - marker.getHeight() /2 , null);
    return true;
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top