File Exchange

image thumbnail


version (7.62 KB) by Val Schmidt
Retrieve a google map from the command line using the Static Google Maps API.


Updated 25 Mar 2016

View Version History

View License

The function get_google_map.m uses the Google Maps API ( to retrieve a google map image from Google given coordinates of the center of the image and the desired zoom level (default 15). The image is returned as a raster array and a color map.
In addition, an estimate of the X and Y coordinate vectors are returned in UTM coordinates to facilitate easy plotting of additional information on the map from within MATLAB.
The desired height and width of the image (in pixels) may be specified as well as the "MapType" (one of roadmap, mobile, satellite, terrain, hybrid, mapmaker-roadmap, mapmaker-hybrid) and up to 50 Google Map markers. For each marker one may specify the location, size, color and optional label.
Default values: Zoom 15, MapType = 'satellite', Height = 640, Width = 640
One may utilize the Google Maps API without a key for a limited number of queries per day. To utilize the Google Maps API up to 25,000 queries per day, one must obtain a Google Maps API Key, freely available from Google. The key may be obtained here: (click "Get a Key")

Note, the key requirement places limitations on the portability of this function, as the key, and therefore the function is necessarily tied to a domain name and therefore (usually) a physical location. None-the-less it remains a very handy function for quickly plotting data on a map.

The following code produce the attached screenshot:

[xx yy M Mcolor] = get_google_map(43.06374,-70.703993);
imagesc(XX,YY,M); shading flat; colormap(Mcolor)
xlabel('Eastings UTM')
ylabel('Northings UTM')
title('Entrance to Portsmouth Harbor')

Known Issues:
1) The estimate of the X and Y coordinate vectors are just that, a guess at an algorithm for determining these values from the images size and zoom level. The algorithm was determined by trial and error (as the API provides no way to query for the spacial bounds of an image), and therefore will likely produce poor results for some locations. In the vicinity of Portsmouth Harbor, NH the resulting image is geo-referenced to about +/- 3m.

2) Because of MATLAB's poor rendering performance on the Mac platform, my experience has been that maps plotted as in the example above produce white gaps whose cause and resolution are unknown.

Cite As

Val Schmidt (2021). get_google_map (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (20)

Laura Johansen

I get the same error as Jules. I have added the api key in the code. What could be wrong?

Jules Ray

Not working in Matlab 2017b

Error using readContentFromWebService (line 42)
Too many output arguments.

Error in webread (line 125)
[varargout{1:nargout}] = readContentFromWebService(connection, options);

Error in get_google_map (line 182)
[M Mcolor] = webread(url);

Costa Danias

What is varargin?


R studio creates a much better rendering of the image. But what if the image is saved as a .jpg or .png?

Shanning Bao

Steven Schelling

Giuliano Langella

Good work!
I suggest to use the following modifications in order to get a well shaped map to which add other geospatial objects (such as points and so on):

1.\ proposed change in get_google_map.m: set Northing orientation as it should be
% YY = 0:dx:W;% ## <--- original
YY = W:-dx:0;% ## <--- modified

2.\ run the new function:
[XX,YY,M,Mcolor] = get_google_map( LatE,LonE,'Zoom',18);

3.\ plot using the image function as well, adding geopoints (from North -> South & West -> East) to the main map
shading flat; colormap(Mcolor)
xlabel('Eastings UTM')
ylabel('Northings UTM')
ax.YDir = 'normal';
Xpoints = XX( floor(end/4) : floor(end/4*3) );
Ypoints = YY( floor(end/4) : floor(end/4*3) );
hold on
scatter(Xpoints, Ypoints,6,'filled');
hold off
title('Now points and other geospatial objects can be properly drawn!')

Linwei Hu

It works fine. I have a short question about the parameter "M" if I load a satellite map. The return matrix M is in numbers with a range from 1-255. Do you have any idea what does these numbers mean?
Correspondingly, the "Mcolor" has 256 color levels. I am not so sure if the numbers in M corresponds to the color levels in "Mcolor".

Wei Bai

Subhraneil Das

whenever i try to run this function, it says
Maximum recursion limit of 500 reached. Use set(0,'RecursionLimit',N) to change the limit. Be aware that exceeding your
available stack space can crash MATLAB and/or your computer.

Error in get_google_map
I tried changing the recursion limits but it didnt work or rather the progra crashed for n>=799.
Its really urgent for me to get this code working. Can someone please give a solution ?

Xiangyang Zhu

how to remove the label on maps? such as name of road...

Todd Mackey

Running R2016a and getting this error:

Error using readContentFromWebService (line 42)
Too many output arguments.

Error in webread (line 122)
[varargout{1:nargout}] = readContentFromWebService(connection, options);

Error in get_google_map (line 182)
[M Mcolor] = webread(url);



Error using webread (line 112)
The IP address of "" could not be determined.

What to do in this case?

Pushkar Limaye


Thanks, this is a great function!
I was able to improve the performance by tweaking it to not write the image to a file but using the webread() function to load the image directly - Maybe you want to consider this. - Martin

Andoni Medina

Hi!, thanks to my colleague Cezar Stanciu we (mainly him) solved the issue. As handles to figures are now objects, it's required to change the axHandle to double->"double(axHandle)" each time is called in the script. regards

Andoni Medina

Hello, script gives error if used with Matlab 2014b on, due to the graphical handles change. Has any of you experienced this? thanks

brijendra kumar

i am getting following error........
please help me to fix it..................
??? Error using ==> imread at 328
Unable to determine the file format.

Error in ==> get_google_map at 163
[M Mcolor] = imread(filename);

Error in ==> Untitled2 at 3
[xx yy M Mcolor] = get_google_map(43.06374,-70.703993);


once the user has set the environment

- get the KEY
- get DEG2UTM

it works quite well for most of the prop/val combinations

two thoughts

no need to FLIPUD(M) in the function... otherwise, users have to flip it back for

axis image;

which seems a faster/better way to display the map compared to the proposed SURF(...)

the props should be case-insensitive...


MATLAB Release Compatibility
Created with R2009a
Compatible with any release
Platform Compatibility
Windows macOS Linux

Inspired by: deg2utm, zoharby/plot_google_map

Inspired: zoharby/plot_google_map

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!