Main Content

Create 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

Create a map axes object.

figure 
axesm bries
text(2.8,-1.8,'Briesemeister projection','HorizontalAlignment','right')
framem('FLineWidth',1)

Load elevation raster data and a geographic cells reference object. Display the data on the map.

load topo60c
geoshow(topo60c,topo60cR,'DisplayType','texturemap')

Step 2: Improve the Terrain Display

Apply a colormap appropriate for elevation data. Make the display brighter.

demcmap(topo60c)
brighten(0.5)

Step 3: Add Simplified Coastlines

Load global coastline coordinates. Generalize the coastlines to 0.25-degree tolerance. Then, plot the coastlines in brown.

load coastlines
[rlat,rlon] = reducem(coastlat,coastlon,0.25);
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

See Also

| | |