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 6073291
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:15:19+00:00 2026-05-23T10:15:19+00:00

I am trying to display a polygon on a mapview in android. I have

  • 0

I am trying to display a polygon on a mapview in android. I have created a custom overlay class(polygon) and overridden the draw method. After adding an instance of polygon to the mapview’s overlay list a polygon “should” be displayed. But when the map is displayed, there is no overlay to be found. What am I missing? Here is the main map activity where the polygon is created:

public class GPSLocator extends MapActivity {
MapView mapView;
Polygon polygon;

@Override
protected boolean isRouteDisplayed() {
    return false;
}

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    mapView = (MapView) findViewById(R.id.mapview);
    mapView.setBuiltInZoomControls(true);

    ArrayList<GeoPoint> points = new ArrayList<GeoPoint>();
    points.add(new GeoPoint((int)(-86.63283601665323*1e6), (int)(34.6857467079488*1e6)));
    points.add(new GeoPoint((int)(-86.63172145427183*1e6), (int)(34.68572865382659*1e6))); 
    points.add(new GeoPoint((int)(-86.63172141228351*1e6), (int)(34.68613493108094*1e6))); 
    points.add(new GeoPoint((int)(-86.63288804303616*1e6), (int)(34.68611093719812*1e6)));     
    polygon = new Polygon(points);

    mapView.getOverlays().clear();
    mapView.getOverlays().add(polygon);
    mapView.invalidate();
}

And here is the custom overlay Polygon:

public class Polygon extends Overlay {

ArrayList<GeoPoint> geoPoints;

public Polygon(ArrayList<GeoPoint> points)
{
    geoPoints = points;
}

@Override
public void draw(Canvas canvas, MapView mapView, boolean shadow)
{
    //Set the color and style
    Paint paint = new Paint();
    paint.setColor(Color.RED);
        paint.setStyle(Paint.Style.FILL_AND_STROKE);

    //Create path and add points
    Path path = new Path();
    Point firstPoint = new Point();
    mapView.getProjection().toPixels(geoPoints.get(0), firstPoint);
    path.moveTo(firstPoint.x, firstPoint.y);

    for(int i = 1; i < geoPoints.size(); ++i)
    {
        Point nextPoint = new Point();
        mapView.getProjection().toPixels(geoPoints.get(i), nextPoint);
        path.lineTo(nextPoint.x, nextPoint.y);
    }

    //Close polygon
    path.lineTo(firstPoint.x, firstPoint.y);
    path.setLastPoint(firstPoint.x, firstPoint.y);
    canvas.drawPath(path, paint);
    super.draw(canvas, mapView, shadow);

}
}
  • 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-05-23T10:15:19+00:00Added an answer on May 23, 2026 at 10:15 am

    You have to use path.moveTo(pixelPoint.x, pixelPoint.y); in your drawing loop. Otherwise you will draw a star not a polygon. Here is my (working) code for similar task:

    public void draw(Canvas canvas, MapView mapv, boolean shadow) {
            super.draw(canvas, mapv, shadow);
            Log.i("BikeComputer", "MyOverlay draw");
            mPaint = new Paint();
            mPaint.setDither(true);
            mPaint.setColor(Color.RED);
            mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
            mPaint.setStrokeJoin(Paint.Join.ROUND);
            mPaint.setStrokeCap(Paint.Cap.ROUND);
            mPaint.setStrokeWidth(5);
    
            Point p1 = new Point();
            path = new Path();
    
            GeoPoint point = geoPoints.get(0);
            mapv.getProjection().toPixels(point, p1);
            path.moveTo(p1.x, p1.y);
    
            Point pixelPoint = new Point();
            Iterator<GeoPoint> i = geoPoints.iterator();
            while (i.hasNext()) {
    
                GeoPoint trackPoint = i.next();
                mapv.getProjection().toPixels(trackPoint, pixelPoint);
                path.lineTo(pixelPoint.x, pixelPoint.y);
                path.moveTo(pixelPoint.x, pixelPoint.y);
            }
            // draw track
            canvas.drawPath(path, mPaint);
    
            // start point
            mapv.getProjection().toPixels(geoPoints.get(0), pixelPoint);
            canvas.drawBitmap(startFlag, pixelPoint.x, pixelPoint.y - 32, null);
    
            // finish point
    
            mapv.getProjection().toPixels(geoPoints.get(geoPoints.size() - 1),
                    pixelPoint);
            canvas.drawBitmap(finishFlag, pixelPoint.x, pixelPoint.y - 32, null);
        }
    
    }
    

    and how is this this class instaniazed:

            mapView = (MapView) findViewById(R.id.mapview);
        mapView.setBuiltInZoomControls(true);
    
        mapOverlays = mapView.getOverlays();
        mapView.getProjection();
        mapOverlays.add(new MyOverlay());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When trying to display a byte stream from HLDS (Half-Life Dedicated Server) in a
I've been trying to display text using a Quartz context, but no matter what
I'm trying to display a series of titles varying from 60 characters to 160
I am trying to display a list of all files found in the selected
I'm trying to display a loading icon while my iPhone app downloads a network
I'm trying to display an array of files in order of date (last modified).
I'm trying to display a caret ( ^ ) in math mode in LaTeX
I am trying to display a pie chart that shows sales by company. However
I'm trying to display a picture in an openGL environment. The picture's origninal dimensions
I'm trying to display an address right aligned in the footer of my page

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.