I have 10 markers in the GoogleMap
. I want to zoom in as much as possible and keep all markers in view? In the earlier version this can be achieved from zoomToSpan()
but in v2 I have no idea how about doing that. Further, I know the radius of the circle that needs to be visible.
相关问题
- How can I create this custom Bottom Navigation on
- Bottom Navigation View gets Shrink Down
- How to make that the snackbar action button be sho
- Listening to outgoing sms not working android
- How to create Circular view on android wear?
相关文章
- android开发 怎么把图片放入drawable的文件夹下
- android上如何获取/storage/emulated/下的文件列表
- androidStudio有个箭头不认识
- SQLite不能创建表
- Windows - Android SDK manager not listing any plat
- Animate Recycler View grid when number of columns
- Why is the app closing suddenly without showing an
- Android OverlayItem.setMarker(): Change the marker
So
for example, your Google Map is inside RelativeLayout:
From this code, I am displaying multiple markers with particular zoom on map screen.
// Declared variables
// Method for adding multiple marker points with drawable icon
// For adding multiple markers visible on map
I have one other way to do this same thing works perfectly. so the idea behind to show all markers on the screen we need a center lat long and zoom level. here is the function which will give you both and need all marker's Latlng objects as input.
This function return Pair object which you can use like
you can instead of using padding to keep away your markers from screen boundaries, you can adjust zoom by -1.
You should use the
CameraUpdate
class to do (probably) all programmatic map movements.To do this, first calculate the bounds of all the markers like so:
Then obtain a movement description object by using the factory:
CameraUpdateFactory
:Finally move the map:
Or if you want an animation:
That's all :)
Clarification 1
Almost all movement methods require the
Map
object to have passed the layout process. You can wait for this to happen using theaddOnGlobalLayoutListener
construct. Details can be found in comments to this answer and remaining answers. You can also find a complete code for setting map extent usingaddOnGlobalLayoutListener
here.Clarification 2
One comment notes that using this method for only one marker results in map zoom set to a "bizarre" zoom level (which I believe to be maximum zoom level available for given location). I think this is expected because:
LatLngBounds bounds
instance will havenortheast
property equal tosouthwest
, meaning that the portion of area of the earth covered by thisbounds
is exactly zero. (This is logical since a single marker has no area.)bounds
toCameraUpdateFactory.newLatLngBounds
you essentially request a calculation of such a zoom level thatbounds
(having zero area) will cover the whole map view.Map
object doesn't support this value so it is clamped to a more reasonable maximum level allowed for given location.Another way to put it: how can
Map
object know what zoom level should it choose for a single location? Maybe the optimal value should be 20 (if it represents a specific address). Or maybe 11 (if it represents a town). Or maybe 6 (if it represents a country). API isn't that smart and the decision is up to you.So, you should simply check if
markers
has only one location and if so, use one of:CameraUpdate cu = CameraUpdateFactory.newLatLng(marker.getPosition())
- go to marker position, leave current zoom level intact.CameraUpdate cu = CameraUpdateFactory.newLatLngZoom(marker.getPosition(), 12F)
- go to marker position, set zoom level to arbitrarily chosen value 12.I couldnt use the onGlobalLayoutlistener, so here is another solution to prevent the
"Map size can't be 0. Most likely, layout has not yet occured for the map view. Either wait until layout has occurred or use newLatLngBounds(LatLngBounds, int, int, int) which allows you to specify the map's dimensions."
error:this would help.. from google apis demos
for clear info look at this url. https://github.com/googlemaps/android-samples/blob/master/ApiDemos/app/src/main/java/com/example/mapdemo/MarkerDemoActivity.java