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")
Open the basemap picker and select a basemap.
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);
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)
Zoom in by moving the slider thumb. As you move the thumb, the zoom level of the map changes to reflect the slider value.
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 theupdateCoordinateDisplay
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 thecopyCoordinatesToClipboard
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));
Display the coordinates of the pointer by right-clicking the map. Copy the coordinates to the clipboard by clicking the menu item.
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