Creating Custom Overlay on the map

前端 未结 3 1970
北荒
北荒 2020-11-29 20:25

I am trying to replicate this feature of Maps in Android: \"Custom

You can see that on the map, t

相关标签:
3条回答
  • 2020-11-29 21:12

    Okay, I tried to do things on my Own, and put this code to get the above effect:

    public class MarkerOverlay extends Overlay {
    
        Geocoder geoCoder = null;
    
        public MarkerOverlay() {
            super();
        }
    
    
        @Override
        public boolean onTap(GeoPoint geoPoint, MapView mapView){
            selectedLatitude = geoPoint.getLatitudeE6(); 
            selectedLongitude = geoPoint.getLongitudeE6();
            return super.onTap(geoPoint,mapView);
        }
    
        @Override
        public void draw(Canvas canvas, MapView mapV, boolean shadow){
    
            if(shadow){
                Projection projection = mapV.getProjection();
                Point pt = new Point();
                projection.toPixels(globalGeoPoint,pt);
    
                GeoPoint newGeos = new GeoPoint(selectedLat+(100),selectedLong); // adjust your radius accordingly
                Point pt2 = new Point();
                projection.toPixels(newGeos,pt2);
                float circleRadius = Math.abs(pt2.y-pt.y);
    
                Paint circlePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    
                circlePaint.setColor(0x30000000);
                circlePaint.setStyle(Style.FILL_AND_STROKE);
                canvas.drawCircle((float)pt.x, (float)pt.y, circleRadius, circlePaint);
    
                circlePaint.setColor(0x99000000);
                circlePaint.setStyle(Style.STROKE);
                canvas.drawCircle((float)pt.x, (float)pt.y, circleRadius, circlePaint);
    
                Bitmap markerBitmap = BitmapFactory.decodeResource(getApplicationContext().getResources(),R.drawable.pin);
                canvas.drawBitmap(markerBitmap,pt.x,pt.y-markerBitmap.getHeight(),null);
    
                super.draw(canvas,mapV,shadow);
            }
        }
    }
    

    This let me have following effect:

    Effect on Map

    The calculation used may not be what you want.
    Its just for demonstration purposes.
    Real range/distance calculation requires the use of bearing too and has some specific formula.
    Let me know if you have any questions regarding this.

    0 讨论(0)
  • 2020-11-29 21:12

    Extend the class ItemizedOverlay to override the draw() method. The Canvas where overlays are drawn is passed to that method and you can call drawCircle or anything that's needed to make your range dragger appear.

    0 讨论(0)
  • 2020-11-29 21:23

    An example code to draw a pushpin with the circle:

    public class MapDemoActivity extends MapActivity {
        MapView mapView;
        MapController mc;
        GeoPoint p;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    
        mapView = (MapView) findViewById(R.id.mapView);
        LinearLayout zoomLayout = (LinearLayout)findViewById(R.id.zoom);  
        View zoomView = mapView.getZoomControls(); 
    
        zoomLayout.addView(zoomView, 
            new LinearLayout.LayoutParams(
                LayoutParams.WRAP_CONTENT, 
                LayoutParams.WRAP_CONTENT)); 
        mapView.displayZoomControls(true);
    
        mc = mapView.getController();        
        String coordinates[] = {"28.38", "77.12"};
        double lat = Double.parseDouble(coordinates[0]);
        double lng = Double.parseDouble(coordinates[1]);
    
        p = new GeoPoint(
            (int) (lat * 1E6), 
            (int) (lng * 1E6));
    
        mc.animateTo(p);
        mc.setZoom(8); 
        //---Add a location marker---
        MapOverlay mapOverlay = new MapOverlay();
        List<Overlay> listOfOverlays = mapView.getOverlays();
        listOfOverlays.clear();
        listOfOverlays.add(mapOverlay);        
        mapView.invalidate();
        }
    
        class MapOverlay extends com.google.android.maps.Overlay
    {
        @Override
        public boolean draw(Canvas canvas, MapView mapView, 
        boolean shadow, long when) 
        {
            super.draw(canvas, mapView, shadow);                   
    
            //---translate the GeoPoint to screen pixels---
            Point screenPts = new Point();
            mapView.getProjection().toPixels(p, screenPts);
            //--------------draw circle----------------------            
    
            Point pt=mapView.getProjection().toPixels(p,screenPts);
    
            Paint circlePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
            circlePaint.setColor(0x30000000);
            circlePaint.setStyle(Style.FILL_AND_STROKE);
            canvas.drawCircle(screenPts.x, screenPts.y, 50, circlePaint);           
    
            //---add the marker---
            Bitmap bmp = BitmapFactory.decodeResource(
                getResources(), R.drawable.pin);            
            canvas.drawBitmap(bmp, screenPts.x, screenPts.y-bmp.getHeight(), null);              
            super.draw(canvas,mapView,shadow);
    
            return true;
    
        }
    }
    @Override
    protected boolean isRouteDisplayed() {
        // TODO Auto-generated method stub
        return false;
    }
    }
    
    0 讨论(0)
提交回复
热议问题