How to draw a shape on the map fragment by touching it using google map V2

前端 未结 2 1888
甜味超标
甜味超标 2021-02-06 15:52

Hello everyone,

I am using Google map V2 and I have to draw a shape on the map fragment by touching it. i.e if I rotate my fingers on the map a shape should be generat

相关标签:
2条回答
  • 2021-02-06 16:12

    I have done it using the map's OnMapClickListener:

    private boolean drawing = false;
    private Polygon polygon;
    private List<LatLng> points = new ArrayList<>();
    
    public class AreasFragment extends mapFragment implements GoogleMap.OnMapClickListener {
    
      @Override
      public void onMapClick(LatLng point) {
        points.add(point);
        if(!drawing) {
            map.addMarker(new MarkerOptions()
                    .icon(BitmapDescriptorFactory.fromResource(R.drawable.flag))
                    .anchor(0.2f, 1.0f)
                    .position(point));
    
            PolygonOptions rectOptions = new PolygonOptions()
                    .strokeWidth(2)
                    .fillColor(0x80000000)
                    .add(point);
            polygon = map.addPolygon(rectOptions);
            drawing = true;
        }
        else {
            polygon.setPoints(points);
        }
      }
    }
    
    0 讨论(0)
  • 2021-02-06 16:19

    GoogleMap doesn't have a touch listener, so you'll have to override onTouchEvent for the parent view of your MapFragment. Once you have the screen coordinates of your touch event, you can get the Lat/Long by using Projection (Doc here). Simply do

    LatLng coords = mapFragment.getMap().getProjection().fromScreenLocation(point);

    Where point is a Point describing the location of your touch event.

    Once you have the LatLng describing your touch event, you can draw shapes using either Circle or Polygon. Google's tutorial on drawing shapes will do a better job of explaining it than I could: Shapes - Google Maps v2

    Hope this helps!

    0 讨论(0)
提交回复
热议问题