Documentation

This is machine translation

Translated by
Mouseover text to see original. Click the button below to return to the English version of the page.

Creating an Interactive Map for Selecting Point Features

This example shows how to construct a map of major world cities enhanced with coastlines and terrain. It uses the modified azimuthal Briesemeister map projection. The example includes some optional code that allows a user to interactively pick a location and get the name and location of the nearest city. To see this part of the example, you must run the complete example, pop-out the last illustration into a separate MATLAB figure, and then run the optional code at the MATLAB command line.

Step 1: Set up a Map Axes Object and Render a Global Elevation Grid

Construct the axes.

```figure axesm bries text(.8, -1.8, 'Briesemeister projection') framem('FLineWidth',1)```

Load and display a 1-by-1-degree elevation grid.

```load topo geoshow(topo, topolegend, 'DisplayType', 'texturemap')```

Step 2: Improve the Terrain Display

Get a colormap appropriate for elevation.

`demcmap(topo)`

Make it brighter.

`brighten(.5)`

`load coastlines`

Generalize the coastlines to 0.25-degree tolerance.

`[rlat, rlon] = reducem(coastlat,coastlon, 0.25);`

Plot the coastlines in brown.

`geoshow(rlat, rlon, 'Color', [.6 .5 .2], 'LineWidth', 1.5)`

Step 4: Plot City Locations with Red Point Markers

Read a shapefile containing names of cities worldwide and their coordinates in latitude and longitude.

`cities = shaperead('worldcities', 'UseGeoCoords', true);`

Extract the point latitudes and longitudes with `extractfield`, and add them to the map.

```lats = extractfield(cities,'Lat'); lons = extractfield(cities,'Lon'); geoshow(lats, lons,... 'DisplayType', 'point',... 'Marker', 'o',... 'MarkerEdgeColor', 'r',... 'MarkerFaceColor', 'r',... 'MarkerSize', 3) text(-2.8,-1.8,'Major World Cities')```

Step 5: Select Cities Interactively (Optional)

Now, using the map you've created, you can set up a simple loop to prompt for clicks on the map and display the name and coordinates of the nearest city. You must pop the last map you created in Step 4 into a separate MATLAB figure window, using the button that appears at the top of the map. Also, in the following code, set `runCitySelectionLoop` to `true`, and execute the code at the command line.

The code first displays text instructions at the upper left of the map. Then, it enters a loop in which it captures selected latitudes and longitudes with `inputm`. Use `distance` to calculate the great circle distance between each selected point and every city in the database. Determine index of the closest city, change the appearance of its marker symbol, and display the city's name and latitude/longitude coordinates.

```runCitySelectionLoop = false; % Set to true to run optional city selection loop if(runCitySelectionLoop) h1 = text(-2.8, 1.9, 'Click on a dot for its city name. Press ENTER to stop'); h2 = text(-2.8, 1.7, ''); h3 = text(-2.8, 1.5, 'City Coordinates.'); while true [selected_lat,selected_lon] = inputm(1); if isempty(selected_lat) break % User typed ENTER end d = distance(lats, lons, selected_lat, selected_lon); k = find(d == min(d(:)),1); city = cities(k); geoshow(city.Lat, city.Lon, ... 'DisplayType', 'point', ... 'Marker', 'o', ... 'MarkerEdgeColor', 'k', ... 'MarkerFaceColor', 'y', ... 'MarkerSize', 3) h2.String = city.Name; h3.String = num2str([city.Lat, city.Lon],'%10.2f'); end disp('End of input.') end```