如何将相机居中以使标记位于屏幕底部?(谷歌地图api V2 Android) [英] How to center the camera so that marker is at the bottom of screen? (Google map api V2 Android)

查看:24
本文介绍了如何将相机居中以使标记位于屏幕底部?(谷歌地图api V2 Android)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当一个标记被点击时,相机的默认行为是让它在屏幕上居中,但是因为我通常在信息窗口中有很长的文字描述,所以实际更改相机位置以便标记打开会更方便屏幕底部(使信息窗口位于屏幕中央).我想我应该能够通过像下面这样覆盖 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;
}

使用已接受答案的步骤解决了问题,代码如下:

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;



    }

感谢帮助^ ^

推荐答案

我稍后可能会编辑此答案以提供一些代码,但我认为可行的是:

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

  1. 获取点击标记的LatLng (LatLng M).
  2. 使用 Projection.toScreenLocation(LatLng) 方法.这为您提供了设备显示屏上标记的位置(以像素为单位).
  3. 计算一个点(新点)的位置,该点位于M点上方地图高度的一半.
  4. New Point 转换回 LatLng 并将地图居中.
  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.

    // googleMap is a GoogleMap object
    // view is a View object containing the inflated map
    // marker is a Marker object
    Projection projection = googleMap.getProjection();
    LatLng markerPosition = marker.getPosition();
    Point markerPoint = projection.toScreenLocation(markerPosition);
    Point targetPoint = new Point(markerPoint.x, markerPoint.y - view.getHeight() / 2);
    LatLng targetPosition = projection.fromScreenLocation(targetPoint);
    googleMap.animateCamera(CameraUpdateFactory.newLatLng(targetPosition), 1000, null);

这篇关于如何将相机居中以使标记位于屏幕底部?(谷歌地图api V2 Android)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆