**0**

# Static Maps API Image size

###
#1
Members - Reputation: **292**

Posted 14 December 2012 - 07:28 AM

I'm using Google maps static API and I need to know how to get the world-size dimensions of the returned image.

I've used the formula they give in their online documentation here: https://developers.google.com/maps/documentation/staticmaps/ but the values returned are roughly 2.5x what they should be.

the formula is:

resolution = 256 * pow(2, zoom_level)

I then put this over the size of the world circumference (i.e. 40,075km) and then multiple by the resolution of the returned image.

size = (40,075,000 / (256 * pow(2, zoom_level)) * image_res

In my example the zoom level is 15 and image res is 1024. The value returned is 4891.96.. but it should really be closer to 1750,00 (judging by the features in the image).

Any ideas?

Thanks.

###
#2
Crossbones+ - Reputation: **3085**

Posted 17 December 2012 - 12:45 AM

I'd like to help with even a basic sanity test to know if the calculations were correct, but I can't seem to find the formula in their documentation in the time that I have. Perhaps you could provide a more specific link to that heading, or quote it for us?Hi there,

I'm using Google maps static API and I need to know how to get the world-size dimensions of the returned image.

I've used the formula they give in their online documentation here: https://developers.g...ion/staticmaps/ but the values returned are roughly 2.5x what they should be.

the formula is:

resolution = 256 * pow(2, zoom_level)

I then put this over the size of the world circumference (i.e. 40,075km) and then multiple by the resolution of the returned image.

size = (40,075,000 / (256 * pow(2, zoom_level)) * image_res

In my example the zoom level is 15 and image res is 1024. The value returned is 4891.96.. but it should really be closer to 1750,00 (judging by the features in the image).

Any ideas?

Thanks.

###
#3
Members - Reputation: **292**

Posted 17 December 2012 - 03:49 AM

###
#4
Crossbones+ - Reputation: **3085**

Posted 18 December 2012 - 09:18 PM

Basically, this says that the pixel coordinate is the world coordinate times two to the power indicated by the zoom level.Pixel Coordinates

World coordinates reflect absolute locations on a given projection, but we need to translate these into pixel coordinates to determine the "pixel" offset at a given zoom level. These pixel coordinates are calculated using the following formula:

pixelCoordinate = worldCoordinate * 2^zoomLevel

From the above equation, note that each increasing zoom level is twice as large in both the x and y directions. Therefore, each higher zoom level contains four times as much resolution as the preceding level. For example, at zoom level 1, the map consists of 4 256x256 pixels tiles, resulting in a pixel space from 512x512. At zoom level 19, each x and y pixel on the map can be referenced using a value between 0 and 256 * 2^19

Because we based world coordinates on the map's tile size, a pixel coordinates' integer part has the effect of identifying the exact pixel at that location in the current zoom level. Note that for zoom level 0, the pixel coordinates are equal to the world coordinates.

Later, it says:

Note that by dividing the pixel coordinates by the tile size and taking the integer parts of the result, you produce as a by-product the tile coordinate at the current zoom level.

So, say that you want to get which tile and offset the center of the world is (128, 128) at zoom level 15 (tile size 1024).

tileX = (worldX / 256) * pow(2, zoomLevel) / tileSize

tileY = (worldY / 256) * pow(2, zoomLevel) / tileSize

To get the pixel offset into that tile:

offsetX = (worldX / 256) * pow(2, zoomLevel) % tileSize

offsetY = (worldY / 256) * pow(2, zoomLevel) % tileSize

Now, I assume you are trying to get the scale of the image in physical units, or something of the like.

You could rewrite your formula a different way, to think of it from another angle.

size = (40,075,000 / pow(2, zoomLevel)) * (tileSize / 256)

Dividing the base tile's real length by the number of tiles, then converting from the base tile's scale factor (256) to yours (tileSize). Doing this calculation results in 4,891.96... which seems to make perfect sense, and I can find nothing wrong with it.

So, my other guess is that your estimation of what the distance should be is incorrect. How are you figuring it out? Are there any other applications that you can use to test it out? For starters, try using Google Maps, zoom in to level 15, ask for directions starting from one edge of the tile to the other, and check the total distance shown in the directions (try to use a highway to minimize curvature of the road). If it is closer to 4000km, then there's no problem.

Additionally, what decides the tile size?

###
#5
Members - Reputation: **292**

Posted 20 December 2012 - 03:52 AM

The problem I'm having is that I need to request a Google/Static map which is directly adjacent to another one previously requested (so that the pixels align). I specify world coordinates in Cartesian (UTM) and I then inverse project these to get their longitude and latitude values. These are then sent to Google maps to retrieve the image. Using the formula in their documentation does not return the correct size apparently. I tried manually scaling the size parameter which meant that the tiles were spaced further apart and therefore the longitude and latitude were also, but this does not show any signs of converging to a correct alignment which makes me believe that this may be something to do with the conversion between UTM and the Mercaror projection that Google maps uses.

Can someone confirm that the coordinate system of the UTM and Mercator are the same? (after projection)

If anyone could provide some useful links to relevant material or come up with some tests I could run I would be most grateful.

Thanks.

###
#6
Members - Reputation: **292**

Posted 20 December 2012 - 12:05 PM

But I now have another problem... apparently UTM uses an ellipsoid model of the earth as opposed to a spherical model that Mercator uses which means that northing values are offset by a specific amount now apparently 200+ meters.

###
#7
Crossbones+ - Reputation: **3085**

Posted 20 December 2012 - 12:15 PM

Yeah, that would do it. It says:Ok, the problem was that I was converting the easting and northing values using a UTM projection, whereas Google maps uses traditional Mercator projection. This is what was causing the misalignment. I changed it to use Mercator and almost everything works now, the equation I stated above calculates correct tile sizes.

But I now have another problem... apparently UTM uses an ellipsoid model of the earth as opposed to a spherical model that Mercator uses which means that northing values are offset by a specific amount now apparently 200+ meters.

So, this may mean that it cuts off the top and bottom 85 degrees to make the map square, which would account for the vertical offset. I might be reading that wrong, but either way, I'm pretty sure that this paragraph explains the phenomenon.Note that a Mercator projection has a finite width longitudinally but an infinite height latitudinally. We "cut off" base map imagery utilizing the Mercator projection at approximately +/- 85 degrees to make the resulting map shape square, which allows easier logic for tile selection. Note that a projection may produce world coordinates outside the base map's usable coordinate space if you plot very near the poles, for example.

###
#8
Members - Reputation: **292**

Posted 20 December 2012 - 01:45 PM

###
#9
Crossbones+ - Reputation: **3085**

Posted 20 December 2012 - 06:45 PM

This site has a form that allows you to convert from UTM to coordinates usable with Google Maps. The page appears to be implemented in JavaScript, so perhaps you could read through its source to learn how it does it?