Identify if point is in the polygon

As per my requirement, I am drawing polygons on google map shown in the image below.(using maps v2) enter image description here

Now I need to show an alert when user enters that particular polygons.

  • EditText looses focus when keyboard appears; requires touching twice to edit
  • AsyncTask onPostExecute() not called in Unit Test Case
  • What are the possible intent types for intent.setType(type)?
  • Android - Expected Resource of type ID
  • Debugging native code in android under eclipse fails
  • Is it possible to scale drawableleft & drawableright in textview?
  • How to identify if my current location is with in the polygon.
    (Need optimized way without draining battery)

    Thanks in advance.

    Related posts:

    How can usb debugging for Kindle Fire can be enabled on a Linux (Ubuntu)?
    How to force a view to redraw immediately before the next line of code is executed
    How to make read only (programmatically filled) Preferences?
    How do you format date and time in Android?
    How to create datePicker and timePicker dialogs in fragment class?
    How to insert drawables in text
  • Font in Android Library
  • How to properly initialize and terminate EGL on Android
  • How to paint a gradient color in Android on a Canvas with the color changing?
  • Is there a way to reduce the spacing between the Action Item Icons on Action Bar?
  • How to put my libraries in front of android.jar by editing build.gradle in Android-Studio
  • Separating integration tests from unit tests in Android Studio
  • 3 Solutions collect form web for “Identify if point is in the polygon”

    Just tried Ray Casting algorithm which identifies point in polygon. This works perfect.

    Refer for thesis of Ray-Casting

    private boolean isPointInPolygon(LatLng tap, ArrayList<LatLng> vertices) {
            int intersectCount = 0;
            for (int j = 0; j < vertices.size() - 1; j++) {
                if (rayCastIntersect(tap, vertices.get(j), vertices.get(j + 1))) {
            return ((intersectCount % 2) == 1); // odd = inside, even = outside;
        private boolean rayCastIntersect(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; // a and b can't both be above or below pt.y, and a or
                                // b must be east of pt.x
            double m = (aY - bY) / (aX - bX); // Rise over run
            double bee = (-aX) * m + aY; // y = mx + b
            double x = (pY - bee) / m; // algebra is neat!
            return x > pX;

    I found ray-casting method unreliable but I ended up using the PolyUtil from google maps.

    You need the dependency compile ''

    And then the method looks like this

    PolyUtil.containsLocation(userLocation, polyPointsList, false);

    Refer this link

    Polygon Touch detection Google Map API V2

    Its RayCasting algorithm, it may help you 🙂

    A brief description about the algorithm:

    A horizontal line is drawn from your point to the right, if it intersects the sides of polygon at odd number of times then the point is inside the polygon else outside 🙂

    These wiki links will give you complete idea:

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.