首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
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

Stack Overflow用户

发布于 2020-08-14 15:37:42

我也遇到了这个问题,并以一种粗糙的方式解决了它。让我们先声明一个双精度字段。你需要根据你的需要调整它的值,但我建议你保持在0.001~0.009之间,否则在缩放动画之后你可能会错过你的标记。

代码语言:javascript
运行
复制
  double offset = 0.009 
  /*You can change it based on your requirement.
 For left-right alignment please kindly keep it between 0.001~0.005 */

底部居中的

代码语言:javascript
运行
复制
    LatLng camera = new LatLng(marker.getPosition().latitude+offset , marker.getPosition().longitude);
//Here "marker" is your target market on which you want to focus

顶部居中的

代码语言:javascript
运行
复制
LatLng camera = new LatLng(marker.getPosition().latitude-offset , marker.getPosition().longitude);

左居中的

代码语言:javascript
运行
复制
LatLng camera = new LatLng(marker.getPosition().latitude, marker.getPosition().longitude+offset);

右居中的

代码语言:javascript
运行
复制
LatLng camera = new LatLng(marker.getPosition().latitude-offset , marker.getPosition().longitude-offset);

最后调用-

代码语言:javascript
运行
复制
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(camera, yourZoom));
票数 1
EN
查看全部 10 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16764002

复制
相关文章

相似问题

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