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

前端 未结 10 2130
执念已碎
执念已碎 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:35

    Anyone who's still looking to center the camera according to location coordinates

    CameraPosition cameraPosition = new CameraPosition.Builder().target(new LatLng(Lat, Lon))
            .zoom(15) 
            .bearing(0)
            .tilt(45)
            .build();
        map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
    

    Credits

    0 讨论(0)
  • 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);
    
    0 讨论(0)
  • 2020-11-30 23:37

    After some experiences i've implemented the solution that fine for me.

    DisplayMetrics metrics = new DisplayMetrics();
    context.getWindowManager().getDefaultDisplay().getMetrics(metrics);
    
    Point targetPoint = new Point(metrics.widthPixels / 2, metrics.heightPixels - metrics.heightPixels / 9);
    LatLng targetLatlng = map.getProjection().fromScreenLocation(targetPoint);
    double fromCenterToTarget = SphericalUtil.computeDistanceBetween(map.getCameraPosition().target, targetLatlng);
    
    LatLng center = SphericalUtil.computeOffset(new LatLng(location.latitude, location.longitude), fromCenterToTarget/1.2, location.bearing);
    CameraUpdate camera = CameraUpdateFactory.newLatLng(center);
    map.animateCamera(camera, 1000, null);
    

    Here. First, we pick the physical point on the screen where the marker should be moved. Then, convert it to LatLng. Next step - calculate distance from current marker position (in center) to target. Finally, we move the center of map straight from the marker to calculated distance.

    0 讨论(0)
  • 2020-11-30 23:43

    If you don't care about the map zooming in and just want the marker to be at the bottom see below, I think it's a simpler solution

    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);
    
    0 讨论(0)
提交回复
热议问题