简体   繁体   English

Android:谷歌地图v2绘制可拖动圈

[英]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!!! 提前致谢!!!

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)));
  1. Add a View above the SupportMapFragment SupportMapFragment上方添加一个View
  2. Add OnTouchListener to this View OnTouchListener添加到此View
  3. Detect if user pressed inside the circle on DOWN event. 检测用户是否在DOWN事件中按下了圆圈。 You will have to use a combination of Projection to convert x,y to LatLng and Location.distanceBeetween 您必须使用Projection的组合将x,y转换为LatLngLocation.distanceBeetween
  4. If user didn't press inside circle, return false to give map chance to handle event 如果用户没有在圈内按,则返回false以给予映射机会处理事件
  5. If user pressed inside circle, return true and save relevant variables 如果用户在圈内按下,则返回true并保存相关变量
  6. On MOVE event continue to use Projection to calculate new center based on initial and new x,y 在MOVE事件中,继续使用Projection根据初始和新x,y计算新中心

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. 并且GoogleMap没有触控侦听器,因此您必须覆盖onTouchEvent以使MapFragment的父视图拖动您的圆圈。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM