Posted in ColdFusion | Posted on 02-04-2010 | 5,713 views
I discovered something cool today - Google has a "Static" Maps API. What is that exactly? While Google Maps is very powerful, it requires the use of JavaScript. For simple maps, or for embedding maps into PDFs, you can't use regular Google Maps. This gets around it. The API is a simple URL based service that returns the image in binary form. So for example, to create a map for my area, I'd use this URL:
http://maps.google.com/maps/api/staticmap?center=Lafayette%2C%20LA&zoom=13&key=ABQIAAAAnKqaqda06cMGIKQ6i1ekrRT2yXp%5FZAY8%5FufC3CFXhHIE1NvwkxT2gB6zcbOMt6hlm0jA8TKTSu9K3g&sensor=false&size=400x400
Which produces:
The URL contains multiple arguments. You can see the address, the zoom, a size, and, yes, my Google Maps key. But since I only use it for localhost I'm not too worried about it. The docs contain the full API and demonstrate how you can even use markers and overlays on the maps.
I tried a PDF version and noticed something odd. Whenever I embedded the URL directly in the PDF, it failed. If I fetched the image and save it first, it worked fine, like in the example below:
2<cfset size = "400x400">
3<cfset loc = "Lafayette, LA">
4<cfset zoom = 13>
5<cfset img = "http://maps.google.com/maps/api/staticmap?center=#urlEncodedFormat(loc)#&zoom=#zoom#&key=#urlEncodedFormat(key)#&sensor=false&size=#size#">
6
7<cfset o = imageNew(img)>
8<cfset imageWrite(o, expandPath("./mygooglemap.png"))>
9
10<cfdocument format="pdf" name="mypdf">
11<h1>Our Store!</h1>
12
13<img src="/mygooglemap.png" align="right">
14<p>
15fjdsk lfjklsdjf jfkdk lfklsjkfl dskfd ksl fklfkl fklk lfkldsfklfk lsdfkljsdfkls
16fjdsk lfjklsdjf jfkdkl fklsjkfl dskfd kslfklfklfklk lfkldsfklfk lsdfklj sdfkls
17fjdsk lfjklsdjf jfkdklfklsjkfl dskfd ksl fklfklfklk lfkldsfklfk lsdfkljsdfkls
18fjdsk lfjklsdjf jfkdklf klsjkfl dskfd kslfklfklfklk lfkldsfklfk lsd fkljsdfkls
19fjdsk lfjklsdjf jfkdklfklsjkf l dskfd kslfklf klfklk lfkldsfklfk lsdfkljsdfkls
20</p>
21
22</cfdocument>
23
24<cfset fileWrite(expandPath("./googlemap.pdf"), mypdf)>
I even whipped up a quick UDF to make it simpler to use:
2 return "http://maps.google.com/maps/api/staticmap?center=#urlEncodedFormat(arguments.address)#&zoom=#arguments.zoom#&key=#urlEncodedFormat(arguments.key)#&sensor=false&size=#arguments.size#";
3}
Anyway, there is a lot more to the API, but for those looking for maps you can store offline, or embed in PDFs, then this looks a great resource. (Oh, and let me bitch a bit a little bit here. I really wish the main Google Maps API would let you pass in an address instead of making you have to perform a geolocation request first. It seems silly that I'd need to make 2 HTTP requests for a map when Google should let me just pass the address in the initial map setup request.)


Actually - I wonder what the UA is for image requests. I know for CFHTTP it defaults to "ColdFusion something", but it may not for imageNew(). I'll test that today.
As an FYI, I tested logging the user agent for imageNew(url), and it is Java/1.6.0_17. Obviously that would be different for your own JDK.
Podcast: http://webdu.com.au/session/feeditunes
[Add Comment] [Subscribe to Comments]