Skip to content

Instantly share code, notes, and snippets.

@battlejj
Created May 31, 2012 20:05
Show Gist options
  • Save battlejj/2845873 to your computer and use it in GitHub Desktop.
Save battlejj/2845873 to your computer and use it in GitHub Desktop.
Larger Static Google Maps
<cftry>
<cfscript>
//calculate the width and height of our little maps that will be combined to make our big map
miniMapWidth = calculateDimension(form.width,500);
miniMapHeight = calculateDimension(form.height,500);
//how many columns and rows of maps will we need to create our big map?
columns = form.width/miniMapWidth;
rows = form.height/miniMapHeight;
//size of the static maps we are requesting, just our minimaps widthxheight
size = miniMapWidth & "x" & miniMapHeight;
//the longitude where our first map should be centered
//take our original maps center and pull our map west by 1/2 the columns width
//then push it back east half of 1 map to get the appropriate center...confused yet?
startingLng = form.mapCenterLng -
((miniMapWidth * form.LngPerPx)*(columns/2)) +
((miniMapWidth * form.LngPerPx)/2);
//essentially the same thing as above but with longitude, north and south :P
startingLat = form.mapCenterLat +
((miniMapHeight * form.LatPerPx)*(rows/2)) -
((miniMapHeight * form.LatPerPx)/2);
//create the blank image we are going to add all our maps to
bufferedImage = createObject("java", "java.awt.image.BufferedImage");
bufferedImage.init(JavaCast("int", form.width), JavaCast("int", form.height), BufferedImage.TYPE_4BYTE_ABGR);
mymap = imageNew(bufferedImage);
for(row = 1; row <= rows; row++){
//the x and y positions we will be writing to on our image
xPos = 0;
yPos = (row - 1) * miniMapHeight;
//the current latitude for the row we are rendering
currentLat = startingLat - ((row - 1)*(miniMapHeight * form.LatPerPx));
for(column = 1; column <= columns; column++){
//update our x position in our image every time we move to the next column
xPos = (column-1) * miniMapWidth;
//calculate the current longitude for the column we are rendering
currentLng = startingLng +
(column - 1) *
(miniMapWidth * form.LngPerPx);
//the URL of the static map we want
currentMapURL = "http://maps.google.com/maps/api/staticmap?center=#currentLat#,#currentLng#&zoom=#form.zoomlevel#&sensor=false&size=#size#";
//read the static map image from google
currentMap = imageNew(currentMapURL);
//paste that image into our big image
imagePaste(mymap, currentMap, xPos, yPos);
}
}
</cfscript>
<cfimage action="writeToBrowser" source="#mymap#">
<!--- function to just calculate the best width and height of the minimaps we will be generating --->
<cffunction name="calculateDimension">
<cfargument name="dimension">
<cfargument name="maxDimension">
<cfset var returnDimension = "">
<cfif arguments.dimension LTE arguments.maxDimension>
<cfset returnDimension = arguments.dimension>
<cfelseif arguments.dimension GT arguments.maxDimension>
<cfset returnDimension = calculateDimension(arguments.dimension/2, arguments.maxDimension)>
</cfif>
<cfreturn ceiling(returnDimension)>
</cffunction>
<cfcatch type="any">
<h2>Looks like google probably rate limited the map requests! That or you tried to put in bad information...but you wouldn't do that...right?</h2>
</cfcatch>
</cftry>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment