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

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

提交回复
热议问题