Google Maps multiple polygon which starting the second polygon from the first polygon location

I am new to the Android development. I am trying to do the multiple polygons. I did it but the second polygon is starting from the end of the first polygon. I have listed the questions I have followed and the image that shows my result.

Multiple Polygon On Map

How to draw free hand polygon in Google map V2 in Android?

My result

Here is my code:

public class MainActivity extends AppCompatActivity implements OnMapReadyCallback {

    private GoogleMap mMap;
    private static final LatLng MainLocation = new LatLng(40.828070, -111.904610);
    FrameLayout fram_map;
    FloatingActionButton fab;
    Boolean Is_MAP_Moveable = false;
    Projection projection;
    public double latitude;
    public double longitude;
    PolygonOptions rectOptions;
    ArrayList < LatLng > val = new ArrayList < > ();
    ArrayList < ArrayList < LatLng >> val2 = new ArrayList < > ();
    Polygon polygon;
    String LogName = "XEL";

    @SuppressLint("ClickableViewAccessibility")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
        fram_map = (FrameLayout) findViewById(R.id.fram_map);
        fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Is_MAP_Moveable) {
                    Is_MAP_Moveable = false;
                    if (val2.isEmpty()) {
                        val2.add(0, val);
                    } else {
                        val2.add(val2.size(), val);
                    }

                } else {
                    Is_MAP_Moveable = true;
                }
            }
        });
        fram_map.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (Is_MAP_Moveable) {
                    float x = event.getX();
                    float y = event.getY();

                    int x_co = Math.round(x);
                    int y_co = Math.round(y);

                    projection = mMap.getProjection();
                    Point x_y_points = new Point(x_co, y_co);

                    LatLng latLng = mMap.getProjection().fromScreenLocation(x_y_points);
                    latitude = latLng.latitude;

                    longitude = latLng.longitude;

                    int eventaction = event.getAction();
                    switch (eventaction) {
                        case MotionEvent.ACTION_DOWN:
                            val.add(new LatLng(latitude, longitude));
                        case MotionEvent.ACTION_MOVE:
                            val.add(new LatLng(latitude, longitude));
                        case MotionEvent.ACTION_UP:
                            Draw_Map();
                            break;
                    }

                } else {
                    Toast.makeText(MainActivity.this, "Please start the zone", Toast.LENGTH_SHORT).show();
                }

                return Is_MAP_Moveable;

            }
        });
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;
        mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
        googleMap.setOnPolygonClickListener(new GoogleMap.OnPolygonClickListener() {
            @Override
            public void onPolygonClick(Polygon polygon) {
                Toast.makeText(MainActivity.this, polygon.getPoints().toString(), Toast.LENGTH_SHORT).show();
            }
        });
        googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(MainLocation,
            20));
    }

    public void Draw_Map() {
        /*  for (int i = 0; i < val2.size(); i++) {
              rectOptions = new PolygonOptions();
              rectOptions.addAll(val2.get(i));
              rectOptions.strokeColor(Color.BLUE);
              rectOptions.strokeWidth(7);
              rectOptions.fillColor(Color.CYAN);
              polygon = mMap.addPolygon(rectOptions);
              polygon.setClickable(true);
          }*/
        rectOptions = new PolygonOptions();
        rectOptions.addAll(val);
        rectOptions.strokeColor(Color.BLUE);
        rectOptions.strokeWidth(7);
        rectOptions.fillColor(Color.CYAN);
        polygon = mMap.addPolygon(rectOptions);
        polygon.setClickable(true);
    }
}

1 answer

  • answered 2018-11-08 08:08 Subrata Mondal

    From an overview, I can suggest few things. If you implement these things the issue might be resolved:

    1. Do now user a separate onClick. Use the onTouch.

      //declare globally
      float init_x, init_y;
      float tolerance = 5;
      
      fram_map.setOnTouchListener(new View.OnTouchListener() {
              @Override
              public boolean onTouch(View v, MotionEvent event) {
                  switch (event.getAction()) {
                      case MotionEvent.ACTION_DOWN:
                          init_x = event.getX();
                          init_y = event.getY();
                          break;
                      case MotionEvent.ACTION_MOVE:
      
                          break;
                      case MotionEvent.ACTION_UP:
                          float x = event.getX();
                          float y = event.getY();
                          Is_MAP_Moveable = Math.abs(init_x - x) > tolerance || Math.abs(init_y - y) > tolerance;
                          if(!Is_MAP_Moveable) {
                              int x_co = Math.round(x);
                              int y_co = Math.round(y);
      
                              projection = mMap.getProjection();
                              Point x_y_points = new Point(x_co, y_co);
                              LatLng latLng = mMap.getProjection().fromScreenLocation(x_y_points);
                              latitude = latLng.latitude;
                              longitude = latLng.longitude;
                              val.add(new LatLng(latitude, longitude));
                              Draw_Map();
                          } else {
                              Toast.makeText(MainActivity.this, "Please start the zone", Toast.LENGTH_SHORT).show();
                          }
                          break;
                  }
                  return Is_MAP_Moveable;
      
              }
          });
      
      1. Do not need to use ArrayList of ArrayLists of LatLng - val2. Use val to draw the polygon.

      2. Clear all polygons on the map before swathing any new one because you are pushing elements in an ArrayList. Use polygon.remove(); before adding new Polygon.