Wednesday, April 12, 2006

Gmap v2 - resizeMap()

A few days ago Google announced their latest version of the Google Maps API on the API blog. The latest release is version 2.45 but the official v2 (2.44) release post announced that all API users would be forced to upgrade from v1 in approximately a month (5/03/2006).

I thought their might be some people out there who are using John Resig's Auto-Scaling Maps script. If you are one of those folks, you will need to modify the resizeMap function for version 2 of the Gmaps API. Consequently, I went ahead and re-wrote John's function so you don't have to. If you aren't familiar with his resizeMap function, it simply takes a collection of points representing the markers that are currently rendered on the map, and then centers the map to the middle of those points while adjusting the zoom level of the map to ensure all of the makers are plotted are visible. The zoom level is set to the finest zoom level that still contains all of the markers.

It would be quite convenient if Google provided this functionality inside of the API but it appears that they have no plans of working it in to the second version of the API. Thanks John for providing the original source code for this functionality. The Auto-Scaling code for v2 can be downloaded here.

Get all of the MSN Conspiracy Game Answers here


At 4/16/2006 8:49 PM, Anonymous mark said...

Thanks for the rewrite ... it's exactly what I needed.

But for some reason this is always calculating a zoom level of 1 when the lat/lon bounds seems to be correct.

I must be doing something wrong ... but it's late and I can't seem to find it.

At 4/17/2006 6:59 PM, Blogger G00GLEFACT said...

If you send me a link to the source I can help you correct the problem. My only off the cuff suggestion is to make sure for GLatLng(lat,lon) you have reversed the parameter order of lat and lon from GPoint(lon,lat).

At 4/25/2006 11:19 PM, Blogger god said...

This comment has been removed by a blog administrator.

At 6/01/2006 12:27 AM, Blogger Paul H├Ąder said...

I'm having trouble downloading the code, I'd be interested to see it though.

At 9/03/2006 12:23 PM, Blogger dp said...

Hey, this sounds great, but the link is broken. Can you repost your rewrite? I've been attempting my own and am a bit stumped. Share your genius please! :)

At 12/09/2006 7:01 AM, Blogger Peter said...

function resizeV2(map, points) {

// by
// please let me know if it works for you.

// we have to resort to the old 'GBounds' object with it's old 'minX' etc. properties that
// actuallay represent lat/lng values. quite weird...
var bounds = new GBounds(points);

var sw = new GLatLng(bounds.maxY, bounds.minX);
var ne = new GLatLng(bounds.minY, bounds.maxX);

var latLngBounds = new GLatLngBounds(sw, ne);

var center = latLngBounds.getCenter();
var zoom = map.getBoundsZoomLevel(latLngBounds);
map.setCenter(center, zoom);



Post a Comment

<< Home