Sourabh Saldi
Sourabh Saldi

Reputation: 3587

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!!!

Upvotes: 3

Views: 2142

Answers (3)

MaciejGórski
MaciejGórski

Reputation: 22232

  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

Upvotes: 1

Basim Sherif
Basim Sherif

Reputation: 5440

Here is the code to draw circle above map,

    CircleOptions circleOptions = new CircleOptions()
    .center(new LatLng(37.4, -122.1))
    .radius(1000)); // In meters

// Get back the mutable Circle
Circle circle = myMap.addCircle(circleOptions);

And GoogleMap doesn't have a touch listener, so you'll have to override onTouchEvent for the parent view of your MapFragment to drag your circle around.

Upvotes: -1

Lionel Port
Lionel Port

Reputation: 3542

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

Upvotes: 1

Related Questions