【问题标题】:How can I rotate on Google Maps API(android) with only 1 finger?如何仅用一根手指在 Google Maps API(android) 上旋转?
【发布时间】:2016-08-20 20:36:11
【问题描述】:

我正在开发一款可以锁定用户位置的应用,就像在新的 Pokemon go 应用中一样,我需要找到一种只用一根手指旋转的方法。我认为可能有一些“拖动”功能,但我还没有找到任何可行的方法。有什么想法吗?

【问题讨论】:

  • 鉴于这是人们期望 UI 的行为方式:material.google.com/patterns/… 您可能想解释一下您的想法。除非它只是一个 +/- 按钮,而是用于旋转。
  • 您的意思是在应用程序中还是在此处解释?
  • 用一根手指像一张纸一样拖动地图是一种常见的手势。如您在材质 UI 模式链接中所见,旋转使用两个手指。您可能想为您的应用解释“仅用一根手指旋转”是什么意思,以及它与以圆形模式拖动有何不同。

标签: android google-maps location


【解决方案1】:

我有同样的问题。我所做的是添加了一个自定义视图,该视图在地图顶部扩展了 FrameLayout,并为其添加了一个“onTouchEvent”方法。 我计算了两条线之间的角度- 第一行位于屏幕上的第一个触摸点和地图中心之间。 第二个是手指最后触摸的位置和地图中心之间。最后要做的就是更新地图对象的方位,并将值 first touch 变量分配给手指在屏幕上最后移动的位置。

class touch view extends FrameLayout {

...

public boolean onTouchEvent(MotionEvent eve) {
    Point touchPoint = new Point();  //first point on screen the user's finger touches
    final GoogleMap map;
    final int action = MotionEventCompat.getActionMasked(eve);
    int pointerIndex = MotionEventCompat.getActionIndex(eve);
    GestureDetector g = new GestureDetector(getContext(), new GestureDetector.SimpleOnGestureListener());
    g.onTouchEvent(eve);
    switch (action){
        case MotionEvent.ACTION_DOWN:
            // get the point the user's finger touches
            touchPoint.x =(int) MotionEventCompat.getX(eve,pointerIndex);
            touchPoint.y =(int) MotionEventCompat.getY(eve,pointerIndex);
            break;
        case MotionEvent.ACTION_MOVE:
            if(eve.getPointerCount()<2) {   // leave two finger gestures for other actions
                final Point newTouchPoint = new Point();  // the new position of user's finger on screen after movement is detected
                newTouchPoint.x = (int) MotionEventCompat.getX(eve, pointerIndex);
                newTouchPoint.y = (int) MotionEventCompat.getY(eve, pointerIndex);
                Point centerOfMap = getCenterOfMapAsPoint();   // center of map(as Point object) for calculation of angle
                // now you need to calculate the angle betwwen 2 lines with centerOfMap as center:
                //line 1: imaginary line between first touch detection on screen - and the center of the map
                //line 2: imaginary line between last place finger moved on screen - and the center of the map
                final float angle = angleBetweenLines(centerOfMap, touchPoint, centerOfMap, newTouchPoint);
                final LatLng latlng = new LatLng(location.getLatitude(), location.getLongitude());  //set camera movement to that position
                new Handler().post(new Runnable() {
                    @Override
                    public void run() {
                        // move the camera (NOT animateCamera() ) to new position with "bearing" updated
                        map.moveCamera(CameraUpdateFactory.newCameraPosition(CameraPosition.builder().target(latlng).tilt(67.5f).zoom(map.getCameraPosition().zoom).bearing(map.getCameraPosition().bearing - angle).build()));
                    }
                });
                touchPoint = newTouchPoint; // update touchPoint value
                return true;
            }else{
                break;
            }
    }
    return true;
}

// convert center of map from Latln object to Point object
public Point getCurrentLocation(){
    Projection projection = map.getProjection();
    return projection.toScreenLocation(new LatLng(location.getLatitude(), location.getLongitude()));
}

角度计算类是这样的-

public float angleBetweenLines(Point center,Point endLine1,Point endLine2){
    float a = endLine1.x - center.x;
    float b = endLine1.y - center.y;
    float c = endLine2.x - center.x;
    float d = endLine2.y - center.y;

    float atan1 = (float) Math.atan2(a,b);
    float atan2 = (float) Math.atan2(c,d);

    return (float) ((atan1 - atan2) * 180 / Math.PI);
}

【讨论】:

    【解决方案2】:

    还不能制作 cmets,但我发现 Semikunchezzz 解决方案使用起来不方便。我们可以扩展 SupportMapFragment,而不是单独的 FrameLayout,如 Gaucho 所示: Google Maps Android API v2 - detect touch on map

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-20
      • 2014-01-09
      • 2017-07-19
      • 1970-01-01
      相关资源
      最近更新 更多