简体   繁体   English

Google Maps多个从第一个多边形位置开始的第二个多边形

[英]Google Maps multiple polygon which starting the second polygon from the first polygon location

I am new to the Android development. 我是Android开发的新手。 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? 如何在Android的Google Map V2中绘制自由手多边形?

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);
    }
}

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. 现在是否使用单独的onClick。 Use the onTouch. 使用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. 不需要使用LatLng-val2的ArrayList中的ArrayList。 Use val to draw the polygon. 使用val绘制多边形。

    2. Clear all polygons on the map before swathing any new one because you are pushing elements in an ArrayList. 在添加任何新多边形之前,请先清除地图上的所有多边形,因为您要推送ArrayList中的元素。 Use polygon.remove(); 使用polygon.remove(); before adding new Polygon. 在添加新的多边形之前。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM