Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android:Google map v2 draw dragable circle

I need to draw circle of some radius on some given location that can be dragged to some other point on map I can well draw circle like this

mMap.addCircle(new CircleOptions().center(circleLocation).radius(10).strokeColor(Color.RED).fillColor(Color.RED));

Now I need to drag this circle on map in such a way that when I click on this circle map gets locked and on moving my finger this circle should move on circle and not map itself

Thanks in advance!!!

like image 261
Sourabh Saldi Avatar asked Apr 27 '26 23:04

Sourabh Saldi


2 Answers

I don't know how to do with shapes but one alternative is to use a marker. It doesn't have the exact result as the marker is drawn flush to the screen view rather than flush to the map but it does have built in drag support. You would need to create a circle image.

map.addMarker(new MarkerOptions()
         .position(MELBOURNE)
         .title("Melbourne")
         .draggable(true)
         .icon(BitmapDescriptorFactory.fromResource(R.drawable.circle)));
like image 87
Lionel Port Avatar answered Apr 29 '26 12:04

Lionel Port


  1. Add a View above the SupportMapFragment
  2. Add OnTouchListener to this View
  3. Detect if user pressed inside the circle on DOWN event. You will have to use a combination of Projection to convert x,y to LatLng and Location.distanceBeetween
  4. If user didn't press inside circle, return false to give map chance to handle event
  5. If user pressed inside circle, return true and save relevant variables
  6. On MOVE event continue to use Projection to calculate new center based on initial and new x,y
like image 33
MaciejGórski Avatar answered Apr 29 '26 13:04

MaciejGórski