Hey, this is definitely something you can accomplish using the Google Maps API.
One important thing you need to do is update the GLatLngBounds object before trying to get the GMap2 object to recalculate its position and zoom level.
To do this, I would suggest storing some kind of data warehouse of all points used by GMarkers.
Using GEvent listeners, you can also bind the zoomToBounds function to the event when GMarker is deleted.
Here is the code snippet I'm talking about:
var bounds = new GLatLngBounds(); var points = {}; function createMarker(location) { var point = new GLatLng(location.lat,location.lon); var marker = new GMarker(point); marker.myMarkerName = 'uniqueNameToIDMarkerPointLater'; points[marker.myMarkerName] = point; GEvent.addListener(marker,"remove",function() { zoomToBounds(this.myMarkerName); }; bounds.extend(point); map.addOverlay(marker); } function zoomToBounds(name) { points[name]=null; bounds = new GLatLngBounds(); for (var point in points) { if (points[point]!=null) { bounds.extend(points[point]); } } map.setCenter(bounds.getCenter()); map.setZoom(map.getBoundsZoomLevel(bounds)-1); }
The GLatLngBounds object does not save all the points that it used to calculate the maximum and minimum borders, so you need to create a new object to redefine the borders of the rectangle.
I also created an example of the functioning of this located here .
Feel free to use the source code for everything you need - let me know how you made it out, or if you have other questions!
Here is the code without comments:
var bounds = new GLatLngBounds(); var points = {}; function createMarker(location) { var point = new GLatLng(location.lat,location.lon); var marker = new GMarker(point); marker.myMarkerName = 'uniqueNameToIDMarkerPointLater'; points[marker.myMarkerName] = point; GEvent.addListener(marker,"remove",function() { zoomToBounds(this.myMarkerName); }; bounds.extend(point); map.addOverlay(marker); } function zoomToBounds(name) { points[name]=null; bounds = new GLatLngBounds(); for (var point in points) { if (points[point]!=null) { bounds.extend(points[point]); } } map.setCenter(bounds.getCenter()); map.setZoom(map.getBoundsZoomLevel(bounds)-1); }
source share