Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8658641
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T15:49:06+00:00 2026-06-12T15:49:06+00:00

My plan is to allow user to draw a rectangle onto a map in

  • 0

My plan is to allow user to draw a rectangle onto a map in order to mark an area and later save it. I found a method to do it, but it does nothing. Here is how the code looks so far.

public class Map extends MapActivity{

private MapView mvMap;
MapController kontrol;
float xs, ys, xe, ye;
GeoPoint start, end;
private Paint paint = new Paint();
private boolean up = false;

@Override
protected void onCreate(Bundle arg0) {
    // TODO Auto-generated method stub
    super.onCreate(arg0);
    setContentView(R.layout.map);
    mvMap = (MapView) findViewById(R.id.mvMap);
    mvMap.setBuiltInZoomControls(true);
    paint.setStrokeWidth(2.0f);
//  
//  DrawOverlay t = new DrawOverlay();
//  List<Overlay> olList = mvMap.getOverlays();
//  olList.add(t);
    mvMap.getOverlays().add(new EmptyOverlay());
    mvMap.postInvalidate();
    kontrol = mvMap.getController();
    GeoPoint ja = new GeoPoint(52172722, 21071987);
    kontrol.animateTo(ja);
    kontrol.setZoom(11);
}

@Override
protected boolean isRouteDisplayed() {
    // TODO Auto-generated method stub
    return false;
}

class DrawOverlay extends Overlay{
    public boolean onTouchEvent(MotionEvent e, MapView m){
        if(up = false){
            if(e.getAction() == MotionEvent.ACTION_DOWN){
                xs = ys = 0;
                xs = e.getX();
                ys = e.getY();
                start = mvMap.getProjection().fromPixels((int)xs,(int)ys);
                //draw(null, m, up);
            }
            if(e.getAction() == MotionEvent.ACTION_MOVE){
                xe = e.getX();
                ye = e.getY();
                end = mvMap.getProjection().fromPixels((int)xe,(int)ye);

            }
            if(e.getAction() == MotionEvent.ACTION_UP){
                up = true;
            }
        }
        return true;
    }

    @Override
    public boolean draw(Canvas canvas, MapView mapView, boolean shadow,
            long when) {

        if(start != null && end != null){
            //get the 2 geopoints defining the area and transform them to pixels
            //this way if we move or zoom the map rectangle will follow accordingly
            Point screenPts1 = new Point();
            mapView.getProjection().toPixels(start, screenPts1);
            Point screenPts2 = new Point();
            mapView.getProjection().toPixels(end, screenPts2);

            //draw inner rectangle
            paint.setColor(0x4435EF56);
            paint.setStyle(Style.FILL);
            canvas.drawRect(screenPts1.x, screenPts1.y, screenPts2.x, screenPts2.y, paint);
            //draw outline rectangle
            paint.setColor(0x88158923);
            paint.setStyle(Style.STROKE);
            canvas.drawRect(screenPts1.x, screenPts1.y, screenPts2.x, screenPts2.y, paint);
        }
        return true;
    }

}

public class EmptyOverlay extends Overlay {
    private float x1,y1;
    private Overlay overlay = null;

    public EmptyOverlay(){

    }

    @Override
    public boolean draw(Canvas canvas, MapView mapView, boolean shadow,
            long when) {
        // TODO Auto-generated method stub
        return super.draw(canvas, mapView, shadow, when);
    }

    @Override
    public boolean onTouchEvent(MotionEvent e, MapView mapView) {
    //  if(mv.isEditMode()){
            if(e.getAction() == MotionEvent.ACTION_DOWN){
                //when user presses the map add a new overlay to the map
                //move events will be catched by newly created overlay
                x1 = y1 = 0;
                x1 = e.getX();
                y1 = e.getY();

                overlay = new DrawOverlay();
                mapView.getOverlays().add(overlay);

            }
            if(e.getAction() == MotionEvent.ACTION_MOVE){
            }
            //---when user lifts his finger---
            if (e.getAction() == MotionEvent.ACTION_UP) {                

            }    
           // return true;
//      }
        return false;
    }
}

I will be grateful for any help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T15:49:07+00:00Added an answer on June 12, 2026 at 3:49 pm

    Use this code it will work for Your on drawing a rectangel with inside text over any mark location., this code will draw many rectangles for many friends which we have passed through overlay item,

          public void draw(android.graphics.Canvas canvas, MapView mapView,
                      boolean shadow)
    {
        super.draw(canvas, mapView, shadow);
    
        // go through all OverlayItems and draw title for each of them
        for (OverlayItem item:mOverlays)
        {
    
            GeoPoint point = item.getPoint();
            Point markerBottomCenterCoords = new Point();
            mapView.getProjection().toPixels(point, markerBottomCenterCoords);
    
            /* Find the width and height of the title*/
            TextPaint paintText = new TextPaint();
            Paint paintRect = new Paint();
    
            Rect rect = new Rect(markerBottomCenterCoords.x,markerBottomCenterCoords.y,33,44);
            paintText.setTextSize(FONT_SIZE);
            paintText.getTextBounds(item.getTitle(), 0, item.getTitle().length(), rect);
           // rect.height()=34;
            rect.inset(-TITLE_MARGIN, -TITLE_MARGIN);
            int markerHeight=34;// this will place to a height of 34 athe top of the mark location
            rect.offsetTo(markerBottomCenterCoords.x - rect.width()/2, markerBottomCenterCoords.y - markerHeight - rect.height());
    
            paintText.setTextAlign(Paint.Align.CENTER);
            paintText.setTextSize(FONT_SIZE);
            paintText.setARGB(255, 255, 255, 255);
            paintRect.setARGB(255,148,0,211);//s(255, 178, 34, 34);
            paintRect.setTextAlign(Align.CENTER);
            canvas.drawRoundRect( new RectF(rect), 0, 29, paintRect);
           canvas.drawText(item.getTitle(), rect.left + rect.width() / 2,
                    rect.bottom - TITLE_MARGIN, paintText);
    
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code: http://www.project-vanquish.co.cc/gridtest/drag-grid.html The plan is to allow the user to
I user godaddy hosting and they allow database use upto 200 mb. I plan
I'm trying to allow user to rename file name, after taking picture, but I
I need to create an input form that will allow a user to enter
Part of a real estate application I'm writing will allow a user to subscribe
Perfecto Mobile and Keynote DeviceAnywhere are both seemingly valuable tools. They allow the user
I plan to use non-renewing subscription on SaaS app since apple doesn't allow auto-renewable
I have a report that I currently allow the user to choose an output
I plan on using multiple popups on my website. Every popup will be rendered
I plan to host a site in a shared hosting environment, where multiple developers

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.