Custom, Disabled and Blank Map in android

后端 未结 1 1866
你的背包
你的背包 2020-12-11 10:14

I want to display only the radius part of marker on map and rest of the map must be blank. Like if Marker radius is 5 KM and i want that only that radius part of the map mus

1条回答
  •  醉梦人生
    2020-12-11 10:54

    You can add a View over the map that hides it and draw a circle on it to show only the desired locations.

    I have based my answers on

    • Draw transparent circle filled outside

    • Android - Google Maps inside CircleView

    • How can I draw a static target circle on Google Maps?

    MapsActivity:

    public class MapsActivity extends FragmentActivity implements GoogleMap.OnCameraChangeListener {
        private GoogleMap mMap;
        private HideOverlayView hideView;
        private List visibleMarkers = new ArrayList<>();
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_maps);
    
            hideView = (HideOverlayView) findViewById(R.id.hideview);
    
            setUpMapIfNeeded();
        }
    
        @Override
        protected void onResume() {
            super.onResume();
            setUpMapIfNeeded();
        }
    
        @SuppressLint("NewApi")
        private void setUpMapIfNeeded() {
            if (mMap == null) {
                final SupportMapFragment f = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
                mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
                        .getMap();
                if (mMap != null) {
                    setUpMap();
                }
            }
        }
    
        private void setUpMap() {
            mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
            mMap.getUiSettings().setAllGesturesEnabled(true);
            mMap.getUiSettings().setZoomControlsEnabled(true);
            mMap.getUiSettings().setMyLocationButtonEnabled(true);
            mMap.setMyLocationEnabled(true);
            mMap.setOnCameraChangeListener(this);
    
            mMap.moveCamera(CameraUpdateFactory.newCameraPosition(
                    CameraPosition.fromLatLngZoom(new LatLng(40.22861, -3.95567), 15)));
    
            visibleMarkers.add(mMap.addMarker(new MarkerOptions().position(
                    new LatLng(40.22861, -3.95567))));
            visibleMarkers.add(mMap.addMarker(new MarkerOptions().position(
                    new LatLng(40.22977, -3.95338))));
        }
    
        @Override
        public void onCameraChange(final CameraPosition cameraPosition) {
            List visiblePoints = new ArrayList<>();
            Projection projection = mMap.getProjection();
    
            for (Marker visibleMarker : visibleMarkers) {
                visiblePoints.add(projection.toScreenLocation(visibleMarker.getPosition()));
            }
    
            float radius = 150f; // meters
            Point centerPoint = projection.toScreenLocation(cameraPosition.target);
            Point radiusPoint = projection.toScreenLocation(
                    SphericalUtil.computeOffset(cameraPosition.target, radius, 90));
    
            float radiusPx = (float) Math.sqrt(Math.pow(centerPoint.x - radiusPoint.x, 2));
    
            hideView.reDraw(visiblePoints, radiusPx);
        }
    }
    

    HideOverlayView:

    public class HideOverlayView extends LinearLayout {
        private Bitmap windowFrame;
        private float radius = 0f;
        private List points;
    
        public HideOverlayView(Context context) {
            super(context);
        }
    
        public HideOverlayView(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        @Override
        protected void dispatchDraw(Canvas canvas) {
            super.dispatchDraw(canvas);
    
            createWindowFrame();
            canvas.drawBitmap(windowFrame, 0, 0, null);
        }
    
        @Override
        public boolean isEnabled() {
            return false;
        }
    
        @Override
        public boolean isClickable() {
            return false;
        }
    
        public void reDraw(List points, float radius) {
            this.points = points;
            this.radius = radius;
    
            invalidate();
        }
    
        protected void createWindowFrame() {
            windowFrame = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
            Canvas osCanvas = new Canvas(windowFrame);
    
            RectF outerRectangle = new RectF(0, 0, getWidth(), getHeight());
            Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
            paint.setColor(Color.BLACK);
            osCanvas.drawRect(outerRectangle, paint);
    
            if (radius > 0 && points != null) {
                for (Point point : points) {
                    paint.setColor(Color.TRANSPARENT);
                    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OUT));
                    paint.setStyle(Paint.Style.FILL);
                    osCanvas.drawCircle(point.x, point.y, radius, paint);
                }
            }
        }
    
        @Override
        public boolean isInEditMode() {
            return true;
        }
    
        @Override
        protected void onLayout(boolean changed, int l, int t, int r, int b) {
            super.onLayout(changed, l, t, r, b);
            windowFrame = null;
        }
    }
    

    activity_maps.xml:

    
    
        
        
    
    

    Result:

    Limitations:

    • This solution only updates the HideOverlayView on onCameraChange, so when the user zooms or pans the map, other locations on the map can be shown
    • The calculations made to compute the radius don't take into account rotations and tilts of the map

    0 讨论(0)
提交回复
热议问题