10
votes

I know that Google Maps provides an API for getting static images:

https://developers.google.com/maps/documentation/static-maps/intro

However that API gets as input a center point, zoom level and output image size.

In my case, I need an image that fits a given area taking as input the NW (top-left) and SE (bottom right) coordinates describing the rectangle of my area. I don't see that option within Google Maps API, so I'm wondering if there is another way to accomplish this.

1

1 Answers

13
votes

You can omit the center and zoom parameters if you specify at least one marker. In this case you can show a certain area with a marker.

For example, I have bounds of Barcelona in Spain:

41.320004,2.069526 and 41.469576,2.22801

I can easily calculate the center position of the bounds:

41.39479,2.148768

Now, let's put a marker in the center of bounds and use the visible parameter of Static Maps API to specify NW and SE.

visible (optional) specifies one or more locations that should remain visible on the map, though no markers or other indicators will be displayed. Use this parameter to ensure that certain features or map locations are shown on the Google Static Maps API.

https://maps.googleapis.com/maps/api/staticmap?size=600x400&markers=icon%3Ahttp%3A%2F%2Fwww.google.com%2Fmapfiles%2Farrow.png%7C41.39479%2C2.148768&visible=41.320004%2C2.069526%7C41.469576%2C2.22801&key=api_key

Code snippet

<img src="https://maps.googleapis.com/maps/api/staticmap?size=600x400&markers=icon%3Ahttp%3A%2F%2Fwww.google.com%2Fmapfiles%2Farrow.png%7C41.39479%2C2.148768&visible=41.320004%2C2.069526%7C41.469576%2C2.22801&key=AIzaSyDztlrk_3CnzGHo7CFvLFqE_2bUKEq1JEU&signature=WkICJTRmmI1EgDK2VJI4l9lt3qA=" title="" />

As a result, you have an image with specified area. I think you can even go further if you create a transparent png file for marker icon.

Hope it helps!