2016-08-20 5 views
0

新しいポケモンgoアプリのように、ユーザーの場所に固定するアプリを開発しています.1本の指で回転する方法を見つける必要があります。私はいくつかの 'ドラッグ'機能があるかもしれないと思ったが、私はまだ動作するものを見つける必要はない。何か案は?1本の指でGoogle Maps API(アンドロイド)を回転するにはどうすればよいですか?

+0

次のようになります。https://material.google.com/patterns/gestures.html#gestures-あなたが考えていることを説明したいと思うかもしれません。 +/-ボタンだけで回転しない限り。 –

+0

あなたはアプリかここで説明を意味するのですか? – kevinb

+0

1本の指で紙のように地図をドラッグするのが一般的な方法です。マテリアルのUIパターンリンクで見られるように回転すると、2本の指が使用されます。あなたは、あなたのアプリのために、「1本の指だけで回転する」ことと、円形パターンでドラッグとはどのように異なるかを説明することができます。 –

答えて

1

私は同じ問題がありました。私がしたのは、マップの上にFrameLayoutを拡張し、それに "onTouchEvent"メソッドを追加したカスタムビューを追加したことでした。 私は2つの線の間の角度を計算しました。 最初の線は画面上の最初のタッチ点と地図の中心の間です。 秒は、指が触れた最後の場所とマップの中心の間にあります。最後に行うべきことは、地図オブジェクトの方位を更新し、最初のタッチ変数の値を、指が画面上で最後に移動した場所に割り当てることです。クラスを計算

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())); 
} 

角度が、これは、人々はUIが動作するように期待する方法であることを考えるとthis-

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); 
} 
関連する問題