Main Content

Add Interactive Controls to Geographic Axes

Typically, you control the appearance of geographic axes by using default pan and zoom interactions, by using functions such as geobasemap and geolimits, and by changing the properties of GeographicAxes objects.

You can also control the appearance of geographic axes by adding interactive controls. This topic shows how to create a geographic axes object that contains a basemap picker, a zoom slider, and a context menu that displays and copies the coordinates of the pointer.

For information about creating apps with similar interactive controls in App Designer, see Create Geographic Axes in App.

Add Basemap Picker to Geographic Axes

Add a basemap picker to the axes toolbar of a geographic axes object by using the addToolbarMapButton function.

Display the locations of European capitals on a map. When there is no current axes, the geoiconchart function plots the data by creating a new geographic axes object.

[lat,lon] = readvars("european_capitals.txt");

figure
geoiconchart(lat,lon)
geolimits([37.3 54.6],[-8.9 31.2])

Preserve the toolbar, limits, and plotted data by using the hold function.

hold on

Create a custom toolbar by using the axtoolbar function. Add the basemap picker to the toolbar.

tb = axtoolbar("default");
addToolbarMapButton(tb,"basemap")

Figure contains an axes object with type geoaxes. The geoaxes object contains an object of type iconchart.

Open the basemap picker and select a basemap.

A geographic axes object that uses the land cover basemap. A basemap picker shows additional basemap options.

By default, the addToolbarMapButton function adds all available basemaps to the basemap picker. For information about customizing the appearance of the basemap picker, see the addToolbarMapButton page.

Add Zoom Slider to Geographic Axes

Add a zoom slider to a geographic axes object by using a slider component. Create the slider component by using the uislider function.

Sliders created with uislider are typically used for app development, so create a figure by using the uifigure function. Specify the width of the figure as 550 pixels and the height of the figure as 350 pixels. Then, add a geographic axes object to the figure.

uif = uifigure;
uif.Position(3:4) = [550 350];

gx = geoaxes(uif);

Figure contains an axes object with type geoaxes. The geoaxes object is empty.

Add a slider component to the figure. Customize the slider by changing properties of the Slider object.

  • Position the slider on the right side of the map.

  • Use a vertical orientation instead of the default horizontal orientation.

  • Set the value of the slider by querying the zoom level of the map.

  • Set the range of the slider to [0, 18].

  • Program the behavior of the zoom slider using an anonymous callback function. For more information about callbacks, see Create Callbacks for Apps Created Programmatically.

sld = uislider(uif);

sld.Position(1:2) = [510 100];
sld.Orientation = "vertical";
sld.Value = gx.ZoomLevel;
sld.Limits = [0 18];
sld.ValueChangedFcn = @(src,event)set(gx,"ZoomLevel",event.Value);

Display a marker at the address of the Eiffel Tower.

GT = geocode("Av. Gustave Eiffel, 75007 Paris, France","address");
geoiconchart(gx,GT.Shape.Latitude,GT.Shape.Longitude)

Figure contains an axes object and an object of type uislider. The geoaxes object contains an object of type iconchart.

Zoom in by moving the slider thumb. As you move the thumb, the zoom level of the map changes to reflect the slider value.

The same geographic axes. The map shows an area surrounding the Eiffel Tower. The slider thumb points to zoom level 16.

For more information about creating and customizing slider components, see uislider and Slider.

Get Coordinates of Pointer

Display the coordinates of the pointer, and copy the coordinates to the clipboard by using a context menu.

Context menus are typically used for app development, so create a figure by using the uifigure function. Then, add a geographic axes object to the figure.

uif = uifigure;
gx = geoaxes(uif);

Create a context menu with one menu item. Define the behavior of the context menu and menu item using callback functions. For more information about callback functions, see Create Callbacks for Apps Created Programmatically.

  • Add the context menu to the geographic axes object. Specify a ContextMenuOpeningFcn callback function using the updateCoordinateDisplay local function. When you right-click the map, the function displays the coordinates of the pointer.

  • Add the menu item to the context menu. Specify a MenuSelectedFcn callback function using the copyCoordinatesToClipboard local function. When you click the menu item, the function copies the coordinates to the clipboard.

gx.ContextMenu = uicontextmenu(uif, ...
    ContextMenuOpeningFcn = @(src,event)updateCoordinateDisplay(gx,event));

m = uimenu(gx.ContextMenu, ...
    MenuSelectedFcn = @(src,event)copyCoordinatesToClipboard(gx));

Figure contains an axes object with type geoaxes. The geoaxes object is empty.

Display the coordinates of the pointer by right-clicking the map. Copy the coordinates to the clipboard by clicking the menu item.

The same geographic axes object. A context menu displays the coordinates of the pointer.

For more information about creating context menus and menu items, see uicontextmenu and uimenu.

Local Functions

Define the updateCoordinateDisplay and copyCoordinatesToClipboard functions, used as callback functions in this example. Both functions use the CurrentPoint property of the input geographic axes gx to get the geographic coordinates of the pointer. The updateCoordinateDisplay function uses the event.Source.Children property to access the menu item stored in the context menu.

function updateCoordinateDisplay(gx,event)
    pt = gx.CurrentPoint;
    coordinateString = "(" + pt(1,1) + ", " + pt(1,2) + ")";
    set(event.Source.Children,Text=coordinateString)
end

function copyCoordinatesToClipboard(gx)
    pt = gx.CurrentPoint;
    coordinateString = "(" + pt(1,1) + ", " + pt(1,2) + ")";
    clipboard("copy",coordinateString)
end

See Also

Functions

Properties

Topics