diff options
author | Thiago Marcos P. Santos <thiago@mapbox.com> | 2015-06-17 15:02:29 +0300 |
---|---|---|
committer | John Firebaugh <john.firebaugh@gmail.com> | 2015-06-17 17:07:16 -0700 |
commit | 4f3fc3c2c975e1d02f3cdd2041e413c9bc159cb4 (patch) | |
tree | 7ac07027c26a782211cda58983fdb6d5243d2cb0 | |
parent | 4806f7f0a55aad2f3ac86f480bac394c0cb1eacf (diff) | |
download | qtlocation-mapboxgl-4f3fc3c2c975e1d02f3cdd2041e413c9bc159cb4.tar.gz |
Support shape annotations on GLFWView
Add different random triangles count by using the keys 7, 8, 9 or 0.
Q will clear all the annotations.
W will remove one annotation.
-rw-r--r-- | include/mbgl/platform/default/glfw_view.hpp | 8 | ||||
-rw-r--r-- | platform/default/glfw_view.cpp | 73 |
2 files changed, 74 insertions, 7 deletions
diff --git a/include/mbgl/platform/default/glfw_view.hpp b/include/mbgl/platform/default/glfw_view.hpp index 094974b59c..9a7e29fe29 100644 --- a/include/mbgl/platform/default/glfw_view.hpp +++ b/include/mbgl/platform/default/glfw_view.hpp @@ -37,7 +37,15 @@ public: void fps(); private: + mbgl::LatLng makeRandomPoint() const; + void addRandomPointAnnotations(int count); + void addRandomShapeAnnotations(int count); + + void clearAnnotations(); + void popAnnotation(); + + mbgl::AnnotationIDs annotationIDs; private: bool fullscreen = false; diff --git a/platform/default/glfw_view.cpp b/platform/default/glfw_view.cpp index 0f063e5925..61d553ae3d 100644 --- a/platform/default/glfw_view.cpp +++ b/platform/default/glfw_view.cpp @@ -114,37 +114,96 @@ void GLFWView::onKey(GLFWwindow *window, int key, int /*scancode*/, int action, if (!mods) view->map->resetNorth(); break; + case GLFW_KEY_Q: + view->clearAnnotations(); + break; } } if (action == GLFW_RELEASE || action == GLFW_REPEAT) { switch (key) { + case GLFW_KEY_W: view->popAnnotation(); break; case GLFW_KEY_1: view->addRandomPointAnnotations(1); break; case GLFW_KEY_2: view->addRandomPointAnnotations(10); break; case GLFW_KEY_3: view->addRandomPointAnnotations(100); break; case GLFW_KEY_4: view->addRandomPointAnnotations(1000); break; case GLFW_KEY_5: view->addRandomPointAnnotations(10000); break; case GLFW_KEY_6: view->addRandomPointAnnotations(100000); break; + case GLFW_KEY_7: view->addRandomShapeAnnotations(1); break; + case GLFW_KEY_8: view->addRandomShapeAnnotations(10); break; + case GLFW_KEY_9: view->addRandomShapeAnnotations(100); break; + case GLFW_KEY_0: view->addRandomShapeAnnotations(1000); break; } } } +mbgl::LatLng GLFWView::makeRandomPoint() const { + const auto sw = map->latLngForPixel({ 0, 0 }); + const auto ne = map->latLngForPixel({ double(width), double(height) }); + + const double lon = sw.longitude + (ne.longitude - sw.longitude) * (double(std::rand()) / RAND_MAX); + const double lat = sw.latitude + (ne.latitude - sw.latitude) * (double(std::rand()) / RAND_MAX); + + return { lat, lon }; +} + + void GLFWView::addRandomPointAnnotations(int count) { std::vector<mbgl::LatLng> points; std::vector<std::string> markers; - const auto sw = map->latLngForPixel({ 0, 0 }); - const auto ne = map->latLngForPixel({ double(width), double(height) }); + for (int i = 0; i < count; i++) { + points.push_back(makeRandomPoint()); + markers.push_back("default_marker"); + } + + auto newIDs = map->addPointAnnotations(points, markers); + annotationIDs.insert(annotationIDs.end(), newIDs.begin(), newIDs.end()); +} + +void GLFWView::addRandomShapeAnnotations(int count) { + std::vector<mbgl::AnnotationSegments> shapes; + std::vector<mbgl::StyleProperties> shapesProperties; + + mbgl::FillProperties fillProperties; + fillProperties.opacity = .1; + + mbgl::StyleProperties properties; + properties.set<mbgl::FillProperties>(fillProperties); for (int i = 0; i < count; i++) { - const double lon = sw.longitude + (ne.longitude - sw.longitude) * (double(std::rand()) / RAND_MAX); - const double lat = sw.latitude + (ne.latitude - sw.latitude) * (double(std::rand()) / RAND_MAX); + mbgl::AnnotationSegment triangle; + triangle.push_back(makeRandomPoint()); + triangle.push_back(makeRandomPoint()); + triangle.push_back(makeRandomPoint()); - points.push_back({ lat, lon }); - markers.push_back("default_marker"); + mbgl::AnnotationSegments segments; + segments.push_back(triangle); + + shapes.push_back(segments); + shapesProperties.push_back(properties); + } + + auto newIDs = map->addShapeAnnotations(shapes, shapesProperties); + annotationIDs.insert(annotationIDs.end(), newIDs.begin(), newIDs.end()); +} + +void GLFWView::clearAnnotations() { + if (annotationIDs.empty()) { + return; + } + + map->removeAnnotations(annotationIDs); + annotationIDs.clear(); +} + +void GLFWView::popAnnotation() { + if (annotationIDs.empty()) { + return; } - map->addPointAnnotations(points, markers); + map->removeAnnotation(annotationIDs.back()); + annotationIDs.pop_back(); } void GLFWView::onScroll(GLFWwindow *window, double /*xOffset*/, double yOffset) { |