- Experts Exchange Approved
You can use the Google Maps API to easily draw "static" maps. This lets you present location-aware information to your web and mobile clients. Static Maps have a great advantage - they are rendered as a standard image. There is no Javascript or CSS needed. This means they are immediately useful on almost any browser and on almost any device.
Don't let the term "static" fool you -- this is a powerful mapping system. You can add markers, icons, paths, polygons and more to the maps. The maps are easy to draw because Google does all the heavy lifting. Your only requirement is to prepare a standard HTML <img /> tag that sends the data to the Google API. Google returns a PNG image (other image types are available, too) containing the map, markers and other characteristics that you have requested. The width and height of the map are completely under your control (up to the Google limits, currently 640px), so your page design is easily accommodated. And there are special features for mobile customers.
To get started, you might want to look over the Google man pages here:
http://code.google.com/api
Once you have read that, you know everything that will be demonstrated in this article, and more.
The usefulness of your map and markers will depend on three principal components. These are the zoom level, center point, and the size of the map image. In Google Maps, zoom level numbers increase as you get closer to the earth. A map that has too low a zoom level number may show too much area to give needed detail. A map with too high a zoom level number may not be big enough to show all the markers you have placed. There is no error message; out-of-bounds markers are simply off the chart.
Let's start with a simple example -- a class that will return the HTML <img /> tag. We will create an object with some default settings, and we have setters to change the most important properties of the object. You might want to add your own twist to this class, including your custom icons, or setting your preferred default location for the center of the map. You might also want to extend the class to add more methods. Here are a couple of the most important functions.
The setCenter() method establishes the geographic center point for the map. You might or might not place a marker here. If you are creating a map that shows a client's home location and nearby stores, you might want to use setCenter() to center the map as close as possible to the client location. If all you have for the client location is a ZIP code, it might be best to omit the marker, since it is unlikely that the ZIP code is an exact pointer to the client address.
The setMarker() and setIcon() methods place visual cues on the map. You can set the marker locations by a variety of data types. If you know the "geocode" (latitude, longitude) you can use these numbers to place the marker. You can also place the marker by naming a famous spot ("The White House") or by a landmark in a city ("Landon School, Bethesda MD") or by a street address and ZIP code.
Or even just a ZIP code. ZIP codes are not exact locations (they are postal carrier routes) but often a ZIP code is good enough to get an approximate location. ZIP codes are often used to answer the question, "Where are the nearest stores?"
If you use anything other than the geocode, you need to be aware that Google may be "creative" in the way it interprets place names. For example, try using "THE US CAPITOL" as a center point. You may find that you really want, "THE US CAPITOL, WASHINGTON DC" instead. Generally more detail is better when you are giving a text description of a location to Google Maps.
Custom icons provide us with a cool way to decorate our maps. Your icons can be up to 64x64 pixels (which is relatively large since the largest map is 640x640) and you can have up to five different icons on each map (you can have more than 5 icons just no more than 5 different icons). To use a custom icon, you provide the Google Maps API with the fully qualified urlencoded URL of the icon file.
The code snippet shows a class that will call the Google Static Map API. A couple of examples are there to help you get started using the API.
Note the use of the urlencode(urldecode()) functions in the setter methods. The intent of this decode-encode construct is to allow the geocodes or addresses or place names to be set without regard to the state of their encoding. These values become part of the URL sent to the Google Static Map API, so they must be urlencode() strings. In my test cases this double function never caused any problems, but it is one of those "interesting" programming steps that merits a moment of thought before it is put into use, especially if your script feeds external data to this class.
by: b0lsc0tt on 2010-07-02 at 17:30:34ID: 16557
Great article and thanks for the excellent code. I have really enjoyed Google Maps and it is good to have such a useful thing pointed out and explained so well. Thanks for the contribution and time writing it!
I am curious, do you find you use the Javascript Maps version at all anymore? If so, for what? It seems the static can provide most of what I use to do with the Javascript. Did you notice a change in your pages if you changed the code from Javascript to just static?
bol