Multi Search¶
Setup¶
Prerequisites¶
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 MultiSearch
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, "MultiSearch", &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.
Environment::SdkSession session(projectApiToken, { argc > 1 ? argv[1] : "" });
CTouchEventListener pTouchEventListener;
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,
"MultiSearch", &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
13 // Position around which to search for POIs
14 gem::Coordinates targetPosition1(25.607, -80.399);
15 float lon1 = targetPosition1.getLongitude(), lat1 = targetPosition1.getLatitude();
16 ImGui::PushItemWidth(128);
17 ImGui::InputFloat("Lon", &lon1);
18 ImGui::InputFloat("Lat", &lat1);
19 ImGui::PopItemWidth();
20 targetPosition1.setLatitude(lat1);
21 targetPosition1.setLongitude(lon1);
22 ImGui::PushStyleColor(ImGuiCol_Text, ImVec4(1.f, 1.f, 1.f, 1.f));
23 ImGui::PushStyleColor(ImGuiCol_Button, ImVec4(.0f, .0f, 1.f, 1.f));
24 ImGui::PushStyleColor(ImGuiCol_ButtonHovered, ImVec4(.5f, .0f, 1.f, 1.f));
25 ImGui::PushStyleColor(ImGuiCol_ButtonActive, ImVec4(1.f, .0f, 1.f, 1.f));
26 if (ImGui::Button("Search POIs around position"))
27 {
28 // Perform the search
29 gem::LandmarkList results;
30 {
31 ProgressListener searchListener;
32 // Position around which to search, given as lat,lon coordinates in degrees.
33 gem::SearchService().searchAroundPosition(results, &searchListener, targetPosition1);
34 auto ret = WAIT_UNTIL(std::bind(&ProgressListener::IsFinished, &searchListener), 15000);
35 }
36 mapView->centerOnCoordinates(results[0].getCoordinates(), 80);
37 mapView->activateHighlight(results);
38 }
39 ImGui::PopStyleColor(4);
40
41 // Position around which to search for text input below
42 gem::Coordinates targetPosition2(29.57, -95.12);
43 float lon2 = targetPosition2.getLongitude(), lat2 = targetPosition2.getLatitude();
44 ImGui::PushItemWidth(128);
45 ImGui::InputFloat("Lon", &lon2);
46 ImGui::InputFloat("Lat", &lat2);
47 ImGui::PopItemWidth();
48 targetPosition2.setLatitude(lat2);
49 targetPosition2.setLongitude(lon2);
50 // Text to search for around above position
51 static char str2[128] = "Wavecrest";
52 ImGui::InputTextWithHint("search text", "enter search text here", str2, IM_ARRAYSIZE(str2));
53 ImGui::PushStyleColor(ImGuiCol_Text, ImVec4(1.f, 1.f, 1.f, 1.f));
54 ImGui::PushStyleColor(ImGuiCol_Button, ImVec4(.0f, .0f, 1.f, 1.f));
55 ImGui::PushStyleColor(ImGuiCol_ButtonHovered, ImVec4(.5f, .0f, 1.f, 1.f));
56 ImGui::PushStyleColor(ImGuiCol_ButtonActive, ImVec4(1.f, .0f, 1.f, 1.f));
57 if (ImGui::Button("Search free text around position"))
58 {
59 // Perform the search
60 gem::LandmarkList results;
61 {
62 ProgressListener searchListener;
63 // Text to search for; position given as lat,lon coordinates in degrees
64 gem::SearchService().search(results, &searchListener, str2, targetPosition2);
65 auto ret = WAIT_UNTIL(std::bind(&ProgressListener::IsFinished, &searchListener), 15000);
66 }
67 if ( results.size() > 0 )
68 {
69 mapView->centerOnCoordinates(results[0].getCoordinates(), 85, gem::Xy(), gem::Animation(gem::AnimationLinear, gem::ProgressListener(), 2000));
70 mapView->activateHighlight(results);
71 }
72 }
73 ImGui::PopStyleColor(4);
74
75 // Rectangular area within which to search for text input below
76 // Upper-left,lower-right bounding box given as lat,lon coordinate pairs in degrees.
77 gem::RectangleGeographicArea rgaLatLon({ 34.14083, -118.12958 }, { 34.13146, -118.12187 });
78 float lon3 = rgaLatLon.getTopLeft().getLongitude(), lat3 = rgaLatLon.getTopLeft().getLatitude();
79 float lon4 = rgaLatLon.getBottomRight().getLongitude(), lat4 = rgaLatLon.getBottomRight().getLatitude();
80 ImGui::PushItemWidth(128);
81 ImGui::InputFloat("Lon Upper left", &lon3, 0, 0, "%.1f");
82 ImGui::InputFloat("Lat", &lat3, 0, 0, "%.1f");
83 ImGui::InputFloat("Lon Lower right", &lon4);
84 ImGui::InputFloat("Lat", &lat4);
85 gem::Coordinates targetPosition7(34.138, -118.124);
86 float lon7 = targetPosition7.getLongitude(), lat7 = targetPosition7.getLatitude();
87 ImGui::InputFloat("Lon search around", &lon7);
88 ImGui::InputFloat("Lat", &lat7);
89 ImGui::PopItemWidth();
90 rgaLatLon.setTopLeft(gem::Coordinates(lat3, lon3));
91 rgaLatLon.setBottomRight(gem::Coordinates(lat4, lon4));
92 targetPosition7.setLatitude(lat7);
93 targetPosition7.setLongitude(lon7);
94 // Text to search for within above rectangular area
95 static char str3[128] = "Laboratory";
96 ImGui::InputTextWithHint("search text", "enter search text here", str3, IM_ARRAYSIZE(str3));
97 ImGui::PushStyleColor(ImGuiCol_Text, ImVec4(1.f, 1.f, 1.f, 1.f));
98 ImGui::PushStyleColor(ImGuiCol_Button, ImVec4(.0f, .0f, 1.f, 1.f));
99 ImGui::PushStyleColor(ImGuiCol_ButtonHovered, ImVec4(.5f, .0f, 1.f, 1.f));
100 ImGui::PushStyleColor(ImGuiCol_ButtonActive, ImVec4(1.f, .0f, 1.f, 1.f));
101 if (ImGui::Button("Search free text within rectangular area"))
102 {
103 // Perform the search
104 gem::LandmarkList results;
105 {
106 ProgressListener searchListener;
107 // Text to search for
108 gem::SearchService().search(results, &searchListener, str3, targetPosition7, gem::SearchPreferences(), rgaLatLon);
109 auto ret = WAIT_UNTIL(std::bind(&ProgressListener::IsFinished, &searchListener), 15000);
110 }
111 if ( results.size() > 0 )
112 {
113 mapView->centerOnCoordinates(results[0].getCoordinates());
114 mapView->activateHighlight(results);
115 }
116 }
117 ImGui::PopStyleColor(4);
118 ImGui::End();
119 }
120 , std::placeholders::_1);
121}
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.
ImGui::Begin()
function.Search POIs around position¶
gem::Coordinates targetPosition1(25.607, -80.399);
gem::SearchService().searchAroundPosition(results, &searchListener, targetPosition1);
WAIT_UNTIL()
macro to wait for the search results to be returned.mapView->centerOnCoordinates(results[0].getCoordinates(), 80);
mapView->activateHighlight(results);
Search text around position¶
gem::SearchService().search(results, &searchListener, str2, targetPosition2);
WAIT_UNTIL()
macro to wait for the search results to be returned.mapView->centerOnCoordinates(results[0].getCoordinates(), 85, gem::Xy(), gem::Animation(gem::AnimationLinear, gem::ProgressListener(), 2000));
mapView->activateHighlight(results);
Search within rectangle¶
gem::RectangleGeographicArea rgaLatLon({ 34.14083, -118.12958 }, { 34.13146, -118.12187 });
gem::SearchService().search(results, &searchListener, str3, targetPosition7, gem::SearchPreferences(), rgaLatLon);
WAIT_UNTIL()
macro to wait for the search results to be returned.mapView->centerOnCoordinates(results[0].getCoordinates());
mapView->activateHighlight(results);