2016-08-20 40 views
0

我正在開發一個應用程序,將鎖定在用戶的位置,就像在新的口袋妖怪去應用程序,我需要找到一種方法來旋轉只有一個手指。我認爲可能會有一些「拖拽」功能,但我還沒有找到任何可行的方法。有任何想法嗎?如何使用1個手指在Google Maps API(android)上旋轉?

+0

考慮到這是多麼讓人期待的UI表現:https://material.google.com/patterns/gestures.html#gestures-觸覺機制你可能想解釋你在想什麼。除非它只是一個+/-按鈕而是用於旋轉。 –

+0

你的意思是在應用程序或在這裏解釋? – kevinb

+0

用一根手指像一張紙一樣拖動地圖是一種常見手勢。旋轉,你可以在素材UI模式中看到鏈接使用兩個手指。你可能想爲你的應用解釋你的意思是'只用一根手指旋轉',以及它與圓形模式下的拖動不同。 –

回答

1

我有同樣的問題。我所做的是我添加了一個自定義視圖,該視圖將FrameLayout擴展到地圖上,併爲其添加了「onTouchEvent」方法。 我計算了兩條線之間的角度 - 第一條線位於屏幕上的第一個觸摸點和地圖中心之間。 秒位於手指觸摸的最後一個位置與地圖中心之間。剩下要做的就是更新地圖對象的方位,並將值第一個觸摸變量分配給手指在屏幕上移動的最後一個位置。

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); 
} 
相關問題