如何中心相机,使标志位于屏幕的底部? (谷歌地图API V2安卓)

问题描述:

在点击标记,摄像机的默认行为是居中在屏幕上,但因为我平时都在信息窗口长的文字说明,它更方便,真正改变相机位置,使标志是屏幕底部(使信息窗口中屏幕的中心)。我想我应该能够做到这一点通过覆盖onMarkerClick功能如下图所示(默认行为将被取消时,该函数返回TRUE)

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 convenient to actually change the camera position so that the marker is on the bottom of screen(making the info window in the center of screen). I think I should be able to do that by overriding onMarkerClick function like below (the default behavior is cancelled when this function return true)

@Override

public boolean onMarkerClick(final Marker marker) {


    // Google sample code comment : We return false to indicate that we have not

    // consumed the event and that we wish
    // for the default behavior to occur (which is for the camera to move
    // such that the
    // marker is centered and for the marker's info window to open, if it
    // has one).

    marker.showInfoWindow();

            CameraUpdate center=
                CameraUpdateFactory.newLatLng(new LatLng(XXXX,
                                                         XXXX));
            mMap.moveCamera(center);//my question is how to get this center

            // return false;
    return true;
}

编辑:

使用公认的答案的步骤问题解决了,codeS如下:

Problem solved using accepted answer's steps, codes below:

@Override

    public boolean onMarkerClick(final Marker marker) {

                //get the map container height
        LinearLayout mapContainer = (LinearLayout) findViewById(R.id.map_container);
        container_height = mapContainer.getHeight();

        Projection projection = mMap.getProjection();

        LatLng markerLatLng = new LatLng(marker.getPosition().latitude,
                marker.getPosition().longitude);
        Point markerScreenPosition = projection.toScreenLocation(markerLatLng);
        Point pointHalfScreenAbove = new Point(markerScreenPosition.x,
                markerScreenPosition.y - (container_height / 2));

        LatLng aboveMarkerLatLng = projection
                .fromScreenLocation(pointHalfScreenAbove);

        marker.showInfoWindow();
        CameraUpdate center = CameraUpdateFactory.newLatLng(aboveMarkerLatLng);
        mMap.moveCamera(center);
        return true;



    }

感谢您的帮助^ ^ ​​

Thanks for helping ^ ^

我会在以后编辑这个答案提供一些code,但我认为可能的工作是这样的:

I might edit this answer later to provide some code, but what I think could work is this:

  1. 获取经纬度经纬度中号的)被点击的标记。
  2. 在转换的经纬度中号的到 M点的),使用Projection.toScreenLocation(LatLng)$c$c>方法。这使您可以标记设备的显示屏上的位置(以像素为单位)。
  3. 在计算点的位置(新焦点的),这是上面的仪M 的一半地图的高度。
  4. 转换在新焦点的回经纬度和中央上的映射。
  1. Get LatLng (LatLng M) of the clicked marker.
  2. Convert LatLng M to a Point (Point M) using the Projection.toScreenLocation(LatLng) method. This gives you the location of the marker on the device's display (in pixels).
  3. Compute the location of a point (New Point) that's above Point M by half of the map's height.
  4. Convert the New Point back to LatLng and center the map on it.

这里我如何获得地图的高度答案看

Look here for my answer on how to get the map's height.