Move camera position to fit LatLngBounds with regard to marker height

会有一股神秘感。 提交于 2020-01-13 10:29:07

问题


I want to move camera to position to fit LatLngBounds with regard to marker height. So far I'm able for fit the anchors of markers with this code:

LatLngBounds.Builder builder = new LatLngBounds.Builder();
for (Marker marker : markerList) {
    builder.include(marker.getPosition());
}
LatLngBounds bounds = builder.build();

int padding = getActivity().getResources().getDimensionPixelSize(R.dimen.home_map_padding); // offset from edges of the map in pixels
cameraUpdate = CameraUpdateFactory.newLatLngBounds(bounds, padding);
mGoogleMap.animateCamera(cameraUpdate);

It produces output like this:

As you can see there's empty space at the bottom (this is int padding). I'd like to have the padding to be equal like this:


回答1:


Your question is similar to the one posted here:

Defining a CameraUpdate with LatLngBounds with different padding values

The solution is to set the top padding of the actual GoogleMap to the height of your marker icon.

Something like this will do:

Drawable drawable = <YOUR DRAWABLE HERE>;
final int padding = drawable.getIntrinsicHeight();
map.setPadding(0, padding, 0, 0);

Looks like this with my custom marker image (rulers inserted for comparison):




回答2:


I don't believe there's any way to get the height of the marker unless you're using a custom marker. However given that the markers stay the same size you can scroll the camera view up by a set amount.

You're going to have to play around a bit with how much to scroll by but once you get the value you can then scale it based on screen density if necessary.



来源:https://stackoverflow.com/questions/28813371/move-camera-position-to-fit-latlngbounds-with-regard-to-marker-height

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!