簡體   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