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

Hello to all,

I use a Google V2 map, and I need to draw a shape on a fragment of the map by touching it. If I turn my fingers on the map, a shape should be formed. I have a problem because Google mapV2 does not provide lat, long when we touch the map. I don’t need to click on MapV2, so the click listener is not suitable for me.

Therefore, please share an idea or code to get the latitude and longitude from the map by touching it. To draw a shape by touching the map.

+4
android google-maps
May 23 '13 at 11:57
source share
2 answers

GoogleMap does not have a touch listener, so you have to redefine onTouchEvent to the parent view of your MapFragment . When you have the screen coordinates of your touch event, you can get Lat / Long using Projection (Doc here ). Just do

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

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

When you have LatLng describing your touch event, you can draw shapes using Circle or Polygon . The google drawing tutorial will better explain this than I could: Shapes - Google Maps v2

Hope this helps!

+9
May 23 '13 at 19:04
source share

I did this using the OnMapClickListener map:

 private boolean drawing = false; private Polygon polygon; private List<LatLng> points; 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
May 17 '16 at 2:25
source share



All Articles