首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何将相机居中,使标记位于屏幕底部?(谷歌地图应用程序接口V2 Android)

如何将相机居中,使标记位于屏幕底部?(谷歌地图应用程序接口V2 Android)
EN

Stack Overflow用户
提问于 2013-05-27 05:49:33
回答 10查看 33.9K关注 0票数 52

当一个标记被点击时,相机的默认行为是在屏幕上居中,但因为我通常在信息窗口中有很长的文本描述,所以更方便的是实际改变相机的位置,使标记在屏幕的底部(使信息窗口在屏幕的中心)。我想我应该能够通过重写onMarkerClick函数来做到这一点,如下所示(当这个函数返回true时,默认行为被取消)

代码语言:javascript
运行
复制
@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;
}

编辑:

使用accepted的步骤解决问题,代码如下:

代码语言:javascript
运行
复制
@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;



    }

感谢您的帮助^^

EN

回答 10

Stack Overflow用户

回答已采纳

发布于 2013-05-27 06:09:03

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

使用方法将所单击的marker.

  1. Convert LatLng M的Get LatLng (LatLng M)获取到一个点(点M)。这将为您提供设备显示屏上标记的位置(在pixels).
  2. Compute中,为点M上方的点(新点)的位置为地图的一半height.
  3. Convert新点返回LatLng并使地图居中。

关于如何获得地图的高度,请在here上查找我的答案。

代码语言:javascript
运行
复制
    // 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);
票数 57
EN

Stack Overflow用户

发布于 2013-08-06 00:16:45

我更喜欢Larry McKenzie的答案,它不依赖于屏幕投影(即mProjection.toScreenLocation()),我猜测当地图缩放级别较低时,投影分辨率会变差,这使得我有时无法获得准确的位置。因此,基于google地图规范的计算肯定能解决这个问题。

下面是一个示例代码,将标记从底部移动到屏幕尺寸的30%。

代码语言:javascript
运行
复制
zoom_lvl = mMap.getCameraPosition().zoom;
double dpPerdegree = 256.0*Math.pow(2, zoom_lvl)/170.0;
double screen_height = (double) mapContainer.getHeight();
double screen_height_30p = 30.0*screen_height/100.0;
double degree_30p = screen_height_30p/dpPerdegree;      
LatLng centerlatlng = new LatLng( latlng.latitude + degree_30p, latlng.longitude );         
mMap.animateCamera( CameraUpdateFactory.newLatLngZoom( centerlatlng, 15 ), 1000, null);
票数 9
EN

Stack Overflow用户

发布于 2016-09-25 13:45:11

如果您不关心地图的放大,只希望标记位于底部,请参阅下面的内容,我认为这是一个更简单的解决方案

代码语言:javascript
运行
复制
double center = mMap.getCameraPosition().target.latitude;
double southMap = mMap.getProjection().getVisibleRegion().latLngBounds.southwest.latitude;

double diff = (center - southMap);

double newLat = marker.getPosition().latitude + diff;

CameraUpdate centerCam = CameraUpdateFactory.newLatLng(new LatLng(newLat, marker.getPosition().longitude));

mMap.animateCamera(centerCam);
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16764002

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档