0

I have this method to set current location pushpin on map:

private void createPushPinOnMap(Canvas canvas, MapView mapView, BOMPushpin pushpin) {
    Location location = currentBestLocation;
    float accuracy = location.getAccuracy();

    Point pointPts = mapView.getProjection().toPixels(new GeoPoint(pushpin.getLatitude(), 
            pushpin.getLongtitude()), 
            null);
    Bitmap bmp1 = BitmapFactory.decodeResource(
            context.getResources(), R.drawable.pushpin);            
    canvas.drawBitmap(bmp1, pointPts.x-10, pointPts.y-34, null);

}

But I need to change this code, so pushpin size would be somehow equal to accuracy (Like in maps.google.com location pushpin).

Paulius Vindzigelskis
  • 2,121
  • 6
  • 29
  • 41

2 Answers2

0

Why no one told me that androids google api has class named MyLocation, which makes everything .

Paulius Vindzigelskis
  • 2,121
  • 6
  • 29
  • 41
0

Here is the sample code what I use in my GTL application, with a little difference i use circle instead of pushpin. If the accuracy is changed you need to set it in the lastAccuracy. The GPSSettings.IS_SHOW_ACCURACY_MARKER is defalt true.

Here is the code

public class AccuracyOverlay extends Overlay {
    private GeoPoint geoPoint;
    private boolean isNeedClearCanvas = false;
    private float lastAccuracy;

    public void clearCanvas(Canvas canvas) {
        canvas.drawColor(Color.WHITE);
    }

    private void drawAccuracyCircle(Canvas canvas, MapView mapView, GeoPoint geoPoint) {
          // convert point to pixels
        Point screenPoint = new Point();
        mapView.getProjection().toPixels(geoPoint, screenPoint);

        Paint circlePaint = new Paint();
        circlePaint.setAntiAlias(true);
        circlePaint.setStrokeWidth(2.0f);
        circlePaint.setColor(0xff6666ff);
        circlePaint.setStyle(Paint.Style.FILL_AND_STROKE);
        circlePaint.setAlpha(64);

        double lat = geoPoint.getLatitudeE6();
        int radius = (int) mapView.getProjection().metersToEquatorPixels(lastAccuracy);
        canvas.drawCircle(screenPoint.x, screenPoint.y, radius, circlePaint);

    }

    public int metersToRadius(double latitude) {
        return (int) (mapView.getProjection().metersToEquatorPixels(lastAccuracy) * (1/ Math.cos(Math.toRadians(latitude))));
    }       

    public void draw(Canvas canvas, MapView mapView, boolean shadow) {
        if (GPSSettings.IS_SHOW_ACCURACY_MARKER) {
            if (!shadow && getLastLocation() != null) {
                geoPoint = getLastLocation();
            }

            if (geoPoint != null) {
                if (GPSSettings.IS_SHOW_ACCURACY_MARKER && (lastAccuracy > 0)) {
                    drawAccuracyCircle(canvas, mapView, geoPoint);
                }
            }
            if (isNeedClearCanvas) {
                boolean wasOn = GPSSettings.IS_SHOW_ACCURACY_MARKER;
                try {
                    try {
                        if (wasOn)
                            GPSSettings.IS_SHOW_ACCURACY_MARKER = false;
                        geoPoint = null;
                        canvas.drawColor(Color.WHITE);
                    }
                    finally {
                        if (wasOn) {
                            GPSSettings.IS_SHOW_ACCURACY_MARKER = true;
                        }   
                    }
                }
                finally {
                    isNeedClearCanvas = false;
                }
            }
        }
    }

    public AccuracyOverlay() {
        super();
    }


    public void clearMarks() {
        this.isNeedClearCanvas = true;
    }

    public void setLastAccuracy(float lastAccuracy) {
        this.lastAccuracy = lastAccuracy;
    }
}
L. Kvri
  • 1,456
  • 4
  • 23
  • 41