How to center the camera so that marker is at the bottom of screen? (Google map api V2 Android)

前端 未结 10 2143
执念已碎
执念已碎 2020-11-30 23:13

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 conv

10条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-30 23:36

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

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

提交回复
热议问题