Drawing shapes in Android MapView independent of the zoom level

后端 未结 4 1693
南方客
南方客 2021-01-31 23:34

I need to draw shapes just like those you would create with custom maps on Google Maps, using Android\'s MapView.

In other words, if I draw a shape on t

4条回答
  •  名媛妹妹
    2021-01-31 23:54

    The radius for drawCircle is in pixels so it make sense that the circle is always the same size. You have to scale the radius based on the zoom level. The example below will graph a geometry from the JTS Topology Suite that will scale.

    public class MapOverlay extends Overlay {
        private static final String TAG = MapOverlay.class.getName();
    
        // Allocate once and reuse
        private final Paint mPaint = new Paint();
        private final Path mPath = new Path();
    
        // Region to highlight
        private Geometry mGeometry;
    
        /**
         * @param  geometry Region to highlight on map
         */
        public MapOverlay(Geometry geometry) {
                // Set region
                mGeometry = geometry;
    
                // Edit paint style
                mPaint.setDither(true);
                mPaint.setColor(Color.rgb(128, 136, 231));
                mPaint.setAlpha(100);
                mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
                mPaint.setStrokeJoin(Paint.Join.ROUND);
                mPaint.setStrokeCap(Paint.Cap.ROUND);
                mPaint.setStrokeWidth(6);
        }
    
    
        /**
         * Draw the overlay over the map.
         * 
         * @see com.google.android.maps.Overlay#draw(Canvas, MapView, boolean)
         */
        @Override
        public void draw(Canvas canvas, MapView mapv, boolean shadow) {
                super.draw(canvas, mapv, shadow);
    
                if (mGeometry != null) {
                        // TODO There could be more than one geometries  
                        Geometry g = mGeometry.getGeometryN(0);
                        final Point p = new Point();
                        boolean first = true;
    
                        mPath.reset();
                        for (Coordinate c : g.getCoordinates()) {
                                // Convert lat/lon to pixels on screen
                                // GeoPoint is immutable so allocation is unavoidable
                                Projection projection = mapv.getProjection();
                                projection.toPixels(new GeoPoint((int) (c.y * 1E6), (int) (c.x * 1E6)), p);
    
                                // Set path starting point to first coordinate
                                // otherwise default start is (0,0)
                                if (first) {
                                        mPath.moveTo(p.x, p.y);
                                        first = false;
                                }
    
                                // Add new point to path
                                mPath.lineTo(p.x, p.y);
                        }
                }
    
                // Draw the path with give paint
                canvas.drawPath(mPath, mPaint);
        }
    

    Adapted from here: MapSelectionOverlay.java

提交回复
热议问题