Detecting overzoom in Google Maps

[Warning: as of 1 October 2008, Google is using an over-simplistic bot detection algorithm, and something as simple as zooming out at regular intervals can trigger it and temporarily block access to Google resources. I recommend waiting until they fix their algorithms to use this technique.]

Here’s a link to a web page showing how to detect overzoom with the Google Maps API.

Overzoom is a big issue for my site OurAirports, which shows a close-up satellite view on each airport’s page (e.g. the former Meigs Field). Unfortunately, there’s no documented way to use the Google Maps API to check if a satellite view is overzoomed (instead of a satellite picture, it’s showing the “We are sorry, but we don’t have imagery at this zoom level for this region…” message). That can be confusing for someone who isn’t a regular Google Maps user and hasn’t actually touched the zoom controls on the map.

The hack

The page above came up with the clever solution of counting paragraphs in the map container. If there is a “sorry” message, there will be HTML p elements inside the map container. Here’s a simple JavaScript function that checks to see if the map is overzoomed, and zooms out one level if it is:

function check_zoom (map)
{
    var zoom = map.getZoom();
    var count = map.getContainer().getElementsByTagName('p').length;

    if (zoom > 1 && count > 0) {
        map.setZoom(zoom - 1);
    }
}

Note that it doesn’t iterate — it just does one check and exits. The easiest way to use this is just to have it run every two seconds or so. If your map is available in a variable named map, this will do the trick:

setInterval("check_zoom(map)", 2000);

Examples

To see how it works, check out Vuotso Airport in Finland. Google Maps doesn’t have very good satellite coverage for Lapland in the far north, but OurAirports now detects that after a couple of seconds and zooms out one step. For a more extreme example, look at Alert Airport, the world’s northernmost permanent airport, in Nunavut, Canada — the code has to zoom out several times until you can see anything in the satellite view.

Caveats

The more elegant solution would be to detect when the map is finished loading after every event that can affect it, but that sounds like too much work to save a couple of milliseconds here and there.

Note that Google can break this at any time simply by adding or removing p elements — it would be much better to have an official, reliable way to detect when a satellite view is overzoomed.

About David Megginson

Scholar, tech guy, Canuck, open-source/data/information zealot, urban pedestrian, language geek, tea drinker, pater familias, red tory, amateur musician, private pilot.
This entry was posted in General and tagged , . Bookmark the permalink.

3 Responses to Detecting overzoom in Google Maps

  1. Cool, but doesn’t seem to work on Safari.

  2. david says:

    Mark: I tested only with Firefox and MSIE, unfortunately. Does Safari have an equivalent of FireBug that you could use to test for JavaScript problems?

  3. Pingback: Megginson Technologies: Quoderat » Blog Archive » sorry.google.com

Comments are closed.