Skip to content

Markers

In this guide you will learn how to draw point, polyline and polygon markers on an interactive map, as well as how to remove them.
Graphic control interface using ImGui.

Markers - cpp example screenshot

Setup

First, get an API key token, see the Getting Started guide.

Prerequisites

It is required that you complete the Environment Setup - CPP Examples guide before starting this guide.

Setting your API key token

To set your API key token, define it like this:

#define API_TOKEN "YOUR_API_KEY_TOKEN"

replacing YOUR_API_KEY_TOKEN with your actual API key token text, within the quotes.

This can be done in the main() function, before the following line:

#if defined(API_TOKEN)

or outside the main() function, further up, or in a header file.

Although it is also possible to define your API key token as the text value of the GEM_TOKEN environment variable, this is not recommended, as it is not secure.

Build and run

In Visual Studio, right-click on the Markers project, and select Set as Startup Project then press F5 to run.

How it works

 1int main( int argc, char** argv )
 2{
 3   std::string projectApiToken = "";
 4#define API_TOKEN "YOUR_API_KEY_TOKEN"
 5#if defined(API_TOKEN)
 6   projectApiToken = std::string( API_TOKEN );
 7#endif
 8   // Sdk objects can be created & used below this line
 9   Environment::SdkSession session(projectApiToken, { argc > 1 ? argv[1] : "" });
10
11   // Create an interactive map view
12   CTouchEventListener pTouchEventListener;
13   gem::StrongPointer<gem::MapView> mapView = gem::MapView::produce(session.produceOpenGLContext(
14      Environment::WindowFrameworks::ImGUI, "Markers", &pTouchEventListener, getUiRender()));
15   if ( !mapView )
16   {
17      GEM_LOGE( "Error creating gem::MapView: %d", GEM_GET_API_ERROR() );
18   }
19   WAIT_UNTIL_WINDOW_CLOSE();
20   return 0;
21}

First, the API key token is set.

Next, the SDK environment is initialized, passing in the API key token string, and the SDK API debug logging path.
Environment::SdkSession session(projectApiToken, { argc > 1 ? argv[1] : "" });
The debug logging path is set from the first command line argument, if there is one, otherwise it can be given as a hardcoded string.
A touch event listener is instantiated, to support touch input for the interactive map, such as pan and zoom.
CTouchEventListener pTouchEventListener;
The MapView interactive map instance is created, using an OpenGL context for rendering, and selecting ImGui for the graphic control interface.
gem::StrongPointer<gem::MapView> mapView = gem::MapView::produce(session.produceOpenGLContext(Environment::WindowFrameworks::ImGUI,
The window title is specified, and the touch event listener instanced is passed in.
"Markers", &pTouchEventListener, getUiRender()));

Finally, the getUiRender() function is passed in, which uses ImGui to render GUI elements, capture user control input, and call the appropriate SDK functions.

 1auto getUiRender()
 2{
 3   return std::bind([](gem::StrongPointer<gem::MapView> mapView)
 4   {
 5      ImGuiIO& io = ImGui::GetIO();
 6      const ImGuiViewport* main_viewport = ImGui::GetMainViewport();
 7      ImGui::SetNextWindowPos(ImVec2(main_viewport->WorkPos.x + 0, main_viewport->WorkPos.y + 20), ImGuiCond_FirstUseEver);
 8      ImGui::Begin("panel", nullptr, ImGuiWindowFlags_NoMove
 9          | ImGuiWindowFlags_NoDecoration
10          | ImGuiWindowFlags_AlwaysAutoResize
11          | ImGuiWindowFlags_NoSavedSettings);
12      if (ImGui::Button("Add point markers"))
13      {
14          auto col = gem::MarkerCollection(gem::EMarkerType::MT_Point, "point");
15          col.add({ 37.78301592799968, -122.44509746977026 });
16          col.add({ 37.744870145184954, -122.47291375685005 });
17          col.add({ 37.73182234501792, -122.39309744523473 });
18          mapView->preferences().markers().add(col);
19          mapView->centerOnArea(col.getArea());
20      }
21      if (ImGui::Button("Add polygon markers"))
22      {
23          auto col = gem::MarkerCollection(gem::EMarkerType::MT_Polygon, "polygon");
24          col.add(gem::Marker({
25              { 37.78301592799968, -122.44509746977026 },
26              { 37.744870145184954, -122.47291375685005 },
27              { 37.73182234501792, -122.39309744523473 }
28              }));
29          col.add(gem::Marker({
30              { 37.7727019264254, -122.42707148907742 },
31              { 37.76671282078619, -122.39085098046263 },
32              { 37.75083649188404, -122.41110088290034 }
33              }));
34          auto styleList = gem::ContentStore().getLocalContentList(gem::EContentType::CT_ViewStyleLowRes);
35          if (styleList.size() > 0)
36          {
37              mapView->preferences().setMapStyle(styleList.at(0));
38          }
39          gem::MarkerCollectionRenderSettings markerCollDisplaySettings;
40          markerCollDisplaySettings.polygonFillColor = gem::Rgba(0, 100, 100, 100);
41          mapView->preferences().markers().add(col, markerCollDisplaySettings);
42          mapView->centerOnArea(col.getArea());
43      }
44      if (ImGui::Button("Add polyline markers"))
45      {
46          auto col = gem::MarkerCollection(gem::EMarkerType::MT_Polyline, "polyline");
47          col.add(gem::Marker({
48              { 37.78301592799968, -122.44509746977026 },
49              { 37.744870145184954, -122.47291375685005 },
50              { 37.73182234501792, -122.39309744523473 }
51              }));
52
53          col.add(gem::Marker({
54              { 37.7727019264254, -122.42707148907742 },
55              { 37.76671282078619, -122.39085098046263 },
56              { 37.74622717625295, -122.41815611350229 }
57              }));
58          mapView->preferences().markers().add(col);
59          mapView->centerOnArea(col.getArea());
60      }
61      if (ImGui::Button("Remove all markers"))
62      {
63          mapView->preferences().markers().clear();
64      }
65      ImGui::End();
66   }
67   , std::placeholders::_1);
68}

First, the main ImGui viewport is obtained, and the x,y position of the ImGui window, with the identifier “panel”, in pixels, is set within the SDK OpenGL viewport.

The ImGui graphical control elements are within the ImGui window/panel, rendered on top of the SDK OpenGL viewport, which contains the interactive map. The ImGui panel is created using the ImGui::Begin() function.
The first three control elements in this case, are ImGui::Button() which the user can click to render point markers, polygon markers, or polyline markers, respectively, on the interactive map.
This is done by first creating a marker collection of the appropiate type, and then adding latitude,longitude coordinate pairs to it:
auto col = gem::MarkerCollection(gem::EMarkerType::MT_Point, "point");
col.add({ 37.78301592799968, -122.44509746977026 });
A point marker requires only one coordinate pair, while a polyline requires at least 2 (or more), and a polygon requires at least 3 (or more).
The marker collection is then added to the map, and finally, the view is centered on the area containing all the markers, such that they all fit in the viewport:
mapView->preferences().markers().add(col);
mapView->centerOnArea(col.getArea());
The 4th button is to remove all markers from the map:
mapView->preferences().markers().clear();

C++ Examples

Maps SDK for C++ Examples can be downloaded or cloned with Git