繁体   English   中英

如何只用一根手指就能在Google Maps API(android)上旋转?

[英]How can I rotate on Google Maps API(android) with only 1 finger?

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

我遇到过同样的问题。 我所做的是我添加了一个自定义视图,该视图在地图顶部扩展了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);
}

尚无法发表评论,但是我发现Semikunchezzz解决方案不方便使用。 代替单独的FrameLayout,我们可以扩展SupportMapFragment,如Gaucho所示: Google Maps Android API v2-在地图上检测触摸

暂无
暂无

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

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