OpenRoadCode
Public API reference for OpenRoadCode
Loading...
Searching...
No Matches
map_view.hpp
1// SPDX-FileCopyrightText: 2026 Mark G. Russell
2// SPDX-License-Identifier: MIT
3
4#pragma once
5
6#include "map_feature_hover.hpp"
7#include <mbgl/map/map.hpp>
8#include <mbgl/util/run_loop.hpp>
9#include <mbgl/util/timer.hpp>
10
11#include <cstddef>
12#include <functional>
13#include <memory>
14#include <string>
15#include <vector>
16
17struct GLFWwindow;
18class GLFWBackend;
20
21namespace mbgl { namespace gfx { class RendererBackend; } }
22
23struct CachedPoiResult {
24 std::string id;
25 std::string name;
26 std::string category;
27 std::string brand;
28 std::string sourceClass;
29 std::string sourceSubclass;
30 double latitude = 0.0;
31 double longitude = 0.0;
32};
33
34struct InteractivePoiMarker {
35 std::size_t index = 0;
36 std::string id;
37 double left = 0.0;
38 double top = 0.0;
39 double right = 0.0;
40 double bottom = 0.0;
41
42 bool contains(double x, double y) const {
43 return x >= left && x <= right && y >= top && y <= bottom;
44 }
45};
46
47struct PoiSearchResult {
48 int count = 0;
49 double south = 0.0;
50 double west = 0.0;
51 double north = 0.0;
52 double east = 0.0;
53};
54
55class MapView : public mbgl::MapObserver {
56public:
57 using ManualCameraCallback = std::function<void()>;
58 using MapClickCallback = std::function<void(
59 double, double, double, const std::string&, std::size_t)>;
60
61 using PoiSelectedCallback = std::function<void(
62 const std::string& name,
63 const std::string& brand,
64 const std::string& sourceClass,
65 const std::string& sourceSubclass,
66 double latitude,
67 double longitude)>;
68
69 MapView(const mbgl::ResourceOptions& resourceOptions, const mbgl::ClientOptions& clientOptions);
70 ~MapView() override;
71 float getPixelRatio() const;
72 mbgl::Size getSize() const;
73 mbgl::gfx::RendererBackend& getRendererBackend();
74 void setMap(mbgl::Map* map);
75 void setRendererFrontend(MapRendererFrontend* rendererFrontend);
76 void run();
77 void invalidate();
78 void showWindow();
79 void finishCameraGesture();
80 void setShouldClose();
81 void onWillStartRenderingFrame() override;
82
84 void onDidFailLoadingMap(mbgl::MapLoadError error, const std::string& message) override;
85
87 void onTileAction(
88 mbgl::TileOperation operation,
89 const mbgl::OverscaledTileID& tileId,
90 const std::string& sourceId
91 ) override;
92
97 void setUpdateCallback(std::function<void()> callback);
98 void setPoiSelectedCallback(PoiSelectedCallback callback);
99 void setManualCameraCallback(ManualCameraCallback callback);
100 void setMapClickCallback(MapClickCallback callback);
101 void setPoiResultsJson(const std::string& geojson);
102 void setCityWeatherJson(const std::string& geojson);
104 std::string searchWeatherCities() const;
105 PoiSearchResult searchVisiblePois(const std::string& category) const;
106
107private:
108 static void onWindowResize(GLFWwindow* window, int width, int height);
109 static void onFramebufferResize(GLFWwindow* window, int width, int height);
110 static void onScroll(GLFWwindow* window, double xOffset, double yOffset);
111 static void onWindowFocus(GLFWwindow* window, int focused);
112 static void onMouseClick(GLFWwindow* window, int button, int action, int modifiers);
113 static void onMouseMove(GLFWwindow* window, double x, double y);
114 static void onCursorEnter(GLFWwindow* window, int entered);
115 void updateMapHover();
116 void publishCityWeatherHover();
117 void publishPoiHover();
118 void clearMapHover();
119 std::vector<InteractivePoiMarker> interactivePoiMarkers() const;
120 void render();
121
122 mbgl::Map* map = nullptr;
123 MapRendererFrontend* rendererFrontend = nullptr;
124 std::unique_ptr<GLFWBackend> backend;
125 GLFWwindow* window = nullptr;
126 int width = 1024;
127 int height = 600;
128 float pixelRatio = 1.0f;
129 double lastX = 0.0;
130 double lastY = 0.0;
131 double pressX = 0.0;
132 double pressY = 0.0;
133 double lastClick = -1.0;
134 bool tracking = false;
135 bool manualGesturePublished = false;
136 bool dirty = false;
137 mbgl::util::RunLoop runLoop;
138 mbgl::util::Timer frameTick;
139 std::function<void()> updateCallback;
140 PoiSelectedCallback poiSelectedCallback;
141 ManualCameraCallback manualCameraCallback;
142 MapClickCallback mapClickCallback;
143 std::vector<CachedPoiResult> poiResults;
144 MapFeatureHover cityWeatherHover;
145 MapFeatureHover poiHover{"id", "", "poi_hover"};
146 bool pointerInside = false;
147 double lastHoverCheck = -1.0;
148};
Connects MapLibre rendering updates to a MapView window.
Definition map_renderer_frontend.hpp:29