Draw circle of certain radius on map view in Android

后端 未结 6 789
一向
一向 2020-12-08 11:55

I want to draw a circle on map view. I want the user to input the radius and for that radius I have to show circle on map. After that I have to display markers on some locat

6条回答
  •  眼角桃花
    2020-12-08 12:29

    This isn't perfect, but here's a little code I put together to put a circle on a map. You could easily expand on it to set the colour of the circle etc. Most of the other code samples I've seen haven't taken into account scaling the circle size with the zoom level which is a common requirement when creating circles. Circle Radius is in meters.

    import com.google.android.maps.GeoPoint;
    import com.google.android.maps.MapView;
    import com.google.android.maps.Overlay;
    import com.google.android.maps.Projection;
    
    import android.content.Context;
    import android.graphics.Canvas;
    import android.graphics.Color;
    import android.graphics.Paint;
    import android.graphics.Point;
    public class CircleOverlay extends Overlay {
    
        Context context;
        double mLat;
        double mLon;
        float mRadius;
    
         public CircleOverlay(Context _context, double _lat, double _lon, float radius ) {
                context = _context;
                mLat = _lat;
                mLon = _lon;
                mRadius = radius;
         }
    
         public void draw(Canvas canvas, MapView mapView, boolean shadow) {
    
             super.draw(canvas, mapView, shadow); 
    
             Projection projection = mapView.getProjection();
    
             Point pt = new Point();
    
             GeoPoint geo = new GeoPoint((int) (mLat *1e6), (int)(mLon * 1e6));
    
             projection.toPixels(geo ,pt);
             float circleRadius = projection.metersToEquatorPixels(mRadius);
    
             Paint innerCirclePaint;
    
             innerCirclePaint = new Paint();
             innerCirclePaint.setColor(Color.BLUE);
             innerCirclePaint.setAlpha(25);
             innerCirclePaint.setAntiAlias(true);
    
             innerCirclePaint.setStyle(Paint.Style.FILL);
    
             canvas.drawCircle((float)pt.x, (float)pt.y, circleRadius, innerCirclePaint);
        }
    }
    

提交回复
热议问题