title: Google Maps Android point in polygon
body: <p>I creating a android app that uses google maps android api.<br>
  This app need to to have a on click polygon function, but since it not available on google maps android api v2. I did some research and found a work around for this function.<br>
  What i ended up with is to check if a point is in a polygon method.<br>
  I'm using this <a href="https://github.com/sromku/polygon-contains-point" rel="nofollow">library</a>. And it work pretty well if you create the bounds manually.<br>
  If can create the bounds in a loop that would solve my problem.<br>
  I get the polygon points from my database.</p>
  
  <p>Here is my MapFragment code:</p>
  
  <pre><code>public class MapBlocksMapView extends Fragment {
  
      protected GoogleMap googleMap;
      protected LatLng latLng;
      protected Intent intent;
      protected String color, crops, block_code, tmp_user;
  
      public MapBlocksMapView() {
      }
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
                               Bundle savedInstanceState) {
          ActionBar actionBar = getActivity().getActionBar();
          actionBar.setTitle("Block View");
          View rootView = inflater.inflate(R.layout.fragment_blocksmapview, container, false);
  
          if (googleMap== null) {
              googleMap= ((SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map)).getMap();
          }
  
          Bundle bundle = getArguments();
          if (bundle != null) {
              block_code = bundle.getString("block_code");
              tmp_user = bundle.getString("user");
          }
  
          googleMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
              @Override
              public void onMapClick(LatLng latLng) {
                  checkpoint(latLng.latitude, latLng.longitude);
              }
          });
          return rootView;
  
      }
  
      @Override
      public void onActivityCreated(Bundle savedInstanceState){
          getdata(getActivity(), block_code);
          super.onActivityCreated(savedInstanceState);
      }
  
      private void checkpoint(double latitude, double longitude) {
  
          System.out.println(latitude +"," +longitude);
      }
  
      public void getdata(Context ctx, String block_code) {
  
          SQLHelper dbhelper = new SQLHelper(ctx);
          dbhelper.getReadableDatabase();
  
          JSONArray jsonArray = dbhelper.getSingleBlocks(block_code);
  
          try {
              for (int a = 0; a &lt; jsonArray.length(); a++) {
  
                  JSONObject jsonObject = new JSONObject(String.valueOf(jsonArray.getJSONObject(a)));
                  JSONArray jsonArray1 = jsonObject.getJSONArray("b_points");
                  if (jsonObject.getJSONArray("b_points").length() &gt; 0) {
                      color = jsonObject.getString("b_color");
                      crops = jsonObject.getString("b_crop");
                      PolygonOptions rectOptions = new PolygonOptions();
  
                      for (int b = 0; b &lt; jsonArray1.length(); b++) {
                          JSONArray jsonArray2 = jsonArray1.getJSONArray(b);
  
                          rectOptions.add(new LatLng(jsonArray2.getDouble(0), jsonArray2.getDouble(1)));
  
  
                          System.out.println(jsonArray2.get(0) + " / " + jsonArray2.get(1));
  
                      }
                      latLng = new LatLng(jsonArray1.getJSONArray(0).getDouble(0), jsonArray1.getJSONArray(0).getDouble(1));
                      rectOptions.strokeWidth(1).strokeColor(Color.parseColor(color)).fillColor(Color.parseColor(color));
                      googleMap.addPolygon(rectOptions);
                      CameraUpdate cameraPosition = CameraUpdateFactory.newLatLngZoom(latLng, 17);
                      googleMap.animateCamera(cameraPosition);
  
                  } else {
  
                      Toast.makeText(getActivity(), "Error with the selected block", Toast.LENGTH_LONG).show();
                      closeFragment();
                  }
              }
          } catch (JSONException e) {
              e.printStackTrace();
          }
  
      }
  
      @Override
      public void onDestroyView() {
          super.onDestroyView();
          SupportMapFragment f = (SupportMapFragment) getFragmentManager()
                  .findFragmentById(R.id.map);
          if (f != null)
              getFragmentManager().beginTransaction().remove(f).commit();
      }
  
      private void closeFragment() {
          getActivity().getFragmentManager().popBackStack();
      }
  
  }
  </code></pre>
  
  <p>Here is the example code from the library:</p>
  
  <pre><code>Polygon polygon = Polygon.Builder()
      .addVertex(new Point(1, 3))
      .addVertex(new Point(2, 8))
      .addVertex(new Point(5, 4))
      .addVertex(new Point(5, 9))
      .addVertex(new Point(7, 5))
      .addVertex(new Point(6, 1))
      .addVertex(new Point(3, 1))
      .build();
  
  Point point = new Point(4.5f, 7);
  boolean contains = polygon.contains(point);
  </code></pre>
  
  <p>If some have a other solution or suggestions would be helpful.<br>
  Thanks </p>