Main Content

Create Interactive Basemap Picker

Interactively change the basemap of a geographic globe by adding a drop-down menu to the figure.

First, create a program file called basemapPicker.m. Within the program file:

  • Create a geographic globe in a figure created using the uifigure function.

  • Specify a position for the menu. In this example, the values of x, y, w, and h position the menu in the upper-right corner of the figure window.

  • Specify the basemaps to include in the menu.

  • Create the menu. Use a ValueChangedFcn callback that executes when you make a selection from the menu. The callback changes the basemap using the geobasemap function.

  • Write custom code to reposition the menu when you change the size of the figure. To do this, disable automatic resizing of the menu. Then, create custom behavior by defining a SizeChangedFcn callback. The repositionDropdown function repositions the menu, so that it stays in the upper-right corner of the figure.

function basemapPicker
    uif = uifigure;
    gl = geoglobe(uif);
   
    x = 0.8;
    y = 0.9;
    w = 0.2;
    h = 0.1;
    uifW = uif.Position(3);
    uifH = uif.Position(4);
    pos = [x*uifW y*uifH w*uifW h*uifH];

    basemaps = ["satellite" "streets" "streets-light" "streets-dark" ...
                "landcover" "darkwater" "grayland" "bluegreen" ...
                "grayterrain" "colorterrain"];

    dd = uidropdown(uif,'Position',pos,'Items',basemaps);
    dd.ValueChangedFcn = @(src,eventdata)geobasemap(gl,src.Value);

    uif.AutoResizeChildren = 'off';
    uif.SizeChangedFcn = @(src,eventdata)repositionDropdown(dd,x,y,w,h);
end

function repositionDropdown(dd,x,y,w,h)
    fig = dd.Parent;
    uifW = fig.Position(3);
    uifH = fig.Position(4);
    dd.Position = [x*uifW y*uifH w*uifW h*uifH];
end

Run the program file. Change the basemap to 'colorterrain' using the drop-down menu.

See Also

| |

Related Topics