Friday, June 8, 2007

GMarkers are everywhere! Where do I look?

I found a common paradigm in programming GMap applications, you have a collection of markers to add to the map and you want to zoom the map and center it in such a way to show all markers at the closest zoom level possible.

GBounds over tall buildings!

All solutions to this problem involve some intense calculations, so I researched the API documentation and found that the GBounds class does this work for you. pass it a collection of points and it will tell you the four max points of a plane that will encompass all points, perfect! ... not quite.

Hobbling over the API

Unfortunately a GBounds object doesn't give us all the necessary information, but its got enough to get us there. Take the min and max coordinates to form the north east and south west GLatLng objects and create a GLatLngBounds object which will then allow you to retrieve necessary information.

GLatLngBounds on point

Now that you've got your GLatLngBounds object you can find the center by the method "getCenter()" and the zoom is found by passing the GLatLngBounds object to the method "getBoundsZoomLevel" of the map object you want to position.

An implementation, GOverlayOverseer

I've taken these ideas and created my own version of the "GMarkerManager" that is based on the ideas of hiding an overlay based on parameters of minZoom and maxZoom. I've done some things very differently though, first is the value of minZoom and maxZoom. minZoom in my class represents the min zoom value that will be considered, such that an object with a minZoom of 10 will not be seen when the viewer's zoom level is less than 10, remember 1 represents no zooming, you're looking at the entire world. maxZoom is the micro zoom, meaning that if the zoom level of the map is greater than the objects specified zoomLevel it will be hidden. This doesn't make a whole lot of sense for a GMarker, which is one point on the map but this feature becomes increasingly valuable as you begin working with polygons and polylines.

View attempts to concretely solve this problem, for good.

No comments: