如何使相机居中,使标记位于屏幕的底部?(谷歌地图api V2 Android)

Arc*_*ect 43 android google-maps android-maps

单击标记时,摄像机的默认行为是将其置于屏幕中心,但由于我通常在信息窗口中有长文本描述,因此实际更改摄像机位置以使标记位于底部时更方便屏幕(使信息窗口位于屏幕中央).我想我应该能够通过覆盖onMarkerClick函数来做到这一点(当此函数返回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;
}
Run Code Online (Sandbox Code Playgroud)

编辑:

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

@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;



    }
Run Code Online (Sandbox Code Playgroud)

谢谢你的帮助^ ^

zbr*_*zbr 48

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

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

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

    // 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);
Run Code Online (Sandbox Code Playgroud)

  • 当相机倾斜和旋转时,可以确认它不起作用 (2认同)

Sam*_*mar 20

是的,使用Projection类.进一步来说:

获取地图的投影:

 Projection projection = map.getProjection();
Run Code Online (Sandbox Code Playgroud)

获取标记的位置:

 LatLng markerLocation = marker.getPosition();
Run Code Online (Sandbox Code Playgroud)

将位置传递给Projection.toScreenLocation()方法:

 Point screenPosition = projection.toScreenLocation(markerLocation);
Run Code Online (Sandbox Code Playgroud)

像这样你可以相对于中心或屏幕周围移动你的标记

Point mappoint = googleMap.getProjection().toScreenLocation(new LatLng(latitude, longitude));
        mappoint.set(mappoint.x, mappoint.y-30);
        googleMap.animateCamera(CameraUpdateFactory.newLatLng(googleMap.getProjection().fromScreenLocation(mappoint)));
Run Code Online (Sandbox Code Playgroud)


小智 7

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

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

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);
Run Code Online (Sandbox Code Playgroud)


use*_*242 5

如果您不关心地图放大并且只希望标记位于底部(见下文),我认为这是一个更简单的解决方案

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);
Run Code Online (Sandbox Code Playgroud)