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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T17:06:44+00:00 2026-06-05T17:06:44+00:00

I am trying to determine if a phone is located in this polygon using

  • 0

I am trying to determine if a phone is located in this polygon using longitude and latitude.

enter image description here

It worked on my first test point. My second test point (the green dot) returns that it is outside of the polygon. Since Android cannot use the Polygon class, I created my own using the code I found from a few different sites:

public class Polygon {

    private double[] polyY, polyX;
    private int polySides;

    public Polygon( double[] px, double[] py, int ps ) {
        polyX = px;
        polyY = py;
        polySides = ps;
    }
    public boolean contains( double x, double y ) {
        boolean oddTransitions = false;
        for( int i = 0, j = polySides -1; i < polySides; j = i++ ) {
            if( ( polyY[ i ] < y && polyY[ j ] >= y ) || ( polyY[ j ] < y && polyY[ i ] >= y ) ) {
                if( polyX[ i ] + ( y - polyY[ i ] ) / ( polyY[ j ] - polyY[ i ] ) * ( polyX[ j ] - polyX[ i ] ) < x ) {
                    oddTransitions = !oddTransitions;          
                }
            }
        }
        return oddTransitions;
    }
}

I query the database to get the longitudes and latitudes from this table:

db.execSQL("insert into " + DATABASE_TABLE_POLYGON  + " select null as " + KEY_ROWID
                + ", 0 as " + KEY_BUILDING
                + ", -95.929635 as " + KEY_LONG
                + ", 41.223459 as " + KEY_LAT  
                + " union select null,0,-95.931544, 41.223491"
                + " union select null,0,-95.931587, 41.224734"
                + " union select null,0,-95.929484, 41.224782"
                + " union select null,0,-95.929227, 41.227655"
                + " union select null,0,-95.929184, 41.229656"
                + " union select null,0,-95.926437, 41.23006"
                + " union select null,0,-95.92633, 41.228478"
                + " union select null,0,-95.924141, 41.227413"
                + " union select null,0,-95.920944, 41.22617"
                + " union select null,0,-95.921073, 41.225299"
                + " union select null,0,-95.922811, 41.224008"
                + " union select null,0,-95.92706, 41.22099"
                + " union select null,0,-95.927553, 41.221022"
                + " union select null,0,-95.927811, 41.222943"
                + " union select null,0,-95.929334, 41.222991";

Here is the code that is failing:

Polygon p = new Polygon(xArray,yArray,numPoints);
if(p.contains(-95.927714, 41.225281)){
    textView.setText("in polygon");
}else{
    textView.setText("outside polygon");
}

Output: outside polygon

Other info:
It seems to be working in another app I created.
I am using an emulator and mock locations.
I also created a normal java class so I could use the built in Polygon class, but that
also says that it is outside the polygon.
I used an online polygon grapher just to be totally sure the point was inside the polygon and it was.
The points go in a clockwise direction.

Why is this point returning “outside polygon”?

  • 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-05T17:06:46+00:00Added an answer on June 5, 2026 at 5:06 pm

    I figured out the problem. I finally noticed that all the points were sorted by longitude. So either the query (shown above) where I insert the data sorted it, or the query where I select longitude and latitude. The points must be in the correct order or the contains() method will not work. My solution will be to add another column to the table so I can number each point. Then whenever I query for the points I will sort by this new column so they are always in the correct order.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to determine how to count the matching rows on a table using
I'm trying to determine if there is a way to accomplish this elegantly. I
Trying to determine best way to code this requirement : Need to insert rows
I am trying to determine whether a ring is contained by another ring using
I'm trying to determine if my Kinect is plugged into the PC using the
So I am trying to determine if this is a bug or browser caching
I'm trying to develop a Windows Phone 7 application. Part of this application includes
I'm trying to determine, based on the result of this call, if it was
I'm trying to use something like a compass, passing it longitude/latitude values to let
I am trying to determine in which column the name Phone appears, by checking

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.