How to center the camera so that marker is at the bottom of screen? (Google map api V2 Android)

前端 未结 10 2140
执念已碎
执念已碎 2020-11-30 23:13

When a marker is clicked, the default behavior for the camera is to center it on screen, but because I usually have long text description in the info window, it\'s more conv

10条回答
  •  青春惊慌失措
    2020-11-30 23:27

    I had the same issue, I tried the following perfectly working solution

    mMap.setOnMarkerClickListener(new OnMarkerClickListener() 
            {
                @Override
                public boolean onMarkerClick(Marker marker)
                {
                    int yMatrix = 200, xMatrix =40;
    
                    DisplayMetrics metrics1 = new DisplayMetrics();
                    getWindowManager().getDefaultDisplay().getMetrics(metrics1);
                    switch(metrics1.densityDpi)
                    {
                    case DisplayMetrics.DENSITY_LOW:
                        yMatrix = 80;
                        xMatrix = 20;
                        break;
                    case DisplayMetrics.DENSITY_MEDIUM:
                        yMatrix = 100;
                        xMatrix = 25;
                        break;
                    case DisplayMetrics.DENSITY_HIGH:
                        yMatrix = 150;
                        xMatrix = 30;
                        break;
                    case DisplayMetrics.DENSITY_XHIGH:
                        yMatrix = 200;
                        xMatrix = 40;
                        break;
                    case DisplayMetrics.DENSITY_XXHIGH:
                        yMatrix = 200;
                        xMatrix = 50;
                        break;
                    }
    
                    Projection projection = mMap.getProjection();
                    LatLng latLng = marker.getPosition();
                    Point point = projection.toScreenLocation(latLng);
                    Point point2 = new Point(point.x+xMatrix,point.y-yMatrix);
    
                    LatLng point3 = projection.fromScreenLocation(point2);
                    CameraUpdate zoom1 = CameraUpdateFactory.newLatLng(point3);
                    mMap.animateCamera(zoom1);
                    marker.showInfoWindow();
                    return true;
                }
            });
    

提交回复
热议问题