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

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

    I have been trying out all the solutions proposed here, and came with a combined implementation of them. Considering, map projection, tilt, zoom and info window height.

    It doesn't really place the marker at the bottom of the "camera view", but I think it accommodates the info window and the marker centre pretty well in most cases.

    @Override
    public boolean onMarkerClick(Marker marker) {
        mIsMarkerClick = true;
        mHandler.removeCallbacks(mUpdateTimeTask);
        mLoadTask.cancel(true);
        getActivity().setProgressBarIndeterminateVisibility(false);
        marker.showInfoWindow();
        Projection projection = getMap().getProjection();
        Point marketCenter = projection.toScreenLocation(marker.getPosition());
        float tiltFactor = (90 - getMap().getCameraPosition().tilt) / 90;
        marketCenter.y -= mInfoWindowAdapter.getInfoWindowHeight() / 2 * tiltFactor;
        LatLng fixLatLng = projection.fromScreenLocation(marketCenter);
    
        mMap.animateCamera(CameraUpdateFactory.newLatLng(fixLatLng), null);
    
        return true;
    }
    

    And then, your custom adapter would have to keep an instance of the info window inflated view, to be able to fetch its height.

    public int getInfoWindowHeight(){
        if (mLastInfoWindoView != null){
            return mLastInfoWindoView.getMeasuredHeight();
        }
        return 0;
    }
    

提交回复
热议问题