1

I am developing an android application. In the application I have integrated map with polygon. Now I want to check whether the given marker is inside the polygon or not. Help Me please.

Following function is used to check the lat long in the polygon

pnpoly(no_of_vertex, lat, lng,(float) (point.latitude),(float) (point.longitude))

boolean pnpoly(int nvert, float vertx[], float verty[], float testx, float testy)
    {
      int i, j;
      boolean c = false;
      for (i = 0, j = nvert-1; i < nvert; j = i++) {
        if ( ((verty[i]>testy) != (verty[j]>testy)) &&
         (testx < (vertx[j]-vertx[i]) * (testy-verty[i]) / (verty[j]-verty[i]) + vertx[i]) )
           c =true;
      }
      return c;
    }

I got the solution to my question following is the code

**private boolean isPointInPolygon(LatLng tap, ArrayList<LatLng> vertices) {
        int intersectCount = 0;
        for(int j=0; j<vertices.size()-1; j++) {
            if( LineIntersect(tap, vertices.get(j), vertices.get(j+1)) ) {
                intersectCount++;
            }
        }
        return (intersectCount%2) == 1; // odd = inside, even = outside;
    }
    private boolean LineIntersect(LatLng tap, LatLng vertA, LatLng vertB) {
        double aY = vertA.latitude;
        double bY = vertB.latitude;
        double aX = vertA.longitude;
        double bX = vertB.longitude;
        double pY = tap.latitude;
        double pX = tap.longitude;
        if ( (aY>pY && bY>pY) || (aY<pY && bY<pY) || (aX<pX && bX<pX) ) {
            return false; }
        double m = (aY-bY) / (aX-bX);               
        double bee = (-aX) * m + aY;                // y = mx + b
        double x = (pY - bee) / m;                 
        return x > pX;
    }**
  • Please provide more details, as well as what you have tried. Also, there are many similar questions with answers on StackOverflow. Have you reviewed them for solutions to your problem? – lreeder Mar 13 '14 at 13:20
  • yes i have checked all the solutions. thanks for the reply i am putting my code as well – user3415537 Mar 13 '14 at 13:22
  • possible duplicate of [Point in Polygon aka hit test](http://stackoverflow.com/questions/217578/point-in-polygon-aka-hit-test) – tyczj Mar 13 '14 at 13:28
  • 2
    or you can check out this library that does it for you https://github.com/googlemaps/android-maps-utils – tyczj Mar 13 '14 at 13:31
  • Thanks to all................. But I got the solution and its working perfectly........... – user3415537 Mar 14 '14 at 05:14
  • Thanks man, it saved me.. your code worked perfectly. – Harsh Nov 07 '14 at 13:17
  • when we are giving the arraylist, we have to provide the starting latlng also as the last latlng? – DJack Apr 08 '16 at 19:00

0 Answers0