2017-06-21 48 views
0

下面的代碼在第三次點擊地圖後創建一個多邊形。我需要展開(即添加新的頂點),在現有的多邊形之後的第四個,第五個等點擊。在Android中擴展Google Map Polygon

public void onMapClick(LatLng point) { 
     mTapTextView.setText("tapped, point=" + point); 
     if (i==0){ 
      i=1; 
      coordinates.add(point); 
      googleMap.addMarker(new MarkerOptions() 
        .position(point) 
        .title(String.valueOf(point.latitude)) 
        .snippet(String.valueOf(point.latitude)) 
        .rotation((float) -15.0) 
        .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ORANGE)) 
      ); 
     } else if (i==1) { 
      i=i+1; 
      coordinates.add(point); 
      googleMap.addPolyline((new PolylineOptions()) 
        .add(coordinates.get(0), coordinates.get(1))); 
     }else if (i==2){ 
      coordinates.add(point); 
      polygon = googleMap.addPolygon((new PolygonOptions()) 
        .add(coordinates.get(0), coordinates.get(1),coordinates.get(2))); 
      i=i+1; 
     }else{ 
      // How do I expand the existing polygon 
     } 
    } 

回答

0

我只是將點移動到一個數組後刪除了現有的,然後用網絡點擊重新創建一個多邊形。

public int i=0; 
    public Polygon polygon; 
    public Polyline polyline; 

    List<LatLng> coordinates=new ArrayList<LatLng>(); 
    public void onMapClick(LatLng point) { 
     mTapTextView.setText("tapped, point=" + point); 
     if (i==0){ 
      i=1; 
      coordinates.add(point); 
      googleMap.addMarker(new MarkerOptions() 
        .position(point) 
        .title(String.valueOf(point.latitude)) 
        .snippet(String.valueOf(point.latitude)) 
        .rotation((float) -15.0) 
        .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ORANGE)) 
      ); 
     } else if (i==1) { 
      i=i+1; 
      coordinates.add(point); 
      polyline = googleMap.addPolyline((new PolylineOptions()) 
        .add(coordinates.get(0), coordinates.get(1))); 
     }else if (i>1){ 
      coordinates.add(point); 
      polyline.remove(); 
      if (i>2){polygon.remove();}; 
      polygon = googleMap.addPolygon(new PolygonOptions() 
       .addAll(coordinates) 
      .strokeColor(Color.BLACK) 
      .strokeWidth(10)); 
      //polygon = googleMap.addPolygon((new PolygonOptions()) 
       //  .add(coordinates.get(0), coordinates.get(1),coordinates.get(2))); 
      i=i+1; 
     }/*else{ 
      List<LatLng> polygonList = polygon.getPoints(); 

      Toast.makeText(getBaseContext(), polygonList.toString(), Toast.LENGTH_LONG).show(); 


      polygonList.add(point); 
      polygon.remove(); 
      polygon = googleMap.addPolygon((new PolygonOptions())); 
      polygon.setPoints(polygonList); 
      i=i+1; 
     }*/ 
    }