简体   繁体   English

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

[英]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. 我正在开发一个可以锁定用户位置的应用程序,就像在新的Pokemon go应用程序中一样,我需要找到一种仅用一根手指即可旋转的方法。 I thought there might be some 'drag' functionality, but I have yet to find anything that will work. 我以为可能会有一些“拖动”功能,但是我还没有找到任何可行的方法。 Any ideas? 有任何想法吗?

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. 我所做的是我添加了一个自定义视图,该视图在地图顶部扩展了FrameLayout,并为其添加了“ onTouchEvent”方法。 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. 最后要做的就是更新地图对象的方位,并将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()));
}

angle calculating class looks like 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);
}

Can not make comments yet, but I found Semikunchezzz solution not convenient to use. 尚无法发表评论,但是我发现Semikunchezzz解决方案不方便使用。 Instead of separate FrameLayout, we can extend SupportMapFragment as shown by Gaucho: Google Maps Android API v2 - detect touch on map 代替单独的FrameLayout,我们可以扩展SupportMapFragment,如Gaucho所示: Google Maps Android API v2-在地图上检测触摸

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

相关问题 如何启用谷歌地图 android api 以实现平滑的两指缩放? - How to enable google maps android api for smooth two finger zoom? 如何指定多个Google Maps Android API? - How do I can specific multiple Google Maps Android API? 我可以编辑适用于Android的Google Maps API吗? - Can I edit the Google maps api for android? 如何使用 Google Maps API 在我的 Google Maps Android 应用程序中添加路线? - How can I add a route in my Google Maps Android application using Google Maps API? Android:如何用一根手指旋转视图 - Android: How to rotate a view with one finger 如何在Android Studio中使用Google Maps Api v2创建Android应用程序? - How can I create an Android application in Android Studio that uses the Google Maps Api v2? Google Maps API(适用于Android):我能否获得*避免*航路点的路线? - Google Maps API (for Android): Can I get directions to *avoid* a waypoint? 如何在Android中以横向模式旋转显示? - How can I rotate display only in landscape mode in android? 如何使用谷歌地图android api过滤器搜索餐馆? - How can I search for restaurants using filters with google maps android api? 如何在Android Google Maps API V2中为GroundOverlay创建onTap()事件? - How can I create an onTap() event for the GroundOverlay in Android Google Maps API V2?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM