Skip to content Skip to sidebar Skip to footer

How Can I Rotate On Google Maps Api(android) With Only 1 Finger?

I'm developing an app that will lock on a users location, much like in the new Pokemon go app, and I need to find a way to rotate with only one finger. I thought there might be so

Solution 1:

I had the same issue. what I did was I've added a custom view that extends FrameLayout on top of the map and added a "onTouchEvent" method to it. I calculated the angle between two lines- first line is between first touch point on screen and center of the map. second is between the last place the finger has touched and the center of the map. last thing left to do is to update the bearing of the map object and assign the value first touch variable to the last place the finger moved on screen.

classtouch view extendsFrameLayout {

...

publicbooleanonTouchEvent(MotionEvent eve) {
    PointtouchPoint=newPoint();  //first point on screen the user's finger touchesfinal GoogleMap map;
    finalintaction= MotionEventCompat.getActionMasked(eve);
    intpointerIndex= MotionEventCompat.getActionIndex(eve);
    GestureDetectorg=newGestureDetector(getContext(), newGestureDetector.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 actionsfinalPointnewTouchPoint=newPoint();  // 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);
                PointcenterOfMap= 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 mapfinalfloatangle= angleBetweenLines(centerOfMap, touchPoint, centerOfMap, newTouchPoint);
                finalLatLnglatlng=newLatLng(location.getLatitude(), location.getLongitude());  //set camera movement to that positionnewHandler().post(newRunnable() {
                    @Overridepublicvoidrun() {
                        // 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 valuereturntrue;
            }else{
                break;
            }
    }
    returntrue;
}

// convert center of map from Latln object to Point objectpublic Point getCurrentLocation(){
    Projectionprojection= map.getProjection();
    return projection.toScreenLocation(newLatLng(location.getLatitude(), location.getLongitude()));
}

angle calculating class looks like this-

publicfloatangleBetweenLines(Point center,Point endLine1,Point endLine2){
    floata= endLine1.x - center.x;
    floatb= endLine1.y - center.y;
    floatc= endLine2.x - center.x;
    floatd= endLine2.y - center.y;

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

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

Solution 2:

Can not make comments yet, but I found Semikunchezzz solution not convenient to use. Instead of separate FrameLayout, we can extend SupportMapFragment as shown by Gaucho: Google Maps Android API v2 - detect touch on map

Post a Comment for "How Can I Rotate On Google Maps Api(android) With Only 1 Finger?"