2

I am using Google map in android where I am populating different markers on the map.In the mean time I am difficulty in implementing the zoom level which cover all the markers on the map.

Can anyone tell me the right way of implementing the zoomToSpan in android.

Altaf
  • 5,150
  • 10
  • 39
  • 55

1 Answers1

3

You need to track the positions of all the markers to determine the proper bounds:

  1. Initialize 4 integers: the top and right bounds with Integer.MIN_VALUE, the bottom and left bounds with Integer.MAX_VALUE.
  2. Iterate over the position of all your markers and update the bounds appropriately: left should be the smallest latitude, right the largest latitude, bottom the smallest longitude, top the largest latitude (all these using the latitude E6 value).
  3. Pass zoomToSpan the difference between right and left and the difference between top and bottom, and you should be good.

You may need to do some extra work to manage centering the map properly so everything is in view, but that should about do it.

Femi
  • 64,273
  • 8
  • 118
  • 148
  • I have tried it in some cases it is working while in some cases its giving me wrong output. – Altaf May 02 '11 at 01:50
  • Its possible there is a bug in the Map zoom implementation, but more likely that there's an error in your code: this works for me very reliably. When you say wrong output, what do you mean: it doesn't zoom at all, or it doesn't zoom to cover all markers? Are you absolutely sure you are calling zoomToSpan from the main loop thread, and that all markers are used in the bounds check above (you aren't adding new markers on some other thread after calling zoomToSpan)? – Femi May 02 '11 at 03:08
  • You'll need to show some code: I've had this working and in production for over 6 months. – Femi May 09 '11 at 00:18
  • is there any way to implement zoomToSpan in customMaps i.e. not google map? – Yauraw Gadav Oct 19 '12 at 05:36